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 frmGSTR2AApi : Form
    {
        GSTSession GstSession = new GSTSession();
        public frmGSTR2AApi()
        {
            try
            {
                InitializeComponent();

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

        private void frmGSTR2AApi_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<Gstr2Json> txnResp = new TxnRespWithObj<Gstr2Json>();
                txnResp = await GSTR2API.GetGstr2ADataScheduleAsync(GstSession, cboSchedule.Text, "Get " + cboSchedule.Text +" 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();
        }
    }
}
