using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TaxProEInvoice.API;

namespace TaxProEinvoiceApiIntigrationDemo.NET
{
    public partial class MultiTPForm : Form
    {
        private int intTPId;
        private TPSession tpSession;
        
        public MultiTPForm()
        {
            InitializeComponent();
            int Irp = 1; //Default Irp value
            int id = 1;//Login detail will load for TaxPayer Id = 1;
            tpSession = new TPSession(id, Irp);
        }
       
        private void MultiTPForm_Load(object sender, EventArgs e)
        {
            
        }
        //public void DisplayLoginDetailInMulty(eInvoiceSession EInvSession)
        //{
        //    ClearTextField();
        //    txtGSTIN2.Text = EInvSession.eInvApiLoginDetails.GSTIN;
        //    txtUserName.Text = EInvSession.eInvApiLoginDetails.UserName;
        //    txtPassword.Text = EInvSession.eInvApiLoginDetails.Password;
        //    txtAppKey.Text = EInvSession.eInvApiLoginDetails.AppKey;
        //    txtSEK.Text = EInvSession.eInvApiLoginDetails.Sek;
        //    txtTokenExp.Text = EInvSession.eInvApiLoginDetails.E_InvoiceTokenExp.ToString();
        //    txtAuthToken.Text = EInvSession.eInvApiLoginDetails.AuthToken;
        //    //txtTaxPayerName.Text = tpSession.eInvApiLoginDetails.TaxPayerName;
        //}

        //private void ClearTextField()
        //{
        //    txtAppKey.Text = "";
        //    txtSEK.Text = "";
        //    txtTokenExp.Text = "";
        //    txtAuthToken.Text = "";
        //}



        public async void btnLoadTPSession_Click(object sender, EventArgs e)
        {
            //DBContext dB = new DBContext();
            ////access login detail from database by GSTIN
            //string gstin = txtGSTIN.Text;
            //try
            //{
            //    einvlogindetail einvlogindetail = new einvlogindetail();
            //    einvlogindetail = await dB.eInvLoginDetail.Where(x => x.GSTIN == gstin).FirstOrDefaultAsync();
            //    txtGSTIN2.Text = einvlogindetail.GSTIN;
            //    txtUserName.Text = einvlogindetail.UserName;
            //    txtPassword.Text = einvlogindetail.Password;
            //    txtAppKey.Text = einvlogindetail.AppKey;
            //    txtSEK.Text = einvlogindetail.Sek;
            //    txtTokenExp.Text = einvlogindetail.E_InvoiceTokenExp.ToString();
            //    txtAuthToken.Text = einvlogindetail.AuthToken;
            //    txtTaxPayerName.Text = einvlogindetail.TaxPayerName;


            //    tpSession = new TPSession(einvlogindetail.Id);
                
            //}
            //catch (Exception ex)
            //{
            //    rtbResponce.Text = ex.Message;
            //}


        }

       

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private async void button1_Click(object sender, EventArgs e)
        {
            TxnRespWithObj<RespPlGenIRN> txnRespWithObj = await eInvoiceAPI.GenIRNAsync(tpSession, rtbPayload.Text, 250);
            txtAppKey.Text = tpSession.eInvApiLoginDetails.AppKey;
            txtSEK.Text = tpSession.eInvApiLoginDetails.Sek;
            txtTokenExp.Text = tpSession.eInvApiLoginDetails.E_InvoiceTokenExp.ToString();
            txtAuthToken.Text = tpSession.eInvApiLoginDetails.AuthToken;

            RespPlGenIRN respPlGenIRN = txnRespWithObj.RespObj;
            string ErrorCodes = "";
            string ErrorDesc = "";
            rtbResponce.Text = "";

            if (txnRespWithObj.IsSuccess)
            {
                rtbResponce.Text = JsonConvert.SerializeObject(respPlGenIRN);
            }
            else
            {
                //Error has occured
                //Display TxnOutCome in text box - process or show msg to user

                //Process error codes
                if (txnRespWithObj.ErrorDetails != null)
                {
                    foreach (RespErrDetailsPl errPl in txnRespWithObj.ErrorDetails)
                    {
                        //Process errPl item here
                        ErrorCodes += errPl.ErrorCode + ",";
                        ErrorDesc += errPl.ErrorCode + ": " + errPl.ErrorMessage + Environment.NewLine;
                        rtbResponce.Text = ErrorDesc;
                    }
                }

                //Process InfoDetails here
                RespInfoDtlsPl respInfoDtlsPl = new RespInfoDtlsPl();
                //Serialize Desc object from InfoDtls as per InfCd
                if (txnRespWithObj.InfoDetails != null)
                {
                    foreach (RespInfoDtlsPl infoPl in txnRespWithObj.InfoDetails)
                    {
                        var strDupIrnPl = JsonConvert.SerializeObject(infoPl.Desc);   //Convert object type to json string
                        switch (infoPl.InfCd)
                        {
                            case "DUPIRN":
                                DupIrnPl dupIrnPl = JsonConvert.DeserializeObject<DupIrnPl>(strDupIrnPl);
                                break;
                            case "EWBERR":
                                List<EwbErrPl> ewbErrPl = JsonConvert.DeserializeObject<List<EwbErrPl>>(strDupIrnPl);
                                break;
                            case "ADDNLNFO":
                                //Deserialize infoPl.Desc as string type and then if this string contains json object, it may be desirilized again as per future releases
                                string strDesc = (string)infoPl.Desc;
                                break;
                        }
                    }
                }
            }

        }

        private void txtAuthToken_TextChanged(object sender, EventArgs e)
        {

        }
    }

    public class eInvLoginDetail
    {

    }
}
