using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TaxProGST.API;
using TaxProGST.JsonModels;

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

        public frmGSTR3BApi()
        {
            try
            {
                InitializeComponent();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtPAN_TextChanged(object sender, EventArgs e)
        {

        }

        private void frmGSTR3BApi_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 btnDownloadAutoCalc1_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                if(File.Exists("Response.json"))
                    File.Delete("Response.json");
                TxnRespWithObj<Gstr3bAutoCalcGstr1LiabilityJson> txnResp = new TxnRespWithObj<Gstr3bAutoCalcGstr1LiabilityJson>();
                txnResp = await GSTR3BAPI.GetGstr1LiabilityAutoCalcDetailsAsync(GstSession, " Get GSTR1 Liability Auto Calc Details", txtGSTIN.Text, txtReturnPeriod.Text, true, "Response.json");
                if(File.Exists("Response.json"))
                    txtAPIResponse.Text = File.ReadAllText("Response.json");
                else
                    txtAPIResponse.Text = txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnDownloadAutoCalc2_Click(object sender, EventArgs e)
        {
            try
            {
                await Task.Delay(200);
                txtAPIResponse.Text = "Please wait...";
                if(File.Exists("Response.json"))
                    File.Delete("Response.json");
                TxnRespWithObj<Gstr3bAutoPopulatedJson> txnResp = new TxnRespWithObj<Gstr3bAutoPopulatedJson>();
                txnResp = await GSTR3BAPI.GetGstr1LiabilityAndItcAutoCalcDetailsAsync(GstSession, " Get GSTR1 Liability and ITC Auto Calc Details",txtGSTIN.Text, txtReturnPeriod.Text,true, "Response.json");
                if(File.Exists("Response.json"))
                    txtAPIResponse.Text = File.ReadAllText("Response.json");
                else
                    txtAPIResponse.Text = txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnBrowseGstr3BJson_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog FileDialog = new OpenFileDialog();
                FileDialog.Filter = "GSTR-3B JSon Files (*.json)|*.json";  //Set filters
                FileDialog.FilterIndex = 1;          //Specify default filter
                if(FileDialog.ShowDialog() == DialogResult.Cancel)
                    return;
                txtGSTR3BJson.Text = FileDialog.FileName;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnUploadData_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                if(string.IsNullOrEmpty(txtGSTR3BJson.Text))
                    MessageBox.Show("GSTR-3B Json file path not provided");
                txtReferenceID.Text = "";
                lblRefID.Text = "";
                #region For Uploading using Json File
                string json = File.ReadAllText(txtGSTR3BJson.Text);
                TxnRespWithTransAndRefID txnResp = await GSTR3BAPI.SaveGstr3BAsync(GstSession, txtReturnPeriod.Text, json);
                #endregion
                #region For Upload using object Sample Example Only
                //GSTR3BJson gstr3b = new GSTR3BJson();
                //gstr3b.gstin = txtGSTIN.Text;
                //gstr3b.ret_period = txtReturnPeriod.Text;
                //gstr3b.sup_details = new GSTR3BJson.SupDetails();
                //gstr3b.sup_details.osup_det = new GSTR3BJson.SupDetails.OsupDet();
                //gstr3b.sup_details.osup_det.txval = 100000;
                //gstr3b.sup_details.osup_det.iamt = 1000;
                //gstr3b.sup_details.osup_det.camt = 2000;
                //gstr3b.sup_details.osup_det.samt = 2000;
                //gstr3b.sup_details.osup_det.csamt = 0;
                //gstr3b.sup_details.osup_zero = new GSTR3BJson.SupDetails.OsupZero();
                //gstr3b.sup_details.osup_zero.txval = 0;
                //gstr3b.sup_details.osup_zero.iamt = 0;
                //gstr3b.sup_details.osup_zero.csamt = 0;
                //gstr3b.sup_details.osup_nil_exmp = new GSTR3BJson.SupDetails.OsupNilExmp();
                //gstr3b.sup_details.osup_nil_exmp.txval = 0;
                //gstr3b.sup_details.isup_rev = new GSTR3BJson.SupDetails.IsupRev();
                //gstr3b.sup_details.isup_rev.txval = 0;
                //gstr3b.sup_details.isup_rev.iamt = 0;
                //gstr3b.sup_details.isup_rev.camt = 0;
                //gstr3b.sup_details.isup_rev.samt = 0;
                //gstr3b.sup_details.isup_rev.csamt = 0;
                //gstr3b.sup_details.osup_nongst = new GSTR3BJson.SupDetails.OsupNongst();
                //gstr3b.sup_details.osup_nongst.txval = 0;
                //gstr3b.itc_elg = new GSTR3BJson.ItcElg();
                //gstr3b.itc_elg.itc_avl = new List<GSTR3BJson.ItcElg.ItcDetails>();
                //GSTR3BJson.ItcElg.ItcDetails itcavl = new GSTR3BJson.ItcElg.ItcDetails();
                //itcavl.ty = "OTH";
                //itcavl.iamt = 10000;
                //itcavl.camt = 10000;
                //itcavl.samt = 10000;
                //itcavl.csamt = 0;
                //gstr3b.itc_elg.itc_avl.Add(itcavl);
                //gstr3b.itc_elg.itc_net = new GSTR3BJson.ItcElg.ItcNet();
                //gstr3b.itc_elg.itc_net.iamt = 10000;
                //gstr3b.itc_elg.itc_net.camt = 10000;
                //gstr3b.itc_elg.itc_net.samt = 10000;
                //gstr3b.itc_elg.itc_net.csamt = 0;
                //gstr3b.intr_ltfee = new GSTR3BJson.IntrLtfee();
                //gstr3b.intr_ltfee.intr_details = new GSTR3BJson.IntrLtfee.IntrDetails();
                //gstr3b.intr_ltfee.intr_details.iamt = 0;
                //gstr3b.intr_ltfee.intr_details.camt = 0;
                //gstr3b.intr_ltfee.intr_details.samt = 0;
                //gstr3b.intr_ltfee.intr_details.csamt = 0;
                //TxnRespWithTransAndRefID txnResp = await GSTR3BAPI.SaveGstr3BAsync(GstSession, txtReturnPeriod.Text, gstr3b);

                #endregion

                if(txnResp.IsSuccess)
                {
                    txtReferenceID.Text = txnResp.ReferenceID;
                    lblRefID.Text = txnResp.ReferenceID;
                }
                txtAPIResponse.Text = txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private async void btnCheckReturnStatus_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                TxnRespWithObj<Gstr3BReturnStatusJson> txnResp = await SharedAPI.GetReturnStatusAsync<Gstr3BReturnStatusJson>(GstSession, "GSTR1", txtGSTIN.Text, txtReturnPeriod.Text, lblRefID.Text, true);
                if(txnResp.IsSuccess)
                {
                    txtAPIResponse.Text = txnResp.TxnOutcome;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnDownloadData_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                #region Method1 - Get Payload
                TxnRespWithPayload txnResp1 = await GSTR3BAPI.GetGstr3BDataSchedulePayloadAsync(GstSession, "RETSUM", " Get GSTR3B Details", txtGSTIN.Text , txtReturnPeriod.Text, "", true);
                if(txnResp1.IsSuccess)
                    txtAPIResponse.Text  = txnResp1.ResponsePayload;
                else
                    txtAPIResponse.Text = txnResp1.TxnOutcome;
                #endregion
                #region Method2 - Get Object
                //TxnRespWithObj<GSTR3BJson> txnResp = new TxnRespWithObj<GSTR3BJson>();
                //txnResp = await GSTR3BAPI.GetGstr3BDataScheduleAsync(GstSession, "RETSUM", " Get GSTR3B Details", txtGSTIN.Text, txtReturnPeriod.Text, "", true);
                //txtAPIResponse.Text = txnResp.TxnOutcome;
                #endregion

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

        private async void btnOffsetData_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                GSTR3BJson jGSTR3B = new GSTR3BJson();
                jGSTR3B.pdcash = new List<GSTR3BJson.pdCash>();
                GSTR3BJson.pdCash pdcash = new GSTR3BJson.pdCash();
                pdcash.liab_ldg_id = 1234;
                pdcash.trans_typ = 30002;
                pdcash.ipd = 0;
                pdcash.cpd = 0;
                pdcash.spd = 0;
                pdcash.cspd = 0;
                pdcash.i_intrpd = 0;
                pdcash.c_intrpd = 0;
                pdcash.s_intrpd = 0;
                pdcash.cs_intrpd = 0;
                pdcash.c_lfeepd = 0;
                pdcash.s_lfeepd = 0;
                jGSTR3B.pdcash.Add(pdcash);
                jGSTR3B.pditc = new GSTR3BJson.pdItc();
                jGSTR3B.pditc.liab_ldg_id = 12321;
                jGSTR3B.pditc.trans_typ = 30002;
                jGSTR3B.pditc.i_pdi = 1000;
                jGSTR3B.pditc.i_pdc = 1000;
                jGSTR3B.pditc.i_pds = 1000;
                jGSTR3B.pditc.c_pdi = 0;

                jGSTR3B.pditc.c_pdc = 0;
                jGSTR3B.pditc.s_pdi = 0;
                jGSTR3B.pditc.s_pds = 0;
                jGSTR3B.pditc.cs_pdcs =0;
                TxnRespWithTransAndRefID txnResp = await GSTR3BAPI.OffsetLiabilityGstr3BAsync(GstSession, txtReturnPeriod.Text, jGSTR3B, true);
                txtAPIResponse.Text = txnResp.TxnOutcome;

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

        private async void btnEVCOTP_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                TxnRespWithRefAck txnResp = await AuthAPI.GetEVCOTPAsync(GstSession, txtGSTIN.Text, "R3B", txtPAN.Text);
                if(txnResp.IsSuccess)
                    txtAPIResponse.Text = "EVC OTP Send Successfully on Registered Mobile and Email";
                else
                    txtAPIResponse.Text = "Failed : Response - " + txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cboSignType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cboSignType.Text == "EVC")
            {
                btnEVCOTP.Enabled = true;
                txtEVCOTP.ReadOnly = false;
            }
            else
            {
                btnEVCOTP.Enabled = false;
                txtEVCOTP.ReadOnly = true;
            }
        }

        private async void btnFileReturn_Click(object sender, EventArgs e)
        {
            try
            {

                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                TxnRespWithPayload txnResp;
                TxnRespWithRefAck txtRespWithARN = new TxnRespWithRefAck();
                string gstr3BSummary = "";
                txnResp = await GSTR3BAPI.GetGstr3BDataSchedulePayloadAsync(GstSession, "RETSUM", " Get GSTR3B Details", txtGSTIN.Text, txtReturnPeriod.Text, "", true);
                if(txnResp.IsSuccess && !string.IsNullOrEmpty(txnResp.ResponsePayload))
                {
                    gstr3BSummary = txnResp.ResponsePayload;
                    txtRespWithARN = await GSTR3BAPI.FileReturn3BAsync(GstSession, "GSTR3B", txtReturnPeriod.Text, gstr3BSummary, cboSignType.Text, txtPAN.Text, true, txtEVCOTP.Text);
                    txtAPIResponse.Text = txtRespWithARN.TxnOutcome;
                }
                else
                    txtAPIResponse.Text = txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

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