using ComPDFKitViewer;
using ComPDFKitViewer.PdfViewer;
using Microsoft.Office.Interop.PowerPoint;
using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Properties;
using PDFSettings.Settings;
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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static PDF_Office.CustomControl.ColorDropBox;

namespace PDF_Office.Views.PropertyPanel.ViewModular
{
    /// <summary>
    /// ThemesContent.xaml 的交互逻辑
    /// </summary>
    public partial class ThemesContent : UserControl
    {
        private ColorDropBoxPop colorPop = new ColorDropBoxPop();
        private AdornerLayer popLayer;
        private AdornerPresenter layerPresent;
        private Canvas popCanvas;
        public Color? SelectedColor { get; set; }

        public ThemesContent()
        {
            InitializeComponent();
            InitBeforeShow();
        }

        /// <summary>
        /// 设置自定义主题显示状态
        /// </summary>
        public void InitBeforeShow()
        {
            if (Settings.Default.AppProperties.CustomColorList != null && Settings.Default.AppProperties.CustomColorList.Count > 0)
            {
                var list = Settings.Default.AppProperties.CustomColorList;
                switch (list.Count)
                {
                    case 1:
                        (((RBtnThemes1.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[0]);
                        RBtnThemes1.Visibility = Visibility.Visible;
                        break;

                    case 2:
                        (((RBtnThemes1.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[0]);
                        (((RBtnThemes2.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[1]);
                        RBtnThemes1.Visibility = Visibility.Visible;
                        RBtnThemes2.Visibility = Visibility.Visible;
                        break;

                    case 3:
                        (((RBtnThemes1.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[0]);
                        (((RBtnThemes2.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[1]);
                        (((RBtnThemes3.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[2]);
                        RBtnThemes1.Visibility = Visibility.Visible;
                        RBtnThemes2.Visibility = Visibility.Visible;
                        RBtnThemes3.Visibility = Visibility.Visible;
                        break;

                    case 4:
                        (((RBtnThemes1.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[0]);
                        (((RBtnThemes2.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[1]);
                        (((RBtnThemes3.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[2]);
                        (((RBtnThemes4.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[3]);
                        RBtnThemes1.Visibility = Visibility.Visible;
                        RBtnThemes2.Visibility = Visibility.Visible;
                        RBtnThemes3.Visibility = Visibility.Visible;
                        RBtnThemes4.Visibility = Visibility.Visible;
                        break;
                }
            }
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (popLayer == null)
            {
                Window parentWnd = Window.GetWindow(this);
                if (parentWnd != null && parentWnd.Content is UIElement)
                {
                    popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
                    if (popLayer != null && colorPop != null)
                    {
                        layerPresent = new AdornerPresenter(popLayer);
                        popLayer.Add(layerPresent);
                        colorPop.Visibility = Visibility.Collapsed;
                        colorPop.ColorSelected += ColorPop_ColorSelected;
                        popCanvas = new Canvas();
                        popCanvas.Children.Add(colorPop);
                        layerPresent.VisualContent = popCanvas;
                        parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
                        {
                            colorPop.Visibility = Visibility.Collapsed;
                        }), false);
                    }
                }
            }
        }

        /// <summary>
        /// 自定义主题颜色，选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ColorPop_ColorSelected(object sender, Color e)
        {
            if (RBtnThemes1.Visibility == Visibility.Collapsed)
            {
                RBtnThemes1.Visibility = Visibility.Visible;
                var panel = RBtnThemes1.Content as StackPanel;
                var rec = (panel.Children[0] as Border).Child as Rectangle;
                rec.Fill = new SolidColorBrush(e);
                Settings.Default.AppProperties.CustomColorList.Add(e);
            }
            else if (RBtnThemes2.Visibility == Visibility.Collapsed)
            {
                RBtnThemes2.Visibility = Visibility.Visible;
                var panel = RBtnThemes2.Content as StackPanel;
                var rec = (panel.Children[0] as Border).Child as Rectangle;
                rec.Fill = new SolidColorBrush(e);
                Settings.Default.AppProperties.CustomColorList.Add(e);
            }
            else if (RBtnThemes3.Visibility == Visibility.Collapsed)
            {
                RBtnThemes3.Visibility = Visibility.Visible;
                var panel = RBtnThemes3.Content as StackPanel;
                var rec = (panel.Children[0] as Border).Child as Rectangle;
                rec.Fill = new SolidColorBrush(e);
                Settings.Default.AppProperties.CustomColorList.Add(e);
            }
            else if (RBtnThemes4.Visibility == Visibility.Collapsed)
            {
                RBtnThemes4.Visibility = Visibility.Visible;
                var panel = RBtnThemes4.Content as StackPanel;
                var rec = (panel.Children[0] as Border).Child as Rectangle;
                rec.Fill = new SolidColorBrush(e);
                Settings.Default.AppProperties.CustomColorList.Add(e);
            }

            #region to do

            //if(Settings.Default.AppProperties.CustomColorList.Count>4)
            //{
            //    for(int i=4;i< Settings.Default.AppProperties.CustomColorList.Count;i++)
            //    {
            //        Settings.Default.AppProperties.CustomColorList.RemoveAt(i);
            //    }
            //}

            #endregion to do

            Settings.Default.Save();
            if (SelectedColor != e)
            {
                SelectedColor = e;
                colorPop.AddColorToRecent(e);
            }
        }

        /// <summary>
        /// 自定义颜色显示状态发生变化时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RBtnThemes1_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (RBtnThemes1.Visibility == Visibility.Visible &&
                RBtnThemes2.Visibility == Visibility.Visible &&
                RBtnThemes3.Visibility == Visibility.Visible &&
                RBtnThemes4.Visibility == Visibility.Visible)
            {
                RBtnCustom.Visibility = Visibility.Collapsed;
            }
            else
            {
                RBtnCustom.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// 添加自定义主题颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RBtnCustom_Click(object sender, RoutedEventArgs e)
        {
            if (colorPop != null)
            {
                if (colorPop.Visibility == Visibility.Visible)
                {
                    colorPop.Visibility = Visibility.Collapsed;
                    return;
                }
                colorPop.Visibility = Visibility.Visible;
                System.Windows.Point offset = GetOffsetToWindow();
                Window parentWnd = Window.GetWindow(this);

                colorPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
                colorPop.SetValue(Canvas.TopProperty, offset.Y - colorPop.DesiredSize.Height / 4);

                if (offset.X + colorPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
                {
                    colorPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - colorPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
                }
                else
                {
                    colorPop.SetValue(Canvas.LeftProperty, offset.X - colorPop.DesiredSize.Width / 2 - 30);
                }
                colorPop.AddColorToRecent((Color)ColorConverter.ConvertFromString("White")); ;
            }
        }

        public System.Windows.Point GetOffsetToWindow()
        {
            Window parentWnd = Window.GetWindow(this);
            return TransformToAncestor(parentWnd).Transform(new System.Windows.Point(0, 0));
        }

        /// <summary>
        /// 删除自定义颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var contextmenu = (sender as MenuItem).Parent as ContextMenu;
            if (contextmenu != null)
            {
                RadioButton btn = contextmenu.PlacementTarget as RadioButton;
                btn.Visibility = Visibility.Collapsed;
                if (btn.IsChecked == true)
                {
                    //如果删除了选中色 则更换主题为白色
                    RBtnLight.IsChecked = true;
                }
                var color = (((btn.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill;
                if (Settings.Default.AppProperties.CustomColorList.Contains(((SolidColorBrush)color).Color))
                {
                    Settings.Default.AppProperties.CustomColorList.Remove(((SolidColorBrush)color).Color);
                    Settings.Default.Save();
                }
            }
        }
    }
}