using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using TaxProEWB.API;


namespace TaxProEInvEWBDemo.Net
{
    public partial class EWBDemoForm : Form
    {
        public static HttpClient client = new HttpClient();
        public const string AppJsonMediaType = "application/json";
        //====================== NOTE ON WORKING OF EWBSession AND IT'S DEFAULT PARAMATERS  ============================================
        //EWBSession object has two main settings objects EwbApiSetting and EwbApiLoginDetails, and some more public properties
        //Default constructor of EWBSession uses json config files to store EwbApiSeting and EwbApiLoginDetails
        //EWBSession constructor has 2 paramaters viz.LoadAPISettingsFromConfigFile and LoadAPILoginDetailsFromConfigFile with default value as True.
        //Thus paramaterless constructor below assums that ASP wants to use config file to store EwbApiSetting and EWBApiLoginDetails
        //in default case, the TaxProEWB.API library will take case of storing AuthToken and Expiary of authtoken internally in json config file.
        //Getting AuthToken if null or expired and storing in EwbApiLoginDetails is concealed calls handled by library.
        //In case ASP needs to change store for EwbApiSetting and EwbApiLoginDetails, pleae refer to "Handling Multiple Taxpayers" section of eWay Bill API Users Guide
        //Also, this demo project contains complete code showing how to create TaxPayer specific EWBSession object using TaxPayer Dependency Injection.
        public EWBSession EwbSession = new EWBSession();
        public EWBDemoForm()
        {
            InitializeComponent();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            cmbIRPURL.Items.Add("e-Waybill1 Portal");
            cmbIRPURL.Items.Add("e-Waybill2 Portal");
            if (EwbSession.EwbApiLoginDetails.IRPURL == 1)
                cmbIRPURL.SelectedIndex = 0;
            else
                cmbIRPURL.SelectedIndex = 1;


            cmbIRP.Items.Add("IRP-1");
            cmbIRP.Items.Add("IRP-2");
            if (EwbSession.EwbApiLoginDetails.IRP == 1)
                cmbIRP.SelectedIndex = 0;
            else
                cmbIRP.SelectedIndex = 1;
            loadApiSetting();
            loadApiLoginDetail();
            EwbSession.RefreshAuthTokenCompleted += RefreshLoginDetailsDisplay;
        }
        private void RefreshLoginDetailsDisplay(object sender, EventArgs e)
        {
            DisplayApiLoginDetails();
        }
        private void DisplayApiLoginDetails()
        {
            txtGSTIN.Text = EwbSession.EwbApiLoginDetails.EwbGstin;
            txtUserName.Text = EwbSession.EwbApiLoginDetails.EwbUserID;
            txtPassword.Text = EwbSession.EwbApiLoginDetails.EwbPassword;
            txtAppKey.Text = EwbSession.EwbApiLoginDetails.EwbAppKey;
            txtAuthToken.Text = EwbSession.EwbApiLoginDetails.EwbAuthToken;
            txtTokenExp.Text = EwbSession.EwbApiLoginDetails.EwbTokenExp?.ToString("dd/MM/yyyy HH:mm:ss");
            txtSEK.Text = EwbSession.EwbApiLoginDetails.EwbSEK;
        }
        public void loadApiSetting()
        {
            txtGspName.Text = EwbSession.EwbApiSetting.GSPName;
            txtAspUserId.Text = EwbSession.EwbApiSetting.AspUserId;
            txtAspPassword.Text = EwbSession.EwbApiSetting.AspPassword;
            txtClientId.Text = EwbSession.EwbApiSetting.EWBClientId;
            txtClientSecret.Text = EwbSession.EwbApiSetting.EWBClientSecret;
            txtEwbBaseUrl.Text = EwbSession.EwbApiSetting.BaseUrl;
        }
        public void loadApiLoginDetail()
        {
            if(EwbSession.EwbApiLoginDetails.IRP == 1)
                cmbIRP.SelectedIndex = 0;
            else
                cmbIRP.SelectedIndex = 1;

            if (EwbSession.EwbApiLoginDetails.IRPURL == 1)
                cmbIRPURL.SelectedIndex = 0;
            else
                cmbIRPURL.SelectedIndex = 1;

            txtUserName.Text = EwbSession.EwbApiLoginDetails.EwbUserID;
            txtPassword.Text = EwbSession.EwbApiLoginDetails.EwbPassword;
            txtGSTIN.Text = EwbSession.EwbApiLoginDetails.EwbGstin;
            txtAppKey.Text = EwbSession.EwbApiLoginDetails.EwbAppKey;
            txtAuthToken.Text = EwbSession.EwbApiLoginDetails.EwbAuthToken;
            txtSEK.Text = EwbSession.EwbApiLoginDetails.EwbSEK;
            txtTokenExp.Text = EwbSession.EwbApiLoginDetails.EwbTokenExp?.ToString("yyyy-MM-dd HH:mm:ss");
        }
        private void EWBDemoForm_Load(object sender, EventArgs e)
        {

        }

