using ComPDFKit.PDFAnnotation;
using ComPDFKitViewer.AnnotEvent;
using PDF_Office.CustomControl;
using PDF_Office.ViewModels.PropertyPanel.AnnotPanel;
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.Controls.Primitives;
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;

namespace PDF_Office.Views.PropertyPanel.AnnotPanel
{
    /// <summary>
    /// SharpsAnnotProperty.xaml 的交互逻辑
    /// </summary>
    public partial class SharpsAnnotProperty : UserControl
    {
        private SharpsAnnotPropertyViewModel ViewModel => DataContext as SharpsAnnotPropertyViewModel;
        public SharpsAnnotProperty()
        {
            InitializeComponent();
            cusColor.SelectedColorHandler += cusColor_SelectedColor;
            layerThick.SelectedValueChanged += layerThick_SelectedValue;

            cusFillColor.SelectedColorHandler += cusFillColor_SelectedColor;
            layerFill.SelectedValueChanged += layerFill_SelectedValue;
            ViewModel.LoadPropertyHandler += ViewModel_LoadPropertyHandler;
        }

        private void ViewModel_LoadPropertyHandler(object sender, object e)
        {
            var item = e as AnnotHandlerEventArgs;
            switch (item.EventType)
            {
                case AnnotArgsType.AnnotSquare:
                    {
                        var annot = item as SquareAnnotArgs;
                        if (annot != null)
                        {
                            cusColor.SelectedColor = annot.LineColor;
                            cusFillColor.SelectedColor= annot.BgColor;
                            layerFill.SetSliOpacity(annot.Transparency);
                            SharpsBtn_Click(SharpRectBtn, null);

                            SolidLineIschecked(annot.LineDash.Dashes == null || annot.LineDash.Dashes.Count == 0 || annot.LineDash.Dashes[0] == 0);
                        }
                        break;
                    }

                case AnnotArgsType.AnnotCircle:
                    {
                        var annot = item as CircleAnnotArgs;
                        if (annot != null)
                        {
                            cusColor.SelectedColor=annot.LineColor;
                            cusFillColor.SelectedColor=annot.BgColor;
                            layerFill.SetSliOpacity(annot.Transparency);
                            SharpsBtn_Click(SharpCircleBtn, null);

                            SolidLineIschecked(annot.LineDash.Dashes == null || annot.LineDash.Dashes.Count == 0 || annot.LineDash.Dashes[0] == 0);
                        }
                        break;
                    }

                case AnnotArgsType.AnnotLine:
                    {
                        var annot = item as LineAnnotArgs;
                        if (annot != null)
                        {
                            cusColor.SelectedColor=annot.LineColor;
                            cusFillColor.SelectedColor=annot.LineColor;
                            layerFill.SetSliOpacity(annot.Transparency);

                            if (annot.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && annot.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
                                SharpsBtn_Click(SharpArrowBtn, null);
                            else
                                SharpsBtn_Click(SharpLineBtn, null);

                            SolidLineIschecked(annot.LineDash.Dashes == null || annot.LineDash.Dashes.Count == 0 || annot.LineDash.Dashes[0] == 0);

                        }
                        break;
                    }
            }
        }

        private void SolidLineIschecked(bool isSolid)
        {
            if (isSolid)
            {
                BtnSolidLine.IsChecked = true;
                BtnDottedLine.IsChecked = false;
            }
            else
            {
                BtnDottedLine.IsChecked = true;
                BtnSolidLine.IsChecked = false;
            }
        }

        private void layerFill_SelectedValue(object sender, double e)
        {
            if (ViewModel != null)
            {
                ViewModel.SelectedFillOpacityCommand?.Execute(e);
            }
        }

        private void cusFillColor_SelectedColor(object sender, Color e)
        {
            if (ViewModel != null)
            {
                ViewModel.SelectedFillColorCommand?.Execute(e);
            }
        }

        private void layerThick_SelectedValue(object sender, double e)
        {
            if (ViewModel != null)
            {
                ViewModel.SelectedThickCommand?.Execute(e);
            }
        }

        private void cusColor_SelectedColor(object sender, Color e)
        {
            if (ViewModel != null)
            {
                ViewModel.SelectedColorCommand?.Execute(e);
            }
        }

        private void SharpsBtn_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as ToggleButton;
           foreach(var item in ToolGrid.Children)
            {
                var btnItem = item as ToggleButton;
                if(btnItem != null)
                {
                    if(btnItem != btn)
                    {
                        btnItem.IsChecked = false;
                    }
                    else
                    {
                        btnItem.IsChecked = true;
                        if(btnItem.Tag.ToString() == "Arrow" || btnItem.Tag.ToString() == "Line")
                        {
                            GridFill.Visibility = Visibility.Collapsed;
                        }
                        else
                        {
                            GridFill.Visibility = Visibility.Visible;
                        }
                    }
                }
            }
        }

        private void BtnLineStyle_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as CustomIconToggleBtn;
            foreach (var item in PnlLineStyle.Children)
            {
                var btnItem = item as CustomIconToggleBtn;
                if (btnItem != null)
                {
                    if (btnItem != btn)
                    {
                        btnItem.IsChecked = false;
                    }
                    else
                    {
                        btnItem.IsChecked = true;
                        
                    }
                }
            }
        }
    }
}
