using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows;
using PDF_Office.Model.EditTools.Background;
using PDF_Office.Model.EditTools.Watermark;
using ComPDFKit.PDFDocument;
using System.Windows.Controls;
using ComPDFKit.PDFPage;
using static Dropbox.Api.TeamLog.PaperDownloadFormat;
using Microsoft.Office.Interop.Word;
using Task = System.Threading.Tasks.Task;
using System.Drawing.Imaging;
using System.Windows.Media;

namespace PDF_Office.Helper
{
    public static class EditToolsHelper
    {

        public static byte[] ConvertColor(Color color)
        {
            byte[] rgb_array = new byte[] { 0, 0, 0 };
            rgb_array[0] = color.R;
            rgb_array[1] = color.G;
            rgb_array[2] = color.B;

            return rgb_array;
        }

        public static Color ConvertColor(byte[] rgb_array)
        {
            Color color = new Color();
            color.R = rgb_array[0];
            color.G = rgb_array[1];
            color.B = rgb_array[2];

            return color;
        }

        public static void ChooseFile(string filePath, ref BackgroundInfo backgroundInfo)
        {
            using (FileStream fileData = File.OpenRead(filePath))
            {
                string fileExt = Path.GetExtension(filePath).ToLower();
                BitmapFrame frame = null;
                BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
                if (decoder != null && decoder.Frames.Count > 0)
                {
                    frame = decoder.Frames[0];
                }
                if (frame != null)
                {
                    backgroundInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
                    backgroundInfo.ImageWidth = frame.PixelWidth;
                    backgroundInfo.ImageHeight = frame.PixelHeight;
                    frame.CopyPixels(backgroundInfo.ImageArray, frame.PixelWidth * 4, 0);
#if DEBUG
                    Trace.WriteLine("width :" + backgroundInfo.ImageWidth);
                    Trace.WriteLine("height :" + backgroundInfo.ImageHeight);
#endif
                }
            }
        }

        public static void ChooseFile(string filePath, ref WatermarkInfo watermarkInfo)
        {
            using (FileStream fileData = File.OpenRead(filePath))
            {
                string fileExt = Path.GetExtension(filePath).ToLower();
                BitmapFrame frame = null;
                BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
                if (decoder != null && decoder.Frames.Count > 0)
                {
                    frame = decoder.Frames[0];
                }
                if (frame != null)
                {
                    watermarkInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
                    watermarkInfo.ImageWidth = frame.PixelWidth;
                    watermarkInfo.ImageHeight = frame.PixelHeight;
                    frame.CopyPixels(watermarkInfo.ImageArray, frame.PixelWidth * 4, 0);
#if DEBUG
                    Trace.WriteLine("width :" + watermarkInfo.ImageWidth);
                    Trace.WriteLine("height :" + watermarkInfo.ImageHeight);
#endif
                }
            }
        }


        public static string ParseRange(string str)
        {
            try
            {
                string strTemp = "";
                string strParse = "";
                for (int i = 0; i < str.Length; i++)
                {
                    if (Int32.TryParse(str.Substring(i, 1), out _))
                    {
                        strTemp += str[i];
                    }
                    else
                    {
                        strParse += int.Parse(strTemp) - 1;
                        strTemp = "";
                        strParse += str.Substring(i, 1);
                    }
                }
                strParse += int.Parse(strTemp) - 1;
                return strParse;
            }
            catch (Exception e)
            {
                return " ";
            }
        }

        public static void GetPageRange(int SelectedIndex, CPDFDocument document, ref string PageRange, string pageRangeText)
        {

            switch (SelectedIndex)
            {
                case 0:
                    {
                        PageRange = "0-" + (document.PageCount - 1);
                    }
                    break;
                case 1:
                    {
                        PageRange = "0";
                        for (int i = 2; i <= (document.PageCount - 1); i += 2)
                            PageRange = PageRange + "," + i;
                    }
                    break;
                case 2:
                    if (document.PageCount >= 2)
                    {
                        PageRange = "1";
                        for (int i = 3; i <= (document.PageCount - 1); i += 2)
                            PageRange = PageRange + "," + i;
                    }
                    else
                    {
                        MessageBox.Show("超出页面");
                    }
                    break;
                case 3:
                    PageRange = ParseRange(pageRangeText);
                    Trace.WriteLine("PageRange : " + PageRange);
                    break;
                default:
                    break;
            }
        }
    }
}
