using PDF_Office.Model.PDFTool;
using PDF_Office.Properties;
using PDFSettings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PDF_Office.Helper
{
    /// <summary>
    /// 预设需求：远程控制对PDF工具进行排序
    /// </summary>
    public enum PDFToolType
    {
        /// <summary>
        /// 普通工具
        /// </summary>
        Normal,
        /// <summary>
        /// 常用工具
        /// </summary>
        Common,
        /// <summary>
        ///付费工具
        /// </summary>
        Payment,
        /// <summary>
        /// 优势工具
        /// </summary>
        Advantage,
        /// <summary>
        /// 无需打开文档
        /// </summary>
        NoNeedToOpenDoc

    }
    public enum PDFFnType
    {
        Split,
        Extract,
        Insert,
        Compress,
        Merge,
        Print,
        Security,
        ConvertPDF,
        PDFToWord,
        PDFToExcel,
        PDFToPPT,
        ImageToPDF,
        OCR,
        WaterMark,
        HeaderFooter,
        BatesNumbers,
        Batch,
        Background,
        CompareDoc,
        Redact,
        PageEdit,
        BatchRemove,
        FormFiledRecognition,
        PDFToImage,
    }

    /// <summary>
    /// PDF工具初始化类型
    /// </summary>
    internal class PDFToolsInit
    {
        public ToolItem toolItem { get; private set; }
        public PDFToolsInit()
        {
            toolItem = new ToolItem();
        }

        /// <summary>
        /// 图标
        /// </summary>
        public void SetImagePath(string path)
        {
            toolItem.FnImg = path;
        }

        /// <summary>
        /// 初始化UI显示内容
        /// </summary>
        public void SetContent(string fnName,string fnInfo)
        {
            toolItem.FnName = fnName;
            toolItem.FnInfo = fnInfo;
        }

        /// <summary>
        /// 初始化功能
        /// </summary>
        public void SetFnType(PDFFnType fnType)
        {
            toolItem.FnType = fnType;
        }


    }
    public class PDFToolsHelper
    {
        public List<ToolItem> AllTools = null;
        public List<ToolItem> QuickTools = null;
        public List<ToolItem> MoreTools = null;
        private static PDFToolsHelper pDFToolsHelper;

        public const int QuickToolCount = 8;//快捷工具显示的个数
        public static PDFToolsHelper GetInstance()
        {
            if (pDFToolsHelper == null)
                pDFToolsHelper = new PDFToolsHelper();

            return pDFToolsHelper;
        }
        private PDFToolsHelper()
        {
            InitPDFTools();
        }

        private void InitPDFTools()
        {
            AllTools = new List<ToolItem>();
            QuickTools = new List<ToolItem>();
            MoreTools = new List<ToolItem>();
            //测试代码
            //Settings.Default.AllPDFToolsList.Clear();
            //Settings.Default.QuickPDFToolsList.Clear();
            //Settings.Default.Save();
            InitAllTools();
            InitQuickTools();
            InitMoreTools();

            //  ContianNewTools();
        }

        //用来测试的
        public void ClearData()
        {
            Settings.Default.AllPDFToolsList.Clear();
            Settings.Default.QuickPDFToolsList.Clear();
            Settings.Default.Save();

            AllTools.Clear();
            QuickTools.Clear();
            MoreTools.Clear();

            InitAllTools();
            InitQuickTools();
            InitMoreTools();
        }

        private void InitAllTools()
        {
            //批量处理
            var batch = new PDFToolsInit();
            batch.SetFnType(PDFFnType.Batch);
            batch.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batch.png");
            batch.SetContent("批量处理", "Batch convert, compress, secure, watermark PDFs.");
            //OCR
            var oCR = new PDFToolsInit();
            oCR.SetFnType(PDFFnType.OCR);
            oCR.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/ocr.png");
            oCR.SetContent("OCR", "Batch convert, compress, secure, watermark PDFs.");
            //转档PDF
            var convertPDF = new PDFToolsInit();
            convertPDF.SetFnType(PDFFnType.ConvertPDF);
            convertPDF.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/convert.png");
            convertPDF.SetContent("转档PDF", "Batch convert, compress, secure, watermark PDFs.");
            //图片转PDF
            var imageToPDF = new PDFToolsInit();
            imageToPDF.SetFnType(PDFFnType.ImageToPDF);
            imageToPDF.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/image.png");
            imageToPDF.SetContent("图片转PDF", "Batch convert, compress, secure, watermark PDFs.");
            //合并
            var merge = new PDFToolsInit();
            merge.SetFnType(PDFFnType.Merge);
            merge.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/merge.png");
            merge.SetContent("合并", "Batch convert, compress, secure, watermark PDFs.");
            //压缩
            var compress = new PDFToolsInit();
            compress.SetFnType(PDFFnType.Compress);
            compress.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/compress.png");
            compress.SetContent("压缩", "Batch convert, compress, secure, watermark PDFs.");
            //安全
            var security = new PDFToolsInit();
            security.SetFnType(PDFFnType.Security);
            security.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/security.png");
            security.SetContent("安全", "Batch convert, compress, secure, watermark PDFs.");
            //文件对比
            var compareDoc = new PDFToolsInit();
            compareDoc.SetFnType(PDFFnType.CompareDoc);
            compareDoc.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/compress.png");
            compareDoc.SetContent("文件对比", "Batch convert, compress, secure, watermark PDFs.");
            //PDF转PPT
            var pDFToPPT = new PDFToolsInit();
            pDFToPPT.SetFnType(PDFFnType.PDFToPPT);
            pDFToPPT.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-ppt.png");
            pDFToPPT.SetContent("PDF转PPT", "Batch convert, compress, secure, watermark PDFs.");
            //PDF转Excel
            var pDFToExcel = new PDFToolsInit();
            pDFToExcel.SetFnType(PDFFnType.PDFToExcel);
            pDFToExcel.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-excel.png");
            pDFToExcel.SetContent("PDF转Excel", "Batch convert, compress, secure, watermark PDFs.");
            //PDF转Word
            var pDFToWord = new PDFToolsInit();
            pDFToWord.SetFnType(PDFFnType.PDFToWord);
            pDFToWord.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-word.png");
            pDFToWord.SetContent("PDF转Word", "Batch convert, compress, secure, watermark PDFs.");
            //PDF转图片
            var pDFToImage = new PDFToolsInit();
            pDFToImage.SetFnType(PDFFnType.PDFToImage);
            pDFToImage.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-image.png");
            pDFToImage.SetContent("PDF转图片", "Batch convert, compress, secure, watermark PDFs.");
            //水印
            var waterMark = new PDFToolsInit();
            waterMark.SetFnType(PDFFnType.WaterMark);
            waterMark.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/watermark.png");
            waterMark.SetContent("水印", "Batch convert, compress, secure, watermark PDFs.");
            //背景
            var background = new PDFToolsInit();
            background.SetFnType(PDFFnType.Background);
            background.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/background.png");
            background.SetContent("背景", "Batch convert, compress, secure, watermark PDFs.");
            //页眉页脚
            var headerFooter = new PDFToolsInit();
            headerFooter.SetFnType(PDFFnType.HeaderFooter);
            headerFooter.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/header&footer.png");
            headerFooter.SetContent("页眉页脚", "Batch convert, compress, secure, watermark PDFs.");
            //贝茨Bates码
            var batesNumbers = new PDFToolsInit();
            batesNumbers.SetFnType(PDFFnType.BatesNumbers);
            batesNumbers.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batesnumbers.png");
            batesNumbers.SetContent("贝茨Bates码", "Batch convert, compress, secure, watermark PDFs.");
            //批量移除
            var batchRemove = new PDFToolsInit();
            batchRemove.SetFnType(PDFFnType.BatchRemove);
            batchRemove.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batch-remove.png");
            batchRemove.SetContent("批量移除", "Batch convert, compress, secure, watermark PDFs.");
            //编辑
            var redact = new PDFToolsInit();
            redact.SetFnType(PDFFnType.Redact);
            redact.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/reduct.png");
            redact.SetContent("编辑", "Batch convert, compress, secure, watermark PDFs.");
            //表单存档识别
            var formFiledRecognition = new PDFToolsInit();
            formFiledRecognition.SetFnType(PDFFnType.FormFiledRecognition);
            formFiledRecognition.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/form-filed-recognition.png");
            formFiledRecognition.SetContent("表单存档识别", "Batch convert, compress, secure, watermark PDFs.");
            //打印
            var print = new PDFToolsInit();
            print.SetFnType(PDFFnType.Print);
            print.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/print.png");
            print.SetContent("打印", "Batch convert, compress, secure, watermark PDFs.");
            //页面编辑
            var pageEdit = new PDFToolsInit();
            pageEdit.SetFnType(PDFFnType.PageEdit);
            pageEdit.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/page-edit.png");
            pageEdit.SetContent("页面编辑", "Batch convert, compress, secure, watermark PDFs.");
            //插入
            var insert = new PDFToolsInit();
            insert.SetFnType(PDFFnType.Insert);
            insert.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/insert.png");
            insert.SetContent("插入", "Batch convert, compress, secure, watermark PDFs.");
            //拆分
            var split = new PDFToolsInit();
            split.SetFnType(PDFFnType.Split);
            split.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/split.png");
            split.SetContent("拆分", "Batch convert, compress, secure, watermark PDFs.");
            //提取
            var extract = new PDFToolsInit();
            extract.SetFnType(PDFFnType.Extract);
            extract.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/extract.png");
            extract.SetContent("提取", "Batch convert, compress, secure, watermark PDFs.");



            AllTools.Add(split.toolItem);
            AllTools.Add(extract.toolItem);
            AllTools.Add(insert.toolItem);
            AllTools.Add(compress.toolItem);
            AllTools.Add(merge.toolItem);
            AllTools.Add(print.toolItem);
            AllTools.Add(security.toolItem);
            AllTools.Add(convertPDF.toolItem);
            AllTools.Add(pDFToWord.toolItem);
            AllTools.Add(pDFToExcel.toolItem);
            AllTools.Add(pDFToPPT.toolItem);
            AllTools.Add(imageToPDF.toolItem);
            AllTools.Add(oCR.toolItem);
            AllTools.Add(waterMark.toolItem);
            AllTools.Add(headerFooter.toolItem);
            AllTools.Add(batesNumbers.toolItem);
            AllTools.Add(batch.toolItem);
            AllTools.Add(background.toolItem);
            AllTools.Add(compareDoc.toolItem);
            AllTools.Add(redact.toolItem);
            AllTools.Add(pageEdit.toolItem);
            AllTools.Add(batchRemove.toolItem);
            AllTools.Add(formFiledRecognition.toolItem);
            AllTools.Add(pDFToImage.toolItem);
        }

        /// <summary>
        /// 检查是否为新增工具
        /// </summary>
        private void ContianNewTools()
        {
            var cacheList = Settings.Default.AllPDFToolsList;
            foreach (var item in AllTools)
            {
                ToolItem toolItem = null;
                foreach (var cacheItem in cacheList)
                {
                    var strFnTye = Enum.GetName(typeof(PDFFnType), item.FnType);
                    if (cacheItem.FnTypeStr != strFnTye && cacheItem.IsNewTool == true)
                    {
                        toolItem = item;
                        break;
                    }
                }

                if(toolItem != null && toolItem.IsNewTool == true)
                {
                    CacheToolItem cacheTool = new CacheToolItem();
                    cacheTool.IsNewTool = true;
                    cacheTool.ToolTypeId = toolItem.ToolTypeId;
                    cacheTool.ToolTypeStr = Enum.GetName(typeof(PDFToolType), toolItem.ToolType);
                    cacheTool.FnTypeStr = Enum.GetName(typeof(PDFFnType), toolItem.FnType);
                    Settings.Default.AllPDFToolsList.Add(cacheTool);
                }
               
            }
            Settings.Default.Save();

        }

        /// <summary>
        /// 快捷工具
        /// </summary>
        private void InitQuickTools()
        {

            var cacheList = Settings.Default.AllPDFToolsList;

            if (cacheList == null)
                cacheList = new AllPDFToolsList();

            if(cacheList.Count > 0)
            {
                int currentCount = 0;
                foreach (var cacheItem in cacheList)
                {
                    if (cacheItem.ToolLayOutType == 1 && currentCount <= QuickToolCount)
                    {
                        foreach (var allItem in AllTools)
                        {
                            if (cacheItem.FnTypeStr == Enum.GetName(typeof(PDFFnType), allItem.FnType))
                            {
                                allItem.IsQuickTool = true;

                                var index = QuickTools.FindIndex(t => t.ToolTypeId > allItem.ToolTypeId);
                                if (index == -1)
                                {
                                    allItem.ToolTypeId = cacheItem.ToolTypeId;
                                    QuickTools.Add(allItem);
                                }
                                else
                                {
                                    allItem.ToolTypeId = index;
                                    QuickTools.Insert(index, allItem);
                                }

                                currentCount++;
                                break;
                            }
                        }
                    }
                }

            }
            else
            {
                //当所有工具大于等于快捷工具显示的个数时
                if (AllTools.Count >= QuickToolCount)
                {
                    for (int i = 0; i < QuickToolCount; i++)
                    {
                        AllTools[i].ToolTypeId = i;
                        AllTools[i].IsQuickTool = true;
                        QuickTools.Add(AllTools[i]);
                    }
                }
                else
                {
                    for (int i = 0; i < AllTools.Count; i++)
                    {
                        AllTools[i].ToolTypeId = i;
                        AllTools[i].IsQuickTool = true;
                        QuickTools.Add(AllTools[i]);
                    }
                }
                SaveAllTools();

            }



        }

        private void SaveAllTools()
        {
            var cacheList = Settings.Default.AllPDFToolsList;

            foreach (var item in AllTools)
            {
                CacheToolItem cacheItem = new CacheToolItem();
                cacheItem.ToolLayOutType = (item.IsQuickTool == true ? 1 : 0);
                cacheItem.IsNewTool = item.IsNewTool;
                cacheItem.ToolTypeId = item.ToolTypeId;
                cacheItem.ToolTypeStr = Enum.GetName(typeof(PDFToolType), item.ToolType);
                cacheItem.FnTypeStr = Enum.GetName(typeof(PDFFnType), item.FnType);
                cacheList.Add(cacheItem);
            }
            Settings.Default.Save();
        }


        private void InitMoreTools()
        {
            int currentCount = 0;
            foreach (var allItem in AllTools)
            {
                if (allItem.IsQuickTool == false)
                {
                    allItem.ToolTypeId = currentCount;
                    MoreTools.Add(allItem);
                    currentCount++;
                }
            }
        }

        public void SaveCacheList()
        {
            var cacheLists = Settings.Default.AllPDFToolsList;
            CacheToolItem cacheItem = null;
            foreach (var item in AllTools)
            {
                cacheItem = cacheLists.Find(t => t.FnTypeStr == Enum.GetName(typeof(PDFFnType), item.FnType));
                if (cacheItem != null)
                {
                    cacheItem.ToolTypeId = item.ToolTypeId;
                    cacheItem.ToolLayOutType = (item.IsQuickTool == true ? 1 : 0);
                }
            }

            Settings.Default.Save();
        }
    }


}
