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 frmGSTR2BApi : Form
    {
        GSTSession GstSession = new GSTSession();

        public frmGSTR2BApi()
        {
            try
            {
                InitializeComponent();

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

        private void frmGSTR2BApi_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 btnDownloadData_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                if(File.Exists("Response.json"))
                    File.Delete("Response.json");
                TxnRespWithObj<GSTR2BJson> txnResp = new TxnRespWithObj<GSTR2BJson>();
                txnResp = await GSTR2BAPI.GetAllDetails (GstSession, "Get GSTR-2B Data", 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 btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
