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 frmPreference : Form
    {
        GSTSession GstSession = new GSTSession();
        public frmPreference()
        {
            try
            {
                InitializeComponent();
               
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frmPreference_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 btnSavePreference_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                SavePreferenceJson SetPreference = new SavePreferenceJson();
                SetPreference.fy = cboFY1.Text;
                SetPreference.gstin = txtGSTIN.Text;
                if(!string.IsNullOrEmpty(cboPreference.Text))
                    SetPreference.preference = cboPreference.Text.Substring(0, 1);
                SetPreference.quarter = cboQuarter.Text;
                TxnResp txnResponse = await SharedAPI.SavePreferenceAsync(GstSession, SetPreference);
                txtAPIResponse.Text = txnResponse.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnGetPreference_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please wait...";
                await Task.Delay(200);
                if(File.Exists("Response.json"))
                    File.Delete("Response.json");
                TxnRespWithObj<GetPreferenceJson> txnResponse =await SharedAPI.GetPreferenceAsync(GstSession, txtGSTIN.Text, cboFY2.Text, true, "Response.json");
                if(File.Exists("Response.json"))
                    txtAPIResponse.Text = File.ReadAllText("Response.json");
                else
                    txtAPIResponse.Text = txnResponse.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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