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 ViewModularContentViewModel ViewModularContentViewModel { 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 (PDFViewer == null)
            {
                return;
            }

            SetUpDrawMode(PDFViewer, obj);

            if (ViewModularContentViewModel.SplitScreenPDFViewer != null)
            {
                SetUpDrawMode(ViewModularContentViewModel.SplitScreenPDFViewer, obj);
            }
        }

        private void SetUpDrawMode(CPDFViewer pDFViewer, object obj)
        {
            DrawModes draw = DrawModes.Draw_Mode_Normal;
            UInt32 color = 0;
            if (LightDrawMode)
            {
                draw = DrawModes.Draw_Mode_Normal;
                SetDrawMode(draw, color, pDFViewer);
            }
            else if (SepiaDrawMode)
            {
                draw = DrawModes.Draw_Mode_Soft;
                SetDrawMode(draw, color, pDFViewer);
            }
            else if (ResedaDrawMode)
            {
                draw = DrawModes.Draw_Mode_Green;
                SetDrawMode(draw, color, pDFViewer);
            }
            else if (DarkDrawMode)
            {
                draw = DrawModes.Draw_Mode_Dark;
                SetDrawMode(draw, color, pDFViewer);
            }
            else
            {
                if (obj is RadioButton radioButton)
                {
                    draw = SetOtherThemeColors(radioButton, pDFViewer);
                }
            }
            //App.SplitScreenDrawModes = draw;
        }

        private DrawModes SetOtherThemeColors(RadioButton radioButton, CPDFViewer pDFViewer)
        {
            DrawModes draw = DrawModes.Draw_Mode_Normal;
            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, pDFViewer);

            draw = DrawModes.Draw_Mode_Custom;
            //App.SplitScreenColor = c;
            return draw;
        }

        private void SetDrawMode(DrawModes drawMode, UInt32 color, CPDFViewer pDFViewer)
        {
            if (pDFViewer.GetDrawMode() != drawMode)
            {
                pDFViewer.SetDrawMode(drawMode);
                UpdateFileInfoList(drawMode, color, pDFViewer);
            }
        }

        /// <summary>
        /// 更新文件信息
        /// </summary>
        /// <param name="modes"></param>
        /// <param name="color"></param>
        private void UpdateFileInfoList(DrawModes modes, UInt32 color, CPDFViewer pDFViewer)
        {
            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)
        {
            if (navigationContext.Parameters[ParameterNames.PDFViewer] is CPDFViewer pdfview)
            {
                PDFViewer = pdfview;
            }
            if (navigationContext.Parameters[ParameterNames.ViewModularContentViewModel] is ViewModularContentViewModel viewModularContentViewModel)
            {
                ViewModularContentViewModel = viewModularContentViewModel;
            }
        }
    }
}