using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
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;

namespace PDF_Office.CustomControl.CompositeControl
{
    public class AdornerPresenter : Adorner
    {
        private VisualCollection VisualChildren;
        private ContentPresenter Content;
        public AdornerPresenter(UIElement adornedElement) : base(adornedElement)
        {
            VisualChildren = new VisualCollection(this);
            Content = new ContentPresenter();
            VisualChildren.Add(Content);
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Content.Measure(constraint);
            return Content.DesiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            Content.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
            return Content.RenderSize;
        }

        protected override Visual GetVisualChild(int index)
        {
            return VisualChildren[index];
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return VisualChildren.Count;
            }
        }

        public object VisualContent
        {
            get
            {
                return Content.Content;
            }
            set
            {
                Content.Content = value;
            }
        }
    }
    /// <summary>
    /// ThicknessContent.xaml 的交互逻辑
    /// </summary>
    public partial class SlidContent : UserControl
    {
        private AdornerLayer popLayer;
        private AdornerPresenter layerPresent;
        private bool layerAdded = false;
        private Canvas popCanvas;
        private SlidContentPop colorPop;
        public  EventHandler<double> SelectedValueChanged;
        public event PropertyChangedEventHandler PropertyChanged; 
        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        private Color? _selectedColor;
        public Color? SelectedColor
        {
            get
            {
                return _selectedColor;
            }
            set
            {
                if (_selectedColor != value)
                {
                    _selectedColor = value;
                    OnPropertyChanged("SelectedColor");
                }
            }
        }
       
        public SlidContent()
        {
            InitializeComponent();
            Loaded += ColorDropBox_Loaded;
            Unloaded += ColorDropBox_Unloaded;
            MouseDown += ColorDropBox_MouseDown;
            colorPop = new SlidContentPop();
            colorPop.OpacityValueChangedHandler += colorPop_OpacityValueChangedHandler;
        }

        private void colorPop_OpacityValueChangedHandler(object sender, double e)
        {
            SelectedValueChanged?.Invoke(this, e);
        }

        private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
        {
            if (layerAdded)
            {
                RemoveFromLayer();
                return;
            }
        }

        private void ColorDropBox_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            if (layerAdded)
            {
                RemoveFromLayer();
                return;
            }
            AddToLayer();
        }

        public void ShowLayer(DependencyObject dependencyObject = null)
        {
            if (layerAdded)
            {
                RemoveFromLayer();
                return;
            }
            AddToLayer(dependencyObject);
        }


        private void AddToLayer(DependencyObject dependencyObject = null)
        {
            Window parentWnd = Window.GetWindow(this);
            popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
            if (popLayer != null && colorPop != null && !layerAdded)
            {
                if (layerPresent == null)
                {
                    layerPresent = new AdornerPresenter(popLayer);
                }
                popLayer.Add(layerPresent);
                if (popCanvas == null)
                {
                    popCanvas = new Canvas();
                    popCanvas.Children.Add(colorPop);
                    layerPresent.VisualContent = popCanvas;

                //    colorPop.ColorSelected += ColorPop_ColorSelected;
                }
                Point offset = GetOffsetToWindow(dependencyObject);
                colorPop.Visibility = Visibility.Visible;
                colorPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
                colorPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);

                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);
                }
                layerAdded = true;
            }
        }
        private void RemoveFromLayer()
        {
            if (popLayer != null && layerPresent != null && layerAdded)
            {
                popLayer.Remove(layerPresent);
                layerAdded = false;
            }
        }

        private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
        {
            if (popLayer == null)
            {
                Window parentWnd = Window.GetWindow(this);
                if (parentWnd != null && parentWnd.Content is UIElement && colorPop != null)
                {
                    parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
                    {
                        if (layerAdded)
                        {
                            try
                            {
                                Window checkWindow = Window.GetWindow(this);
                                Point clickPoint = param.GetPosition(checkWindow);
                                Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
                                Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
                                Point popLeftTop = new Point(Canvas.GetLeft(colorPop), Canvas.GetTop(colorPop));
                                Point popRightBottom = new Point(colorPop.ActualWidth, colorPop.ActualHeight);
                                Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
                                Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
                                if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
                                {
                                    return;
                                }
                                RemoveFromLayer();
                            }
                            catch (Exception ex)
                            {

                            }

                        }
                    }), true);
                }
            }
        }

        private void ColorPop_ColorSelected(object sender, Color e)
        {
            if (SelectedColor != e)
            {
                //NormalColorRectControl.Visibility = Visibility.Collapsed;
                //TransparentRectControl.Visibility = Visibility.Collapsed;
                //if (e != Colors.Transparent)
                //{
                //    NormalColorRectControl.Visibility = Visibility.Visible;
                //    NormalColorRectControl.Fill = new SolidColorBrush(e);
                //}
                //else
                //{
                //    TransparentRectControl.Visibility = Visibility.Visible;
                //}
                SelectedColor = e;
              //  SelectedValueChanged?.Invoke(this, SelectedColor);
            }
            RemoveFromLayer();
        }

        public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
        {
            Window parentWnd = Window.GetWindow(this);
            if (dependencyObject != null)
            {
                var frame = dependencyObject as FrameworkElement;
                if (frame != null)
                {
                    return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
                }
            }

            return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
        }

        public void SetThemeColors(List<Color> themeColorList)
        {
            if (colorPop != null)
            {
              //  colorPop.ThemeColors.Clear();
                //foreach (Color themeColor in themeColorList)
                //{
                //    colorPop.ThemeColors.Add(themeColor);
                //}
            }
        }

        public void  SetSliOpacity(double newopacity)
        {
            colorPop.OpacitySlider.Value = newopacity;
        }

    }
}
