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

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

        public frmPublicAPI()
        {
            try
            {
                InitializeComponent();

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

        private void frmPublicAPI_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 btnSearchTaxPayer_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please Wait...";
                await Task.Delay(200);
                TxnRespWithObj<SearchTaxpayerJson> jsonResp = await PublicAPI.SearchTaxPayerAsync(GstSession, txtSearchGSTIN.Text.Trim());
                if(jsonResp.IsSuccess && jsonResp.RespObj != null)
                {
                    txtAPIResponse.Text = JsonConvert.SerializeObject(jsonResp.RespObj, Formatting.Indented);
                }
                else
                    txtAPIResponse.Text = jsonResp.TxnOutcome;
                #region Alternative Method
                //TxnResp txnresp1 = await GstSession.StartAPITxn(true);
                //bool IsSandboxURL = GstSession?.ApiSetting?.ASPName?.ToUpper()?.Contains("SANDBOX") ?? false;
                //string TxnID = "TaxPro" + GstSession?.ApiLoginDetails?.GSTIN?.Substring(0, 4) + DateTime.Now.ToString("ddMMyHHmmssfff");
                //TxnRespWithObj<SearchTaxpayerJson> jsonResp1 = await PublicAPI.SearchTaxPayerAsync((GstSession?.ApiSetting?.AspUserId ?? ""), TPCrypto.AesEncryptBase64(GstSession.ApiSetting?.AspPassword, GstSession.ApiSetting?.AspEK ?? ""), GstSession?.ApiSetting?.AspSessionID, TxnID, GstSession?.ApiLoginDetails?.GSTIN?.Trim(), txtSearchGSTIN.Text.Trim(), IsSandboxURL);
                //if(jsonResp1.IsSuccess && jsonResp1.RespObj != null)
                //{
                //    txtAPIResponse.Text = JsonConvert.SerializeObject(jsonResp1.RespObj, Formatting.Indented);
                //}
                //else
                //    txtAPIResponse.Text = jsonResp1.TxnOutcome;

                #endregion Alternative Method

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

        private async void btnGetReturnFileDetails_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please Wait...";
                await Task.Delay(200);
                TxnRespWithObj<EFiledReturn> jsonResp = await PublicAPI.ViewAndTrackAllReturnAsync(GstSession, cboFY.Text);
                GstSession.ApiLoginDetails.GSTIN = txtGstinForReturnFileDetail.Text;
                if(jsonResp.IsSuccess && jsonResp.RespObj != null)
                {
                    txtAPIResponse.Text = JsonConvert.SerializeObject(jsonResp.RespObj, Formatting.Indented);
                }
                else
                    txtAPIResponse.Text = jsonResp.TxnOutcome;
                #region Alternative Method
                //bool IsSandboxURL = (bool)GstSession.ApiSetting.ASPName?.ToUpper().Contains("SANDBOX");
                //string TxnID = "TaxPro" + GstSession?.ApiLoginDetails?.GSTIN?.Substring(0, 4) + DateTime.Now.ToString("ddMMyHHmmssfff");

                //TxnRespWithObj<EFiledReturn> jsonResp1 = await PublicAPI.ViewAndTrackAllReturnAsync(GstSession.ApiSetting.AspUserId, TPCrypto.AesEncryptBase64(GstSession.ApiSetting?.AspPassword, GstSession.ApiSetting?.AspEK ?? ""), GstSession.ApiSetting.AspSessionID, TxnID, txtGstinForReturnFileDetail.Text, txtGstinForReturnFileDetail.Text, cboFY.Text,false, IsSandboxURL);
                //if(jsonResp1.IsSuccess && jsonResp1.RespObj != null)
                //{
                //    txtAPIResponse.Text = JsonConvert.SerializeObject(jsonResp1.RespObj, Formatting.Indented);
                //}
                //else
                //    txtAPIResponse.Text = jsonResp1.TxnOutcome;

                #endregion Alternative Method


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

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

        private async void button1_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> txnResp = await PublicAPI.GetPreferenceAsync(GstSession, txtGetPreferenceGSTIN.Text, cboFY3.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);
            }
        }
    }
}
