using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using PDF_Office.Model;
using PDF_Office.Model.PropertyPanel.AnnotPanel;
using PDF_Office.ViewModels.Tools;
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.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;

namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
{
    public class EraseThicknessConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double)
            {
                return (double)value * 6;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class FreehandAnnotPropertyViewModel : BindableBase, INavigationAware
    {
        private bool isPen = true;
        public bool IsPen
        {
            get { return isPen; }
            set
            {
                SetProperty(ref isPen, value);
            }
        }

        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();
            }
        }

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


        private double thicknessLine = 1;
        public double ThicknessLine
        {
            get { return thicknessLine; }
            set
            {
                SetProperty(ref thicknessLine, value);
                AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, thicknessLine);
                AnnotEvent?.UpdateAnnot();
            }
        }

        private double erasethicknessLine = 1;
        public double EraseThicknessLine
        {
            get { return erasethicknessLine; }
            set
            {
                SetProperty(ref erasethicknessLine, value);
                AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, erasethicknessLine);
                AnnotEvent?.UpdateAnnot();
            }
        }

      

        public AnnotAttribEvent AnnotEvent { get; set; }
        private AnnotHandlerEventArgs Annot;
        private AnnotPropertyPanel PropertyPanel;
        public DelegateCommand<object> EraseCommand { get; set; }
        public DelegateCommand<object> PenCommand { get; set; }

        public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
        public DelegateCommand<object> SelectPenThickChangedCommand { get; set; }
        public DelegateCommand<object> SetEraserThickCommand { get; set; }
        public DelegateCommand<object> LineModeCheckedCommand { get; set; }
        public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }

        


        public FreehandAnnotPropertyViewModel()
        {
            EraseCommand = new DelegateCommand<object>(Erase_Command);
            PenCommand = new DelegateCommand<object>(Pen_Command);

            SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
            SelectPenThickChangedCommand = new DelegateCommand<object>(SelectPenThickChanged_Command);
            SetEraserThickCommand = new DelegateCommand<object>(SelectEraserThickChanged_Command);
            LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
            SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);

            InitVariable();
        }

        private void InitVariable()
        {

        }

        private void SelectEraserThickChanged_Command(object obj)
        {
            if (obj != null)
            {
                var item = (ComboBoxItem)obj;
                var content = (string)item.Content;
                if (content != null)
                {
                    var intData = double.Parse(content);
                    AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
                    AnnotEvent?.UpdateAnnot();
                    EraseThicknessLine = intData;
                }
            }
        }

        private void LineMode_Checked(object obj)
        {
            if (obj != null)
            {
                var tag = ((string)obj);
                switch (tag)
                {
                    case "Dashed":
                        AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
                        break;

                    case "Solid":
                        AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
                        break;

                }
                AnnotEvent?.UpdateAnnot();
            }
        }


        private void SelectPenThickChanged_Command(object obj)
        {
            if (obj != null)
            {
                var item = (ComboBoxItem)obj;
                var content = (string)item.Content;
                if (content != null)
                {
                    var intData = double.Parse(content);
                    AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
                    AnnotEvent?.UpdateAnnot();
                    ThicknessLine = intData;
                }
            }
        }

        private void SelectedOpacityValue(object obj)
        {
            if (obj != null)
            {
                annotOpacity = (double)obj;
                SelectColor.Opacity = annotOpacity;

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


        
        private void SelectedColorChanged_Click(object obj)
        {
            if (obj != null)
            {
                var colorValue = (Color)obj;
                if (colorValue != null)
                {

                    SelectColor = new SolidColorBrush(colorValue);
                    SelectColor.Opacity = AnnotOpacity;

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

        private void Erase_Command(object obj)
        {
            var btn = obj as ToggleButton;
            if(btn.IsChecked == true)
            {
                Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
                changeData[AnnotArgsType.AnnotErase] = btn;
                PropertyPanel.DataChangedInvoke(this, changeData);
                IsPen = false;
            }
        }

        private void Pen_Command(object obj)
        {
            var btn = obj as ToggleButton;
            if (btn.IsChecked == true)
            {
                Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
                changeData[AnnotArgsType.AnnotErase] = btn;
                PropertyPanel.DataChangedInvoke(this, changeData);
                IsPen = true;
            }
        }


        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;
                GetAnnotProperty();
            }
        }

        private void GetAnnotProperty()
        {
            if (Annot is FreehandAnnotArgs)
            {
                var annot  = Annot as FreehandAnnotArgs;
                if (annot != null)
                {
                    AnnotOpacity = annot.Transparency;
                    SelectColor = new SolidColorBrush(annot.InkColor);
                    ThicknessLine = annot.LineWidth;
                    IsPen = true;
                }
            }

            if (Annot is EraseArgs)
            {
                var annot = Annot as EraseArgs;
                if (annot != null)
                {
                    EraseThicknessLine = annot.Thickness;
                    IsPen = false;
                }
            }
        }

    }
}