        private void btnSaveApiSetting_Click(object sender, EventArgs e)
        {
            EwbSession.EwbApiSetting.GSPName = txtGspName.Text;
            EwbSession.EwbApiSetting.AspUserId = txtAspUserId.Text;
            EwbSession.EwbApiSetting.AspPassword = txtAspPassword.Text;
            EwbSession.EwbApiSetting.EWBClientId = txtClientId.Text;
            EwbSession.EwbApiSetting.EWBClientSecret = txtClientSecret.Text;
            EwbSession.EwbApiSetting.EWBGSPUserID = txtGspUserId.Text;
            EwbSession.EwbApiSetting.BaseUrl = txtEwbBaseUrl.Text;
            // EwbSession.EwbApiSetting.AspUrl = txtAspUrl.Text;
            TaxProEWB.API.Shared.SaveAPISetting(EwbSession.EwbApiSetting);
            MessageBox.Show("Ewb ApiSetting Saved Successfully.");
        }

        private void btnSaveLoginDetail_Click(object sender, EventArgs e)
        {
            //Save EWB Login Detail
            if(cmbIRPURL.SelectedIndex == 0)
                 EwbSession.EwbApiLoginDetails.IRPURL = 1;
            else
                EwbSession.EwbApiLoginDetails.IRPURL = 2;

            if (cmbIRP.SelectedIndex == 0)
                EwbSession.EwbApiLoginDetails.IRP = 1;
            else
                EwbSession.EwbApiLoginDetails.IRP = 2;

            EwbSession.EwbApiLoginDetails.EwbGstin = txtGSTIN.Text;
            EwbSession.EwbApiLoginDetails.EwbUserID = txtUserName.Text;
            EwbSession.EwbApiLoginDetails.EwbPassword = txtPassword.Text;
            EwbSession.EwbApiLoginDetails.EwbAppKey = txtAppKey.Text;
            EwbSession.EwbApiLoginDetails.EwbAuthToken = EwbSession.EwbApiLoginDetails.EwbAuthToken;
            EwbSession.EwbApiLoginDetails.EwbTokenExp = EwbSession.EwbApiLoginDetails.EwbTokenExp;
            EwbSession.EwbApiLoginDetails.EwbSEK = EwbSession.EwbApiLoginDetails.EwbSEK;
            TaxProEWB.API.Shared.SaveAPILoginDetails(EwbSession.EwbApiLoginDetails);
            MessageBox.Show("Ewb Ewb ApiLoginDetails Saved Successfully.");
        }

        private async void btnEWBAuthToken_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Calling any API method will internally check for valid AuthToken and would try to obtain AuthToken if its is expired.  You don't need to explicitly call GetAuthTokenAsync method. Do you want to proceed?", "AuthToken is Automatic", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

                TxnRespWithObjAndInfo<EWBSession> TxnResp = await EWBAPI.GetAuthTokenAsync(EwbSession);

                //// Below is the code to call Api Synchroniously
                //TxnRespWithObjAndInfo<EWBSession> TxnResp = Task.Run(() => EWBAPI.GetAuthTokenAsync(EwbSession)).Result;

