using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using PDF_Office.Model;
using PDF_Office.ViewModels.Tools;
using PDFSettings;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
{
    public class AnnotArgsTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is AnnotArgsType)
            {
                if (parameter is string)
                {
                    var args = (AnnotArgsType)value;
                    if ((string)parameter == "AnnotHighlight" && args == AnnotArgsType.AnnotHighlight)
                    {
                        return Visibility.Visible;
                    }

                    if ((string)parameter == "AnnotUnderline" && args == AnnotArgsType.AnnotUnderline)
                    {
                        return Visibility.Visible;
                    }

                    if ((string)parameter == "AnnotSquiggly" && args == AnnotArgsType.AnnotSquiggly)
                    {
                        return Visibility.Visible;
                    }

                    if ((string)parameter == "AnnotStrikeout" && args == AnnotArgsType.AnnotStrikeout)
                    {
                        return Visibility.Visible;
                    }

                    if ((string)parameter == "AnnotSticky" && args == AnnotArgsType.AnnotSticky)
                    {
                        return Visibility.Visible;
                    }
                }
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class TextAnnotPropertyViewModel:BindableBase, INavigationAware
    {
     

        #region 属性

        private AnnotArgsType annotType;
        public AnnotArgsType AnnotType
        {
            get { return annotType; }
            set
            {
                SetProperty(ref annotType, value);
                SetAnnotType();
            }
        }


        private string annotTypeTitle;
        public string AnnotTypeTitle
        {
            get { return annotTypeTitle; }
            set
            {
                SetProperty(ref annotTypeTitle, value);
            }
        }


        private double annotOpacity;
        public double AnnotOpacity
        {
            get {return annotOpacity;}
            set 
            { 
                SetProperty(ref annotOpacity, value);
            }
        }


        /// <summary>
        /// 示例背景
        /// </summary>
        private Brush sampleTextBg = new SolidColorBrush(Colors.Transparent);
        public Brush SampleTextBg
        {
            get { return sampleTextBg; }
            set { SetProperty(ref sampleTextBg, value); 
                AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (sampleTextBg as SolidColorBrush).Color);
                AnnotEvent?.UpdateAnnot();
            }
        }


        private Brush selectColor = new SolidColorBrush(Colors.Transparent);
        public Brush SelectColor
        {
            get { return selectColor; }
            set { 
                SetProperty(ref selectColor, value);
                AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (selectColor as SolidColorBrush).Color);
                AnnotEvent?.UpdateAnnot();
               // SetAnnotProperty(); 
            }
        }

        #endregion

        public AnnotAttribEvent AnnotEvent { get; set; }
        private AnnotHandlerEventArgs Annot;
        private AnnotPropertyPanel PropertyPanel;
        public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
        public DelegateCommand<object> OpacityItemCommand { get; set; }
        public DelegateCommand<object> OpacityValueChangedCommand { get; set; }
        public DelegateCommand<object> SelectedValueChangedCommand { get; set; }

        public event EventHandler<object> LoadPropertyHandler;

        public TextAnnotPropertyViewModel()
        {
            SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
            OpacityItemCommand = new DelegateCommand<object>(OpacityItemCommand_Click);
            OpacityValueChangedCommand = new DelegateCommand<object>(OpacityValueChanged_Command);
            SelectedValueChangedCommand = new DelegateCommand<object>(SelectedValueChanged_Command);
        }

        private void SelectedValueChanged_Command(object obj)
        {
            if (obj != null)
            {
                annotOpacity = (double)obj;
                if (AnnotType == AnnotArgsType.AnnotHighlight)
                    SampleTextBg.Opacity = annotOpacity;
                else
                    SelectColor.Opacity = annotOpacity;

                AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
                AnnotEvent?.UpdateAnnot();
           
                Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
                changeData[AnnotType] = annotOpacity;
                PropertyPanel.DataChangedInvoke(this, changeData);
            }
        }

        private void OpacityItemCommand_Click(object obj)
        {
            if (obj != null)
            {
                if (AnnotType == AnnotArgsType.AnnotHighlight)
                    SampleTextBg.Opacity = annotOpacity;
                else
                    SelectColor.Opacity = annotOpacity;

                AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
                AnnotEvent?.UpdateAnnot();
      
                Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
                changeData[AnnotType] = annotOpacity;
                PropertyPanel.DataChangedInvoke(this, changeData);
            }
        }

        private void OpacityValueChanged_Command(object obj)
        {
            if (obj != null)
            {
                if (AnnotType == AnnotArgsType.AnnotHighlight)
                    SampleTextBg.Opacity = annotOpacity;
                else
                    SelectColor.Opacity = annotOpacity;

                AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
                AnnotEvent?.UpdateAnnot();
               
                Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
                changeData[AnnotType] = annotOpacity;
                PropertyPanel.DataChangedInvoke(this, changeData);
            }
        }

        public void SelectedColorChanged_Click(object color)
        {
            if (color != null && PropertyPanel != null)
            {
                var colorValue = (Color)color;
                if (colorValue != null)
                {
                   
                    switch (AnnotType)
                    {
                        case AnnotArgsType.AnnotHighlight:
                            SampleTextBg = new SolidColorBrush(colorValue);
                            SampleTextBg.Opacity = AnnotOpacity;
                            break;
                        case AnnotArgsType.AnnotUnderline:
                        case AnnotArgsType.AnnotStrikeout:
                        case AnnotArgsType.AnnotSquiggly:
                        case AnnotArgsType.AnnotSticky:
                            SelectColor = new SolidColorBrush(colorValue);
                            SelectColor.Opacity = AnnotOpacity;
                            break;
                    }

                    Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
                    changeData[AnnotType] = color;
                    PropertyPanel.DataChangedInvoke(this, changeData);
                }
            }
        }

        private void SetAnnotType()
        {
            switch (AnnotType)
            {
                case AnnotArgsType.AnnotHighlight:
                    AnnotTypeTitle = "高亮";
                    break;
                case AnnotArgsType.AnnotUnderline:
                    AnnotTypeTitle = "下划线";
                    break;
                case AnnotArgsType.AnnotStrikeout:
                    AnnotTypeTitle = "删除线";
                    break;
                case AnnotArgsType.AnnotSquiggly:
                    AnnotTypeTitle = "波浪线";
                    break;
                case AnnotArgsType.AnnotSticky:
                    AnnotTypeTitle = "？？";
                    break;
            }
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }


       
        public void OnNavigatedTo(NavigationContext navigationContext)
        {

            navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
            if(PropertyPanel != null)
            {
                AnnotEvent = PropertyPanel.AnnotEvent;
                Annot = PropertyPanel.annot;
                AnnotType = Annot.EventType;
                GetAnnotProperty();
                LoadPropertyHandler?.Invoke(null, Annot);
            }

        }

        private void GetAnnotProperty()
        {
            if (Annot != null)
            {
                switch (Annot.EventType)
                {
                    case AnnotArgsType.AnnotHighlight:
                      if (Annot is TextHighlightAnnotArgs)
                        {
                            var Hightlight = Annot as TextHighlightAnnotArgs;
                            AnnotOpacity = Hightlight.Transparency;
                            SampleTextBg = new SolidColorBrush(Hightlight.Color);

                        }
                        break;

                    case AnnotArgsType.AnnotUnderline:
                        {
                            var Underline = Annot as TextUnderlineAnnotArgs;
                            AnnotOpacity = Underline.Transparency;
                             SampleTextBg = new SolidColorBrush(Colors.Transparent);
                            SelectColor = new SolidColorBrush(Underline.Color);
                        }
                        break;

                    case AnnotArgsType.AnnotStrikeout:
                        {
                            var Strikeout = Annot as TextStrikeoutAnnotArgs;
                            AnnotOpacity = Strikeout.Transparency;
                            SampleTextBg = new SolidColorBrush(Colors.Transparent);
                            SelectColor = new SolidColorBrush(Strikeout.Color);
                        }
                        break;

                    case AnnotArgsType.AnnotSquiggly:
                        {
                            var Squiggly = Annot as TextSquigglyAnnotArgs;
                            AnnotOpacity = Squiggly.Transparency;
                            SampleTextBg = new SolidColorBrush(Colors.Transparent);
                            SelectColor = new SolidColorBrush(Squiggly.Color);
                        }
                        break;

                }
            }
        }
    }
}
