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;

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

        public frmAuthToken()
        {
            InitializeComponent();
            GstSession.ApiSetting = Shared.LoadAPISetting();
            GstSession.ApiLoginDetails = Shared.LoadAPILoginDetails();
            ShowUpdatedAPILoginDetails();
        }
        public void ShowUpdatedAPILoginDetails()
        {
            try
            {
                if(GstSession.ApiLoginDetails != null)
                {
                    txtGSTUserID.Text = GstSession.ApiLoginDetails?.GstPortalUserID ?? "";
                    txtGSTIN.Text = GstSession.ApiLoginDetails?.GSTIN ?? "";
                    txtAppKey.Text = GstSession.ApiLoginDetails?.AppKey ?? "";
                    txtAuthToken.Text = GstSession.ApiLoginDetails?.AuthToken ?? "";
                    if(GstSession.ApiLoginDetails.TokenExp != null)
                        txtExpiry.Text = GstSession.ApiLoginDetails.TokenExp.ToString("dd/MM/yyyy hh:mm:ss tt");
                    else
                        txtExpiry.Text = "";
                    txtSEK.Text = GstSession.ApiLoginDetails?.SessionEK ?? "";
                }
            }
            catch(Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
           
        }
        private async void btnGetOTP_Click(object sender, EventArgs e)
        {
            try
            {
                lblOutCome.Text = "Wait...";
                await Task.Delay(200);
                if(GstSession.ApiLoginDetails == null)
                    GstSession.ApiLoginDetails = new APILoginDetails();
                if(GstSession.ApiLoginDetails.GSTIN != txtGSTIN.Text)
                {
                    GstSession.ApiLoginDetails.GSTIN = txtGSTIN.Text;
                    GstSession.ApiLoginDetails.GstPortalUserID = txtGSTUserID.Text;
                }
                #region New Code with RefreshToken Explaination Call
                if(GstSession.ApiLoginDetails.TokenExp > DateTime.Now)
                {
                    if(MessageBox.Show("Auth Token for this TaxPayer is Active.Do you want to Cancel it?", "",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.No)
                    {
                        TxnRespWithRefAck txnResp = await AuthAPI.GetOTPAsync(GstSession);
                        //Update AppKey in APILoginDetails
                        lblOutCome.Text = txnResp.IsSuccess.ToString() + " - " + txnResp.TxnOutcome +
                               "\nAppKey Established (Please Store): " + txnResp.RefOrAckNo;
                        //Save Updated Details
                        Shared.SaveAPISetting(GstSession.ApiSetting);
                        Shared.SaveAPILoginDetails(GstSession.ApiLoginDetails);
                        ShowUpdatedAPILoginDetails();
                    }
                    else
                        lblOutCome.Text = "Auth Token for this TaxPayer is Active";
                }
                else if(DateTime.Compare(DateTime.Now, GstSession.ApiLoginDetails.TokenExp.AddHours(18)) < 0 && GstSession != null && GstSession.ApiLoginDetails.SessionEK != null)
                {
                    //Token is Expired and needs Refresh
                    TxnRespWithObj<APISession> txnRespRefresh = await AuthAPI.RefreshAuthTokenAsync(GstSession);
                    if(txnRespRefresh.IsSuccess)
                    {
                        lblOutCome.Text = "Auth Token  Refreshed Automatically for this TaxPayer & Session is Active you no need to Call OTP. Note Authtoken can be refresh within 24 Hr from First Successful Auth Token Request";
                        Shared.SaveAPISetting(GstSession.ApiSetting);
                        Shared.SaveAPILoginDetails(GstSession.ApiLoginDetails);
                        ShowUpdatedAPILoginDetails();
                    }
                    else
                    {
                        TxnRespWithRefAck txnResp = await AuthAPI.GetOTPAsync(GstSession);
                        //Update AppKey in APILoginDetails
                        lblOutCome.Text = txnResp.IsSuccess.ToString() + " - " + txnResp.TxnOutcome +
                                   "\nAppKey Established (Please Store): " + txnResp.RefOrAckNo;
                        //Save Updated Details
                        Shared.SaveAPISetting(GstSession.ApiSetting);
                        Shared.SaveAPILoginDetails(GstSession.ApiLoginDetails);
                        ShowUpdatedAPILoginDetails();

                    }

                }
                else
                {
                    TxnRespWithRefAck otpResp = await AuthAPI.GetOTPAsync(GstSession);
                    lblOutCome.Text = otpResp.IsSuccess.ToString() + " - " + otpResp.TxnOutcome +
                        "\nAppKey Established (Please Store): " + otpResp.RefOrAckNo;
                    //Save Updated Details
                    Shared.SaveAPISetting(GstSession.ApiSetting);
                    Shared.SaveAPILoginDetails(GstSession.ApiLoginDetails);
                    ShowUpdatedAPILoginDetails();
                }
                #endregion
                #region Old Code
                //lblOutCome.Text = "Wait...";
                //await Task.Delay(200);
                //if(GstSession.ApiLoginDetails == null)
                //    GstSession.ApiLoginDetails = new APILoginDetails();
                //if(GstSession.ApiLoginDetails.GSTIN != txtGSTIN.Text)
                //{
                //    GstSession.ApiLoginDetails.GSTIN = txtGSTIN.Text;
                //    GstSession.ApiLoginDetails.GstPortalUserID = txtGSTUserID.Text;
                //}
                //TxnRespWithRefAck otpResp = await AuthAPI.GetOTPAsync(GstSession);
                //lblOutCome.Text = otpResp.IsSuccess.ToString() + " - " + otpResp.TxnOutcome +
                //    "\nAppKey Established (Please Store): " + otpResp.RefOrAckNo;
                ////Save Updated Details
                //Shared.SaveAPISetting(GstSession.ApiSetting);
                //Shared.SaveAPILoginDetails(GstSession.ApiLoginDetails);
                //ShowUpdatedAPILoginDetails();
                #endregion
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          
        }

        private async void btnGetAuthToken_Click(object sender, EventArgs e)
        {
            try
            {
                lblOutCome.Text = "Please Wait...";
                await Task.Delay(200);
                if(GstSession.ApiLoginDetails == null)
                    GstSession.ApiLoginDetails = new APILoginDetails();
                if(GstSession.ApiLoginDetails.GSTIN != txtGSTIN.Text)
                {
                    GstSession.ApiLoginDetails.GSTIN = txtGSTIN.Text;
                    GstSession.ApiLoginDetails.GstPortalUserID = txtGSTUserID.Text;
                }
                TxnRespWithObj<APISession> AuthResp = await AuthAPI.GetAuthTokenAsync(GstSession, txtOTP.Text);
                lblOutCome.Text = AuthResp.TxnOutcome + "\nAuth Token (Please Store): " + GstSession.ApiLoginDetails.AuthToken +
                    "\nSession EK: " + GstSession.ApiLoginDetails.SessionEK +
                    "\nExpiary: " + GstSession.ApiLoginDetails.TokenExp;
                //Save Updated Details
                Shared.SaveAPISetting(GstSession.ApiSetting);
                Shared.SaveAPILoginDetails(GstSession.ApiLoginDetails);
                ShowUpdatedAPILoginDetails();

            }
            catch(Exception ex)
            {

                MessageBox.Show(ex.Message); 
            }
          
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                GstSession.ApiLoginDetails.GSTIN = txtGSTIN.Text;
                GstSession.ApiLoginDetails.GstPortalUserID = txtGSTUserID.Text;
                //Save Updated Details
                Shared.SaveAPISetting(GstSession.ApiSetting);
                Shared.SaveAPILoginDetails(GstSession.ApiLoginDetails);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

        private void txtGSTIN_TextChanged(object sender, EventArgs e)
        {
            txtAppKey.Text = "";
            txtAuthToken.Text = "";
            txtExpiry.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
        }
    }
}