                if (TxnResp.IsSuccess)
                {
                    //Call Refresh Display Api Login Details to refresh auth token and Exp Time in display
                    DisplayApiLoginDetails();
                }
                txtResponce.Text = TxnResp.TxnOutcome;
                txtHeader.Text = "Auth Token Responce";
            }
        }

        private void button30_Click(object sender, EventArgs e)
        {
            int size = -1;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                txtHeader.Text = file;

                try
                {
                    txtResponce.Text = File.ReadAllText(file);
                    //size = text.Length;
                }
                catch (IOException)
                {
                }
            }
        }

        private async void btnGenEWB_Click(object sender, EventArgs e)
        {
            //ReqGenEwbPl ewbGen = new ReqGenEwbPl();

            ReqGenEwbPl ewbGen = new ReqGenEwbPl();

            ewbGen.supplyType = "O";
            ewbGen.subSupplyType = "1";
            ewbGen.subSupplyDesc = "";
            ewbGen.docType = "INV";
            ewbGen.docNo = "Doc017";
            ewbGen.docDate = "29/10/2024";
            ewbGen.fromGstin = "34AACCC1596Q002";//"07AACCC1596Q1Z4";
            ewbGen.fromTrdName = "welton";
            ewbGen.fromAddr1 = "2ND CROSS NO 59  19  A";
            ewbGen.fromAddr2 = "GROUND FLOOR OSBORNE ROAD";
            ewbGen.fromPlace = "FRAZER TOWN";
            ewbGen.fromPincode = 605001;//263652;/*110001;*/
            ewbGen.fromStateCode = 34;
            ewbGen.actFromStateCode = 34;
            ewbGen.toGstin = "05AAACG0904A1ZL";
            ewbGen.toTrdName = "sthuthya";
            ewbGen.toAddr1 = "Shree Nilaya";
            ewbGen.toAddr2 = "Dasarahosahalli";
            ewbGen.toPlace = "Beml Nagar";
            ewbGen.toPincode = 263652;/*110005;*/
            ewbGen.toStateCode = 05;
            ewbGen.actToStateCode = 05;
            ewbGen.transactionType = 1;
            ewbGen.dispatchFromGSTIN = ""; /*29AAAAA1303P1ZV*/
            ewbGen.dispatchFromTradeName = "ABC Traders";
            ewbGen.shipToGSTIN = "05AAACG0904A1ZL"; //29ALSPR1722R1Z3
            ewbGen.shipToTradeName = "XYZ Traders";
            ewbGen.otherValue = -100;
            ewbGen.totalValue = 0;
            ewbGen.cgstValue = 0;
            ewbGen.sgstValue = 0;
            ewbGen.igstValue = 0;
            ewbGen.cessValue = 0;
            ewbGen.cessNonAdvolValue = 400;
            ewbGen.transporterId = "12AACCC1596Q002";
            ewbGen.transporterName = "";
            ewbGen.transDocNo = "";
            ewbGen.totInvValue = 0;
            ewbGen.transMode = "";//1
            ewbGen.transDistance = "0"; /*1200*/
            ewbGen.transDocDate = "";
            ewbGen.vehicleNo = "";//PVC1234
            ewbGen.vehicleType = "R";//R
            ewbGen.itemList = new List<ReqGenEwbPl.ItemListInReqEWBpl>();
            //ReqGenEwbPl itemfromdatabase = null;///data from database of perticular id
            //foreach (ReqGenEwbPl.ItemListInReqEWBpl itm in itemfromdatabase.itemList)
            //{
            //	ewbGen.itemList.Add(new ReqGenEwbPl.ItemListInReqEWBpl() { productName = itm.productName, productDesc = itm.productDesc, cessNonAdvol = itm.cessNonAdvol, cessRate = itm.cessRate, cgstRate = itm.cgstRate, hsnCode = itm.hsnCode, igstRate = itm.igstRate, qtyUnit = itm.qtyUnit, quantity = itm.quantity, sgstRate = itm.sgstRate, taxableAmount = itm.taxableAmount});
            //	ewbGen.itemList.Add(new ReqGenEwbPl.ItemListInReqEWBpl() { productName = itm.productName, productDesc = itm.productDesc, cessNonAdvol = itm.cessNonAdvol, cessRate = itm.cessRate, cgstRate = itm.cgstRate, hsnCode = itm.hsnCode, igstRate = itm.igstRate, qtyUnit = itm.qtyUnit, quantity = itm.quantity, sgstRate = itm.sgstRate, taxableAmount = itm.taxableAmount });
            //}
            //ewbGen.itemList = EWBItems.ToList();

            //foreach (ReqGenEwbPl.ItemListInReqEWBpl item in EWBItems)
            //{

            //    item.productName = EWBItems.
            //}


            ewbGen.itemList.Add(new ReqGenEwbPl.ItemListInReqEWBpl
            {
                productName = "Wheat",
                productDesc = "Wheat",
                hsnCode = 1001,
                quantity = 4,
                qtyUnit = "BOX",
                cgstRate = 0,
                sgstRate = 0,
                igstRate = 0,
                cessRate = 0,
                cessNonAdvol = 0,
                taxableAmount = 0
            }
              );
            ewbGen.itemList.Add(new ReqGenEwbPl.ItemListInReqEWBpl
            {
                productName = "Steel",
                productDesc = "Steel",
                hsnCode = 7213,
                quantity = 4,
                qtyUnit = "BOX",
                cgstRate = 0,
                sgstRate = 0,
                igstRate = 0,
                cessRate = 0,
                cessNonAdvol = 0,
                taxableAmount = 0
            }
              );
            TxnRespWithObjAndInfo<RespGenEwbPl> TxnResp = await EWBAPI.GenEWBAsync(EwbSession, ewbGen);

            if (TxnResp.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
            {

                txtResponce.Text = TxnResp.TxnOutcome;
                //Check for error "The distance between the pincodes given is too high"



                if (TxnResp.TxnOutcome.Contains("702") && !string.IsNullOrEmpty(TxnResp.Info))
                {
                    TaxProEWB.API.RespInfoPl respInfoPl = new TaxProEWB.API.RespInfoPl();

                    respInfoPl = JsonSerializer.Deserialize<TaxProEWB.API.RespInfoPl>(TxnResp.Info);
                    //You can retrive respInfoPl attributes here
                    ewbGen.transDistance = respInfoPl.distance;
                    //Call GenEWB API again
                    TxnResp = await EWBAPI.GenEWBAsync(EwbSession, ewbGen);
                    if (TxnResp.IsSuccess)
                        txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
                    else
                        txtResponce.Text = TxnResp.TxnOutcome;
                }

            }


            txtHeader.Text = "Generate e-Way Bill Responce";
        }

        private async void btnUpdateVehicleNo_Click(object sender, EventArgs e)
        {
            ReqVehicleNoUpdtPl reqVehicleNo = new ReqVehicleNoUpdtPl();
            reqVehicleNo.ewbNo = 551008872558;
            reqVehicleNo.vehicleNo = "PVC9999"; /*PVC1239*/
            reqVehicleNo.fromPlace = "FRAZER TOWN";
            reqVehicleNo.fromState = 05;
            reqVehicleNo.reasonCode = 1;
            reqVehicleNo.reasonRem = "vehicle broke down";
            reqVehicleNo.transDocNo = "LR180321";//LR180321
            reqVehicleNo.transDocDate = "10/01/2023";
            reqVehicleNo.transMode = 2;
            reqVehicleNo.vehicleType = 'R';

            TxnRespWithObjAndInfo<RespVehicleNoUpdtPl> resVehicleNoUpdt = await EWBAPI.UpdateVehicleNosync(EwbSession, reqVehicleNo);

            if (resVehicleNoUpdt.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(resVehicleNoUpdt.RespObj);
            else
                txtResponce.Text = resVehicleNoUpdt.TxnOutcome;
            txtHeader.Text = "Vehicle No. update Responce";
        }

        private async void btnGenConsolidatedEWB_Click(object sender, EventArgs e)
        {
            ReqGenCEwbPl reqCEWB = new ReqGenCEwbPl();
            reqCEWB.fromPlace = "BANGALORE SOUTH";
            reqCEWB.fromState = 05;
            reqCEWB.vehicleNo = "PVC1234"; /*PVC1234*/
            reqCEWB.transMode = 1;/*1*/
            reqCEWB.TransDocNo = ""; /*1234*/
            reqCEWB.TransDocDate = "";/* 22 / 05 / 2018*/
            reqCEWB.tripSheetEwbBills = new List<ReqGenCEwbPl.TripSheetGenCEWB>() {
                new ReqGenCEwbPl.TripSheetGenCEWB { ewbNo = 551008872558 },
                new ReqGenCEwbPl.TripSheetGenCEWB { ewbNo = 521008872559 }};

            //reqCEWB.tripSheetEwbBills.Add(
            //    new ReqGenCEwbPl.TripSheetEwbBills { ewbNo = 321000978068 } );


            TxnRespWithObjAndInfo<RespGenCEwbPl> respGenCEWB = await EWBAPI.GenCEWBAsync(EwbSession, reqCEWB);
            if (respGenCEWB.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(respGenCEWB.RespObj);
            else
                txtResponce.Text = respGenCEWB.TxnOutcome;
            txtHeader.Text = "Generate Consolidated e-Way Bill Responce";
        }

        private async void btnCancelEWBByEWBLib_Click(object sender, EventArgs e)
        {
            ReqCancelEwbPl reqCancelEWB = new ReqCancelEwbPl();
            reqCancelEWB.ewbNo = 521008872559; /*311001074490*/
            reqCancelEWB.cancelRsnCode = 2;
            reqCancelEWB.cancelRmrk = "Cancelled the order";

            TxnRespWithObjAndInfo<RespCancelEwbPl> respCancelEWB = await EWBAPI.CancelEWBAsync(EwbSession, reqCancelEWB);
            if (respCancelEWB.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(respCancelEWB.RespObj);
            else
                txtResponce.Text = respCancelEWB.TxnOutcome;
            txtHeader.Text = "Cancel e-Way Bill Responce";
        }

        private async void btnRejectEWB_Click(object sender, EventArgs e)
        {
            ReqRejectEwbPl reqRejectEWB = new ReqRejectEwbPl();
            reqRejectEWB.ewbNo = 321001062894;
            TxnRespWithObjAndInfo<RespRejectEwbPl> respRejectEWB = await EWBAPI.RejectEWBAsync(EwbSession, reqRejectEWB);
            if (respRejectEWB.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(respRejectEWB.RespObj);
            else
                txtResponce.Text = respRejectEWB.TxnOutcome;
            txtHeader.Text = "Reject e-Way Bill Responce";
        }

        private async void btnUpdateTransporter_Click(object sender, EventArgs e)
        {
            ReqUpdtTransporterPl reqUpdtTrans = new ReqUpdtTransporterPl();
            reqUpdtTrans.ewbNo = 561008872577; /*391001062837*/
            reqUpdtTrans.transporterId = "29AKLPM8755F1Z2";
            TxnRespWithObjAndInfo<RespUpdtTransporterPl> respUpdtTrans = await EWBAPI.UpdtTransporterAsync(EwbSession, reqUpdtTrans);
            if (respUpdtTrans.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(respUpdtTrans.RespObj);
            else
                txtResponce.Text = respUpdtTrans.TxnOutcome;
            txtHeader.Text = "Update Transporter Responce";
        }

        private async void btnExtendValidity_Click(object sender, EventArgs e)
        {
            ReqExtendValidityEWBPl reqExtValidity = new ReqExtendValidityEWBPl();
            reqExtValidity.ewbNo = 521008917160;
            reqExtValidity.vehicleNo = "PVC1234";
            reqExtValidity.fromPlace = "FRAZER TOWN";
            reqExtValidity.fromState = 34;
            reqExtValidity.remainingDistance = 362;
            reqExtValidity.transDocNo = "DOCPal/123";
            reqExtValidity.transDocDate = "25/07/2024";
            reqExtValidity.extnRemarks = 1;
            reqExtValidity.extnRsnCode = 1;
            reqExtValidity.transMode = 1;
            reqExtValidity.fromPincode = 605001;
            reqExtValidity.consignmentStatus = "M";
            reqExtValidity.transitType = "";/*R*/
            reqExtValidity.addressLine1 = ""; /*Bengaluru*/
            reqExtValidity.addressLine2 = "";
            reqExtValidity.addressLine3 = "";
            ////TxnRespWithObjAndInfo this method is deprecated
            //TxnRespWithObjAndInfo<RespExtendValidityEWBPl> respExtndValidity = await EWBAPI.ExtendValidityOfEWBAsync(EwbSession, reqExtValidity);
            //Use new method
            TxnRespWithObjAndInfo<RespExtendValidityEWBPl> respExtndValidity = await EWBAPI.ExtendValidityOfEWBAsync(EwbSession, reqExtValidity);
            if (respExtndValidity.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(respExtndValidity.RespObj);
            else
            {
                txtResponce.Text = respExtndValidity.TxnOutcome;
                txtResponce.Text += Environment.NewLine + respExtndValidity.RespObj;
                //Check for error "The distance between the pincodes given is too high"
                if (respExtndValidity.TxnOutcome.Contains("702"))
                {
                    TaxProEWB.API.RespInfoPl respInfoPl = new TaxProEWB.API.RespInfoPl();
                    respInfoPl = JsonSerializer.Deserialize<TaxProEWB.API.RespInfoPl>(respExtndValidity.Info);
                    //You can retrive respInfoPl attributes here
                    reqExtValidity.remainingDistance = Convert.ToInt32(respInfoPl.distance);
                    //Call GenEWB API again
                    respExtndValidity = await EWBAPI.ExtendValidityOfEWBAsync(EwbSession, reqExtValidity);
                    if (respExtndValidity.IsSuccess)
                        txtResponce.Text = JsonSerializer.Serialize(respExtndValidity.RespObj);
                    else
                        txtResponce.Text = respExtndValidity.TxnOutcome;
                }

            }
            txtHeader.Text = "Update Transporter Responce";
        }

        private async void btnReGenCEWB_Click(object sender, EventArgs e)
        {
            ReqReGenerateCEWBPl reqReGenCEWB = new ReqReGenerateCEWBPl();
            reqReGenCEWB.tripSheetNo = 5010005595;
            reqReGenCEWB.vehicleNo = "PQR1234";
            reqReGenCEWB.fromPlace = "Bengaluru";
            reqReGenCEWB.fromState = 29;
            reqReGenCEWB.transDocNo = "1234";
            reqReGenCEWB.transDocDate = "26/04/2018";
            reqReGenCEWB.transMode = "1";
            reqReGenCEWB.reasonCode = 1;
            reqReGenCEWB.reasonRem = "Flood";


            TxnRespWithObjAndInfo<RespReGenerateCEWBPl> TxnObj = await EWBAPI.ReGenCEWBAsync(EwbSession, reqReGenCEWB);
            if (TxnObj.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(TxnObj.RespObj);
            else
                txtResponce.Text = TxnObj.TxnOutcome;
            txtHeader.Text = "Re-Generate Consolidated e-Way Bill";
        }

        private async void btnGetEWBDetail_Click(object sender, EventArgs e)
        {
            long EwbNo = 518008680455   ;

            TxnRespWithObjAndInfo<RespGetEWBDetail> TxnResp = await EWBAPI.GetEWBDetailAsync(EwbSession, EwbNo);
            //var a = JsonConvert.SerializeObject(TxnResp.RespObj);
            if (TxnResp.IsSuccess)

                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
            txtHeader.Text = "Get e-Way Bill Details Responce";
        }

        private async void btnByOthParty_Click(object sender, EventArgs e)
        {
            string Date = "30/03/2018";
            TxnRespWithObjAndInfo<List<EwayBillsofOtherParty>> TxnResp = await EWBAPI.GetEWBOfOtherPartyAsync(EwbSession, Date);
            if (TxnResp.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
            txtHeader.Text = "Get e-Way Bills of OtherParty";
        }

        private async void btnGetCEWB_Click(object sender, EventArgs e)
        {
            string tripSheetNo = "5010005595";
            TxnRespWithObjAndInfo<GetConsolidatedEWB> TxnResp = await EWBAPI.GetConsolidatedEWBAsync(EwbSession, tripSheetNo);
            if (TxnResp.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
            txtHeader.Text = "Get Consolidated e-Way Bill details";
        }

        private async void btnGetTransinDetail_Click(object sender, EventArgs e)
        {
            string trn_no = "01AMDPA2430P1ZK";
            TxnRespWithObjAndInfo<TransinDetail> TxnResp = await EWBAPI.GetTransinDetailAsync(EwbSession, trn_no);
            if (TxnResp.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
            txtHeader.Text = "Get HSN details";
        }

        private async void btnErrorList_Click(object sender, EventArgs e)
        {
            TxnRespWithObjAndInfo<List<RespError>> TxnResp = await EWBAPI.GetErrorListAsync(EwbSession);
            if (TxnResp.IsSuccess == true)
            {
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            }
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnHSNDetails_Click(object sender, EventArgs e)
        {
            string Hsncode = "1001";
            TxnRespWithObjAndInfo<HSNDetail> TxnResp = await EWBAPI.GetHSNDetailAsync(EwbSession, Hsncode);
            if (TxnResp.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
            txtHeader.Text = "Get HSN details";
        }

        private async void btnEwbDetGenByConsigner_Click(object sender, EventArgs e)
        {
            string strDocType = "INV";
            string strDocNo = "111-33";
            TxnRespWithObjAndInfo<RespEwbDetGenByConsigner> TxnResp = await EWBAPI.GetEwayBillGeneratedByConsignor(EwbSession, strDocType, strDocNo);
            if (TxnResp.IsSuccess == true)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnEWBForTransByState_Click(object sender, EventArgs e)
        {
            txtHeader.Text = "Get Eway Bills For Transporter By State";
            string strDate = "14/03/2019";/* "dd/MM/yyyy";*/
            int strStateCode = 05;
            TxnRespWithObjAndInfo<List<RespGetEwayBillsForTranByState>> TxnResp = await EWBAPI.GetEwayBillsForTransporterByState(EwbSession, strDate, strStateCode);
            if (TxnResp.IsSuccess == true)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnGetEwayBillsByDate_Click(object sender, EventArgs e)
        {
            string strDate = "28/03/2019";
            txtHeader.Text = "Get Eway Bills By Date API";
            TxnRespWithObjAndInfo<List<RespGetEwayBillsByDate>> TxnResp = await EWBAPI.GetEwayBillsByDateAsync(EwbSession, strDate);
            if (TxnResp.IsSuccess == true)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnGetEwayBillsRejectedByOthers_Click(object sender, EventArgs e)
        {
            string strDate = "27/03/2019";
            txtHeader.Text = "Get Eway Bills Rejected By Others API";
            TxnRespWithObjAndInfo<List<RespGetEwayBillsRejectedByOthers>> TxnResp = await EWBAPI.GetEwayBillsRejectedByOthersAsync(EwbSession, strDate);
            if (TxnResp.IsSuccess == true)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnGetEWBAssignForTrans_Click(object sender, EventArgs e)
        {
            string Date = "10/01/2023";
            TxnRespWithObjAndInfo<List<AssignedEWBItem>> TxnResp = await EWBAPI.GetEWBAssignedForTransAsync(EwbSession, Date);
            if (TxnResp.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
            txtHeader.Text = "Get e-Way Bill assigned to you for transportation";
        }

        private async void btnAsignedByGSTIN_Click(object sender, EventArgs e)
        {
            string genGSTIN = "05AAACG0904A1ZL";
            string Date = "16/05/2018";
            TxnRespWithObjAndInfo<List<AssignedEWBItem>> TxnResp = await EWBAPI.GetEWBAssignedForTransByGstinAsync(EwbSession, Date, genGSTIN);
            if (TxnResp.IsSuccess)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;

            txtHeader.Text = "Get e-Way Bill for transporter by gstin";
        }

        private async void btnMultiVehMovnt_Click(object sender, EventArgs e)
        {
            ReqIniMultiVehicleMov objIniMultiVehUpd = new ReqIniMultiVehicleMov();

            objIniMultiVehUpd.ewbNo = 391001460200;
            objIniMultiVehUpd.reasonCode = "1";
            objIniMultiVehUpd.reasonRem = "vehicle broke down";
            objIniMultiVehUpd.fromPlace = "BANGALORE";
            objIniMultiVehUpd.fromState = 29;
            objIniMultiVehUpd.toPlace = "Chennai";
            objIniMultiVehUpd.toState = 33;
            objIniMultiVehUpd.transMode = "1";
            objIniMultiVehUpd.totalQuantity = 33;
            objIniMultiVehUpd.unitCode = "BOX";


            TxnRespWithObjAndInfo<RespIniMultiVehicleMov> TxnResp = await EWBAPI.InitiateMultiVehMovntAsync(EwbSession, objIniMultiVehUpd);
            if (TxnResp.IsSuccess == true)
            {
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            }
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnMultiVehAdd_Click(object sender, EventArgs e)
        {
            txtHeader.Text = "Add Vehicles details for Multi Vehicle Movement";
            ReqMultiVehAdd objReqMulVehAdd = new ReqMultiVehAdd();
            objReqMulVehAdd.ewbNo = 391001460200;
            objReqMulVehAdd.groupNo = "1";
            objReqMulVehAdd.vehicleNo = "PQR1234";
            objReqMulVehAdd.transDocNo = "1234";
            objReqMulVehAdd.transDocDate = "12/10/2017";
            objReqMulVehAdd.quantity = 15;

            TxnRespWithObjAndInfo<RespMultiVehAdd> TxnResp = await EWBAPI.AddVehDetailAsync(EwbSession, objReqMulVehAdd);
            if (TxnResp.IsSuccess == true)
            {
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            }
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnMultiVehUpdt_Click(object sender, EventArgs e)
        {
            txtHeader.Text = "Change Vehicles details for Multi Vehicle Movement";
            ReqMultiVehUpdt objReqMulVehUpdt = new ReqMultiVehUpdt();
            objReqMulVehUpdt.ewbNo = 391001460200;
            objReqMulVehUpdt.groupNo = "1";
            objReqMulVehUpdt.oldvehicleNo = "PQR1234";
            objReqMulVehUpdt.newVehicleNo = "PQR1234";
            objReqMulVehUpdt.oldTranNo = "ABC123";
            objReqMulVehUpdt.newTranNo = "PQR123";
            objReqMulVehUpdt.fromPlace = "Lucknow";
            objReqMulVehUpdt.fromState = 09;
            objReqMulVehUpdt.reasonCode = "1";
            objReqMulVehUpdt.reasonRem = "vehicle broke down";

            TxnRespWithObjAndInfo<RespMultiVehUpdt> TxnResp = await EWBAPI.UpdtMultiVehAsync(EwbSession, objReqMulVehUpdt);
            if (TxnResp.IsSuccess == true)
            {
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            }
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnGetApiBal_Click(object sender, EventArgs e)
        {
            TxnRespWithObjAndInfo<EwbApiBalance> TxnResp = await EWBAPI.GetApiBalance(EwbSession);
            if (TxnResp.IsSuccess == true)
                txtResponce.Text = JsonSerializer.Serialize(TxnResp.RespObj);
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnPrintEwb_Click(object sender, EventArgs e)
        {
            long EwbNo = 561008872577;

            TxnRespWithObjAndInfo<RespGetEWBDetail> TxnResp = await EWBAPI.GetEWBDetailAsync(EwbSession, EwbNo);
            string strJson = JsonSerializer.Serialize(TxnResp.RespObj);
            ReqPrintEWB reqPrintEWB = new ReqPrintEWB();
            reqPrintEWB = JsonSerializer.Deserialize<ReqPrintEWB>(strJson);
            reqPrintEWB.Irn = "c023d83fbb25ef50648297d534fe95a6100621574bf8c7bcb6d127def717bb88";
            if (TxnResp.IsSuccess == true)
            {
                //string pdfFolderPath = @"MyeWaybillDoc\";


                EWBAPI.PrintEWB(EwbSession, reqPrintEWB, "", true, false);
                //Return Byte Array
                //byte[] pdfFileResp  = EWBAPI.PrintEWB(EwbSession, TxnResp.RespObj, false);

            }
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private async void btnPrintCEWB_Click(object sender, EventArgs e)
        {
            string tripSheetNo = "5010005595";
            TxnRespWithObjAndInfo<GetConsolidatedEWB> txnresp = await EWBAPI.GetConsolidatedEWBAsync(EwbSession, tripSheetNo);
            if (txnresp.IsSuccess == true)
            {
                EWBAPI.PrintCEWB(EwbSession, txnresp.RespObj, "");
            }
        }

        private async void button40_Click(object sender, EventArgs e)
        {
            HttpRequestMessage httpRequestMessage;
            HttpResponseMessage httpResponseMessage;
            string strUrl = "";
            byte[] pdfFileResp;

            //long EwbNo = 561008872577;

            //TxnRespWithObjAndInfo<RespGetEWBDetail> TxnResp = await EWBAPI.GetEWBDetailAsync(EwbSession, EwbNo);

            string ReqJson = File.ReadAllText(@"C:\Users\CISPL\Desktop\Print.txt");


            string strOutcome = "Errors in Printing E-Way Bill.";      //Default values would indicate error
                                                                       //RespGetEWBDetail GeneratedEwb = null;
            strUrl = EWBSession.GspApiBaseUrl + "/printewb";                                                           //APITxnLogArgs txnLogArgs;
            httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, strUrl);
            httpRequestMessage.Headers.Add("Gstin", EwbSession.EwbApiLoginDetails.EwbGstin);
            httpRequestMessage.Headers.Add("aspid", EwbSession.EwbApiSetting.AspUserId);
            httpRequestMessage.Headers.Add("password", EwbSession.EwbApiSetting.AspPassword);
            //httpRequestMessage.Headers.Add("Content-Type", "application/json; charset=utf-8");

            //httpRequestMessage.Content = new StringContent(JsonSerializer.Serialize(TxnResp.RespObj, new JsonSerializerOptions { WriteIndented = true }), Encoding.UTF8, AppJsonMediaType);
            httpRequestMessage.Content = new StringContent(ReqJson);


            httpResponseMessage = await client.SendAsync(httpRequestMessage);
            pdfFileResp = await httpResponseMessage.Content.ReadAsByteArrayAsync();
            try
            {
                //This methods checks and creates directory if does not exists
                var pdfFolderPath = Path.GetFullPath(@"PrintEWB\");

                string pdfPath = pdfFolderPath + "EwbNo" + ".pdf";

                File.WriteAllBytes(pdfPath, pdfFileResp);
                var p = new Process();
                p.StartInfo = new ProcessStartInfo(pdfPath)
                {
                    UseShellExecute = true
                };
                p.Start();
                strOutcome = "EWB .pdf generated: " + pdfPath;

            }
            catch (Exception ex)
            {
                strOutcome = ex.Message;
            }
        }

        private async void btnDetailedPrint_Click(object sender, EventArgs e)
        {
            long EwbNo = 521008840697;

            TxnRespWithObjAndInfo<RespGetEWBDetail> TxnResp = await EWBAPI.GetEWBDetailAsync(EwbSession, EwbNo);

            var strJson = JsonSerializer.Serialize(TxnResp.RespObj);
            ReqPrintEWB reqPrintEWB = new ReqPrintEWB();
            reqPrintEWB = JsonSerializer.Deserialize<ReqPrintEWB>(strJson);
            reqPrintEWB.Irn = "c023d83fbb25ef50648297d534fe95a6100621574bf8c7bcb6d127def717bb88";

            if (TxnResp.IsSuccess == true)
            {
                //string pdfFolderPath = (@"C:\Users\Pallavi\Desktop");

                EWBAPI.PrintEWB(EwbSession, reqPrintEWB, "", true, true);

            }
            else
                txtResponce.Text = TxnResp.TxnOutcome;
        }

        private void btnPrintByDec_Click(object sender, EventArgs e)
        {

        }

        private void cmbIRPURL_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIRPURL.SelectedIndex == 0) {
                EwbSession.EwbApiLoginDetails.IRPURL = 1;
            }
            else
                EwbSession.EwbApiLoginDetails.IRPURL = 2;
        }

        private void cmbIRP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIRP.SelectedIndex == 0)
            {
                EwbSession.EwbApiLoginDetails.IRP = 1;
            }
            else
                EwbSession.EwbApiLoginDetails.IRP = 2;
        }
    }
}
