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

namespace TaxProGSTApiWinFormsDemo
{
    public partial class frmLargeFileDownloadAPI : Form
    {
        GSTSession GstSession = new GSTSession();
        private FileDetailResponse FileDetailResponse = null;
        public frmLargeFileDownloadAPI()
        {
            InitializeComponent();
        }
        private void frmLargeFileDownloadAPI_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 btnGetAllFileDetails_Click(object sender, EventArgs e)
        {
            try
            {
                FileDetailResponse = null;
                txtAPIResponse.Text = "Please wait...";
                TxnRespWithObj<FileDetailResponse> txnRespFileDetails = await SharedAPI.GetAllFileDetailsFromToken(GstSession, txtGSTIN.Text, txtReturnPeriod.Text, txtTokenNo.Text, true);
                if(txnRespFileDetails.IsSuccess)
                {
                    FileDetailResponse = txnRespFileDetails.RespObj;
                    txtAPIResponse.Text = JsonConvert.SerializeObject(txnRespFileDetails.RespObj, Formatting.Indented);
                }
                else
                {
                    txtAPIResponse.Text = txnRespFileDetails.TxnOutcome;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void btnDownloadAllFiles_Click(object sender, EventArgs e)
        {
            try
            {

                if(FileDetailResponse != null)
                {
                    bool IsCompleted = false;
                    foreach(FileDetailResponse.FileDetail fileDtl in FileDetailResponse.urls)
                    {
                        TxnRespWithObj<byte[]> txnRespFile = await AspAPI.DownloadGstrLargeFile(GstSession, fileDtl, true);
                        if(txnRespFile.IsSuccess)
                        {
                            string strUrl = fileDtl.ul;
                            string ExtractZipFileName = (strUrl.Split('/')[5]).Split('?')[0];
                            if(!Directory.Exists("LargeFile"))
                                Directory.CreateDirectory("LargeFile");
                            if(!Directory.Exists(@"LargeFile\" + txtGSTIN.Text))
                                Directory.CreateDirectory(@"LargeFile\" + txtGSTIN.Text);
                            File.WriteAllBytes(@"LargeFile\" + txtGSTIN.Text + @"\" + ExtractZipFileName, txnRespFile.RespObj);
                            await Task.Delay(2000);
                            IsCompleted = true;
                        }
                        else
                        {
                            IsCompleted = false;
                            txtAPIResponse.Text = txnRespFile.TxnOutcome;
                            break;
                        }

                    }
                    if(IsCompleted)
                        txtAPIResponse.Text = "File Downloaded Successfully.";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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