using ComPDFKitViewer;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Properties;
using PDF_Office.Views.PropertyPanel.ViewModular;
using PDFSettings.Settings;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using static PDF_Office.CustomControl.ColorDropBox;
using RadioButton = System.Windows.Controls.RadioButton;

namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
{
    public class ThemesContentViewModel : BindableBase, INavigationAware
    {
        public CPDFViewer PDFViewer { get; set; }
        public Color? SelectedColor { get; set; }
        private bool resedaDrawMode;

        public bool ResedaDrawMode
        {
            get { return resedaDrawMode; }
            set
            {
                SetProperty(ref resedaDrawMode, value);
                if (value)
                {
                    SetDrawModeEvent(null);
                }
            }
        }

        private bool darkDrawMode;

        public bool DarkDrawMode
        {
            get { return darkDrawMode; }
            set
            {
                SetProperty(ref darkDrawMode, value);
                if (value)
                {
                    SetDrawModeEvent(null);
                }
            }
        }

        private bool sepiaDrawMode;

        public bool SepiaDrawMode
        {
            get { return sepiaDrawMode; }
            set
            {
                SetProperty(ref sepiaDrawMode, value);
                if (value)
                {
                    SetDrawModeEvent(null);
                }
            }
        }

        private bool lightDrawMode;

        public bool LightDrawMode
        {
            get { return lightDrawMode; }
            set
            {
                SetProperty(ref lightDrawMode, value);
                if (value)
                {
                    SetDrawModeEvent(null);
                }
            }
        }

        public DelegateCommand<object> LoadedCommand { get; set; }
        public DelegateCommand<object> SetDrawModeCommand { get; set; }

        public ThemesContentViewModel()
        {
            LoadedCommand = new DelegateCommand<object>(LoadedEvent);

            SetDrawModeCommand = new DelegateCommand<object>(SetDrawModeEvent);
        }

        private void LoadedEvent(object obj)
        {
            if (obj is WrapPanel wrapPanel)
            {
                InitBeforeShow(wrapPanel);
            }
        }

        /// <summary>
        /// 设置主题颜色
        /// </summary>
        /// <param name="obj"></param>
        private void SetDrawModeEvent(object obj)
        {
            //if (obj is RadioButton radioButton)
            //{
            //    bool sepia = radioButton.Name.IndexOf(ThemesDrawMode.Sepia.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
            //    bool light = radioButton.Name.IndexOf(ThemesDrawMode.Light.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
            //    bool reseda = radioButton.Name.IndexOf(ThemesDrawMode.Reseda.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
            //    bool dark = radioButton.Name.IndexOf(ThemesDrawMode.Dark.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
            //}

            DrawModes draw = PDFViewer.GetDrawMode();
            UInt32 color = 0;

            if (LightDrawMode)
            {
                draw = DrawModes.Draw_Mode_Normal;
                SetDrawMode(draw, color);
            }
            else if (SepiaDrawMode)
            {
                draw = DrawModes.Draw_Mode_Soft;
                SetDrawMode(draw, color);
            }
            else if (ResedaDrawMode)
            {
                draw = DrawModes.Draw_Mode_Green;
                SetDrawMode(draw, color);
            }
            else if (DarkDrawMode)
            {
                draw = DrawModes.Draw_Mode_Dark;
                SetDrawMode(draw, color);
            }
            else
            {
                if (obj is RadioButton radioButton)
                {
                    StackPanel panel = radioButton.Content as StackPanel;
                    Rectangle rec = (panel.Children[0] as Border).Child as Rectangle;
                    OpenFileInfo file = SettingHelper.GetFileInfo(PDFViewer?.Document.FilePath);
                    if (file != null)
                    {
                        file.LastFillBrushColor = ((SolidColorBrush)rec.Fill).Color;
                    }
                    var color1 = rec.Fill.ToString().Substring(1, 8);
                    UInt32 c = Convert.ToUInt32(color1, 16);
                    PDFViewer.SetDrawMode(DrawModes.Draw_Mode_Normal);
                    PDFViewer.SetDrawMode(DrawModes.Draw_Mode_Custom, c);
                    UpdateFileInfoList(DrawModes.Draw_Mode_Custom, c);
                }
            }
        }

        private void SetDrawMode(DrawModes drawMode, UInt32 color)
        {
            if (PDFViewer == null)
            {
                return;
            }

            if (PDFViewer.GetDrawMode() != drawMode)
            {
                PDFViewer.SetDrawMode(drawMode);
                UpdateFileInfoList(drawMode, color);
            }
        }

        /// <summary>
        /// 更新文件信息
        /// </summary>
        /// <param name="modes"></param>
        /// <param name="color"></param>
        private void UpdateFileInfoList(DrawModes modes, UInt32 color)
        {
            OpenFileInfo file = SettingHelper.GetFileInfo(PDFViewer?.Document.FilePath);
            if (file != null)
            {
                file.LastDrawMode = modes;
                if (color != 0)
                {
                    file.LastFillColor = color;
                }
            }
            SettingHelper.SetFileInfo(file);
        }

        /// <summary>
        /// 设置选中主题颜色
        /// </summary>
        /// <param name="wrapPanel"></param>
        private void InitBeforeShow(WrapPanel wrapPanel)
        {
            var mode = PDFViewer.GetDrawMode();
            if (mode == DrawModes.Draw_Mode_Normal)
            {
                LightDrawMode = true;
            }
            else if (mode == DrawModes.Draw_Mode_Dark)
            {
                DarkDrawMode = true;
            }
            else if (mode == DrawModes.Draw_Mode_Green)
            {
                ResedaDrawMode = true;
            }
            else if (mode == DrawModes.Draw_Mode_Soft)
            {
                SepiaDrawMode = true;
            }
            else
            {
                OpenFileInfo info = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
                var color = info.LastFillBrushColor;
                for (int i = 4; i < wrapPanel.Children.Count; i++)
                {
                    var btn = wrapPanel.Children[i] as RadioButton;
                    if (btn != null && Color.Equals(((SolidColorBrush)(((btn.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill).Color, color))
                    {
                        btn.IsChecked = true;
                    }
                }
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
            if (pdfview != null)
            {
                PDFViewer = pdfview;
            }
        }
    }
}