using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComPDFKit_Conversion.Converter;
using System.Diagnostics;
using System.IO;
using Microsoft.AppCenter.Crashes.Ingestion.Models;
using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
using ComPDFKit_Conversion.Options;
using PDF_Office.CustomControl;
using Exception = System.Exception;

namespace PDF_Office.Helper
{
    public static class ConverterHelper
    {
        public static CPDFConverterWord wordConverter = null;
        public static CPDFConverterExcel excelConverter = null;
        public static CPDFConverterPPT pptConverter = null;
        public static CPDFConverterTxt txtConverter = null;
        public static CPDFConverterCsv csvConverter = null;
        public static CPDFConverterImg imgConverter = null;
        public static async Task<bool> WordConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword)
        {
            bool result = false;
            try
            {
                int[] pageArray = pageIndexLists.ToArray();
                wordConverter = new CPDFConverterWord(inputpath, pawssword);
                string outputFolder = outputpath;
                string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
                ConvertError error = ConvertError.ERR_UNKNOWN;
                string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".docx");
                result = await Task.Run(() => wordConverter.Convert(outputFolder, ref outputFileName, pageArray, ref error, getProgress));
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public static async Task<bool> PPTConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword)
        {
            bool result = false;
            try
            {
                int[] pageArray = pageIndexLists.ToArray();

                pptConverter = new CPDFConverterPPT(inputpath, pawssword);
                string outputFolder = outputpath;
                string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
                ConvertError error = ConvertError.ERR_UNKNOWN;

                string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".ppt");

                result = await Task.Run(() => pptConverter.Convert(outputFolder, ref outputFileName, pageArray, ref error, getProgress));
                if (result)
                {
                    if (File.Exists(outputFileName))
                        Process.Start(outputFileName);
                }
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public static async Task<bool> ExcelConvert(string inputpath, string outputpath,OnProgress getProgress , List<int> pageIndexLists, string pawssword,CPDFConvertExcelOptions ExcelOption)
        {
            bool result = false;
            try
            {
                excelConverter = new CPDFConverterExcel(inputpath,pawssword);
                int[] pageArray = pageIndexLists.ToArray();

                string outputFolder = outputpath;
                string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
                ConvertError error = ConvertError.ERR_UNKNOWN;
                 result = await Task.Run(() => excelConverter.Convert(outputFolder, ref outputFileName, ExcelOption, pageArray, ref error, getProgress));
                if (result)
                {
                    if (File.Exists(outputFileName))
                        Process.Start(outputFileName);
                    else if (outputFileName == "NoTable")
                        MessageBoxEx.Show("No Table");
                }
               
                   
            }
            catch (Exception ex)
           {

               }
            return result;
        }

        //public static async Task<bool> TableConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword)
        //{
        //    bool result = false;
        //    try
        //    {
        //        int[] pageArray = pageIndexLists.ToArray();
        //        tableConverter = new CPDFConverterTable(inputpath, pawssword);
        //        string outputFolder = outputpath;
        //        string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
        //        ConvertError error = ConvertError.ERR_UNKNOWN;
        //        string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".xlsx");
        //        result = await Task.Run(() => tableConverter.Convert(outputFolder, ref outputFileName, pageArray, ref error, getProgress));

        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //    return result;
        //}

        public static async Task<bool> CSVConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword)
        {
            bool result = false;
            try
            {
                int[] pageArray = pageIndexLists.ToArray();

                csvConverter = new CPDFConverterCsv(inputpath, pawssword);
                string outputFolder = outputpath;
                string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
                ConvertError error = ConvertError.ERR_UNKNOWN;
                string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".csv");
                result = await Task.Run(() => csvConverter.Convert(outputFolder, ref outputFileName, pageArray, ref error, getProgress));

            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public static async Task<bool> TxtConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword)
        {
            bool result = false;
            try
            {
                int[] pageArray = pageIndexLists.ToArray();

                txtConverter = new CPDFConverterTxt(inputpath, pawssword);
                string outputFolder = outputpath;
                string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
                ConvertError error = ConvertError.ERR_UNKNOWN;
                string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".txt");
                result = await Task.Run(() => txtConverter.Convert(outputFolder, ref outputFileName, pageArray, ref error, getProgress));

            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public static async Task<bool> ImgConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists,string pawssword, ImgType type)
        {
            bool result = false;
            try
            {
                int[] pageArray = pageIndexLists.ToArray();

                imgConverter = new CPDFConverterImg(inputpath, pawssword);
                string outputFolder = outputpath;
                string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
                ConvertError error = ConvertError.ERR_UNKNOWN;
                result = await Task.Run(() => imgConverter.Convert(outputFolder, ref outputFileName, pageArray, type, ref error, getProgress));
                if (result)
                {
                    if (File.Exists(outputFileName))
                        Process.Start(outputFileName);
                }
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public static void Clear()
        {
            if (wordConverter != null)
            {
                wordConverter.Cancel();
            }
            if (excelConverter != null)
            {
                excelConverter.Cancel();
            }
            if (pptConverter != null)
            {
                pptConverter.Cancel();
            }
            if (txtConverter != null)
            {
                txtConverter.Cancel();
            }
            //if (tableConverter != null)
            //{
            //    tableConverter.Cancel();
            //}
            if (csvConverter != null)
            {
                csvConverter.Cancel();
            }
            Console.WriteLine("killed");
        }
        private static void StartFile(string path)
        {
            //Process.Start(path);
        }
        public static string GetFileNameAddSuffix(string path, string filename, string suffix)
        {
            int i = 1;
            string outname = filename;
            while (File.Exists(path + @"\" + outname + suffix))
            {
                outname = filename + $"({i.ToString()})";
                i++;
            }
            return outname;
        }

    }
}
