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 Newtonsoft.Json;
using TaxProGST.API;
using TaxProGST.JsonModels;

namespace TaxProGSTApiWinFormsDemo
{
    public partial class frmLedgerAPI : Form
    {
        GSTSession GstSession = new GSTSession();
        public frmLedgerAPI()
        {
            try
            {
                InitializeComponent();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void frmLedgerAPI_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 btnGetITCLedgerDetails_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please Wait...";
                await Task.Delay(200);
                TxnRespWithObj<ITCLedgerDetailJson> txnResp = await LedgerAPI.GetITCLedgerAsync(GstSession, dtpItcLedgerFromDate.Text.Replace("/", "-"), dtpItcLedgerToDate.Text.Replace("/", "-"));
                if(txnResp.IsSuccess && txnResp.RespObj != null)
                    txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                else
                    txtAPIResponse.Text = txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnGetCashLedgerDetails_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please Wait...";
                await Task.Delay(200);
                TxnRespWithObj<CashLedgerDetailJson> txnResp = await LedgerAPI.GetCashLedgerAsync(GstSession, dtpCashLedgerFromDate.Text.Replace("/", "-"), dtpCashLedgerToDate.Text.Replace("/", "-"));
                if(txnResp.IsSuccess && txnResp.RespObj != null)
                    txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                else
                    txtAPIResponse.Text = txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnGetLiabilityLedgerDetails_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please Wait...";
                await Task.Delay(200);
                TxnRespWithObj<LiabLedgerDetailJson> txnResp = await LedgerAPI.GetLiabilityLedgerAsync(GstSession, txtReturnPeriod.Text);
                if(txnResp.IsSuccess && txnResp.RespObj != null)
                    txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                else
                    txtAPIResponse.Text = txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnGetItcCashBalanceDetails_Click(object sender, EventArgs e)
        {
            try
            {
                txtAPIResponse.Text = "Please Wait...";
                await Task.Delay(200);
                TxnRespWithObj<CashITCBalanceJson>  txnResp = await LedgerAPI.GetCashITCBalanceAsync(GstSession, txtReturnPeriod.Text);
                if(txnResp.IsSuccess && txnResp.RespObj != null)
                    txtAPIResponse.Text = JsonConvert.SerializeObject(txnResp.RespObj, Formatting.Indented);
                else
                    txtAPIResponse.Text = txnResp.TxnOutcome;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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