using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using TaxProGST.API;
using TaxProGST.JsonModels;

namespace TaxProGSTApiWinFormsDemo
{
    public partial class frmEinvoiceAPI : Form
    {
        GSTSession GstSession = new GSTSession();

        public frmEinvoiceAPI()
        {
            try
            {
                InitializeComponent();
                cboSupplyType.SelectedIndex = 0;

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtTokenNoResponse_TextChanged(object sender, EventArgs e)
        {
            txtTokenNo.Text = txtTokenNoResponse.Text;
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void frmEinvoiceAPI_Load(object sender, EventArgs e)
        {
            try
            {
                GstSession.ApiSetting = Shared.LoadAPISetting();
                GstSession.ApiLoginDetails = Shared.LoadAPILoginDetails();
                if(GstSession.ApiLoginDetails != null)
                {
                    txtGSTUserID.Text = GstSession.ApiLoginDetails?.GstPortalUserID ?? "";
                    txtGSTIN.Text = GstSession.ApiLoginDetails?.GSTIN ?? "";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnIrnListAsSupplier_Click(object sender, EventArgs e)
        {
            try
            {
                if(string.IsNullOrEmpty(cboSupplyType.Text))
                {
                    MessageBox.Show("Supply Type Not Selected");
                    return;
                }
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                txtTokenNoResponse.Text = "";
                TxnRespWithObj<IrnListJson> txnResp = await EInvoiceAPI.GetIrnListAsync(GstSession,"Get IRN List",txtGSTIN.Text, txtReturnPeriod.Text, cboSupplyType.Text,txtGSTIN.Text,"");

                if(txnResp.IsSuccess)
                {
                    txtTokenNoResponse.Text = txnResp.TokenNumber;
                    if(string.IsNullOrEmpty(txnResp.TokenNumber) && txnResp.RespObj != null)
                        txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                    else
                        txtAPIResponse.Text = txnResp.TxnOutcome;
                }
                else
                {
                    txtAPIResponse.Text = txnResp.TxnOutcome;

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnIrnListAsReceipient_Click(object sender, EventArgs e)
        {
            try
            {
                if(string.IsNullOrEmpty(cboSupplyType.Text))
                {
                    MessageBox.Show("Supply Type Not Selected");
                    return;
                }
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                txtTokenNoResponse.Text = "";
                TxnRespWithObj<IrnListJson> txnResp = await EInvoiceAPI.GetIrnListAsync(GstSession, "Get IRN List", txtGSTIN.Text, txtReturnPeriod.Text, cboSupplyType.Text, "",txtGSTIN.Text);
                if(txnResp.IsSuccess)
                {
                    txtTokenNoResponse.Text = txnResp.TokenNumber;
                    if(string.IsNullOrEmpty(txnResp.TokenNumber) && txnResp.RespObj != null)
                        txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                    else
                        txtAPIResponse.Text = txnResp.TxnOutcome;
                }
                else
                {
                    txtAPIResponse.Text = txnResp.TxnOutcome;

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnGetFileDetails_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                txtFileDetailResponse.Text = "";
                TxnRespWithObj<FileDetailResponse> txnResp = await EInvoiceAPI.GetFileDetailsFromToken(GstSession, txtGSTIN.Text, txtReturnPeriod.Text, txtTokenNo.Text);
                if(txnResp.IsSuccess)
                {
                    if(txnResp.RespObj != null)
                    {
                        txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                        txtFileDetailResponse.Text = txtAPIResponse.Text;
                    }
                    else
                        txtAPIResponse.Text = txnResp.TxnOutcome;
                }
                else
                {
                    txtAPIResponse.Text = txnResp.TxnOutcome;

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnGetIRNListUsingFileDetailResponse_Click(object sender, EventArgs e)
        {
            try
            {
                if(string.IsNullOrEmpty(txtFileDetailResponse.Text))
                {
                    MessageBox.Show("File Detail Response is Empty. Provide File Detail Response and then try");
                    return;
                }
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                FileDetailResponse fileDetailResp = JsonConvert.DeserializeObject<FileDetailResponse>(txtFileDetailResponse.Text);
                TxnRespWithObj<IrnListJson> txnResp = await EInvoiceAPI.GetIrnListFromFileDetailResp(fileDetailResp, AppDomain.CurrentDomain.BaseDirectory + @"\EINVOICE");
                if(txnResp.IsSuccess)
                {
                    if(txnResp.RespObj != null)
                    {
                        txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                    }
                    else
                        txtAPIResponse.Text = txnResp.TxnOutcome;
                }
                else
                {
                    txtAPIResponse.Text = txnResp.TxnOutcome;

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnGetIRNDetail_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                TxnRespWithObj<IrnDetailJson> txnResp = await EInvoiceAPI.GetIRNDetailsAsync(GstSession, "Get IRN Detail", txtGSTIN.Text, txtIRN.Text);
                if(txnResp.IsSuccess)
                {
                    if( txnResp.RespObj != null)
                        txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                    else
                        txtAPIResponse.Text = txnResp.TxnOutcome;
                }
                else
                {
                    txtAPIResponse.Text = txnResp.TxnOutcome;

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
