using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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;

namespace PDF_Office.CustomControl.CompositeControl
{
    public class ColorItem: BindableBase
    {

        private Brush _color;
        public Brush Color
        {
            get { return _color; }
            set
            {
                SetProperty(ref _color, value);
            }
        }

        public Brush defaultColor { get; private set; }

        private bool _isTransparent;
        public bool IsTransparent
        {
            get { return _isTransparent; }
            set
            {
                SetProperty(ref _isTransparent, value);
            }
        }

        public ColorItem(Color color, bool isTransparent = false)
        {
            var colorBrush = new SolidColorBrush(color);
            Color = colorBrush;
            defaultColor = colorBrush;
            IsTransparent = isTransparent;
        }

        public void RestoreColor()
        {
            Color = defaultColor;
        }






    }

    //类说明：
    //复合控件：调色板
    //组合而成的控件：颜色圆形UI的列表容器、调色板选择的颜色圆形按钮UI，弹出调色版的按钮
    /// <summary>
    /// ColorContent.xaml 的交互逻辑
    /// </summary>
    public partial class ColorContent : UserControl
    {
        private ObservableCollection<ColorItem> colors = new ObservableCollection<ColorItem>();
        public event EventHandler<Color> SelectedColorHandler;
        private ColorItem ChangedColorItem;

        /// <summary>
        /// 选中项的颜色
        /// </summary>
        public Color SelectedColor
        {
            get { return (Color)GetValue(SelectedColorProperty); }
            set { SetValue(SelectedColorProperty, value); }
        }

        public static readonly DependencyProperty SelectedColorProperty =
            DependencyProperty.Register("SelectedColor", typeof(Color), typeof(ColorContent), new PropertyMetadata((Color)ColorConverter.ConvertFromString("#6C33F8CC")));


        public Visibility ShowColorList
        {
            get { return (Visibility)GetValue(ShowColorListProperty); }
            set { SetValue(ShowColorListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowColorList.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowColorListProperty =
            DependencyProperty.Register("ShowColorList", typeof(Visibility), typeof(ColorContent), new PropertyMetadata(Visibility.Visible));


        public ColorContent()
        {
            InitializeComponent();
            colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0xFF, 0x10)));
            colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0x10, 0x10))); 
            colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0xFF, 0x10)));
            colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0x70, 0xFF))); 
            ListColor.ItemsSource = colors;
        }

        private void ColorDropPicker_SelectedColorChanged(object sender, Color? e)
        {
            IsExistForContainer((ElcustomColor.Fill as SolidColorBrush).Color);
            SelectedColorHandler?.Invoke(this, e.Value);
            //ElcustomColor.Fill = new SolidColorBrush(e.Value);
            SelectedColor = e.Value;
        }

        private void ListColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(ListColor.SelectedItem != null)
            {
                var item = ListColor.SelectedItem as ColorItem;
                SelectedColorHandler?.Invoke(this, (item.Color as SolidColorBrush).Color);
            }
            
        }

        private void ElcustomColor_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IsExistForContainer((ElcustomColor.Fill as SolidColorBrush).Color);
            SelectedColorHandler?.Invoke(this, (ElcustomColor.Fill as SolidColorBrush).Color);
            
        }

        private void PnlColor_SelectedColorChanged(object sender, Color? e)
        {
            ChangedColorItem.Color = new SolidColorBrush(e.Value);
            if (ChangedColorItem == ListColor.SelectedItem as ColorItem)
                SelectedColorHandler?.Invoke(this, (ChangedColorItem.Color as SolidColorBrush).Color);
        }

        private void ChangeColorMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var colorIem = (sender as FrameworkElement).DataContext as ColorItem;
            var listBoxItem = ListColor.ItemContainerGenerator.ContainerFromItem(colorIem) as ListBoxItem;

            if (listBoxItem != null)
            {
                ChangedColorItem = colorIem;
                PnlColor.SelectedColor = (colorIem.Color as SolidColorBrush).Color;
                PnlColor?.ShowLayer(listBoxItem);
            }

        }

        private void BackColorMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var colorIem = (sender as FrameworkElement).DataContext as ColorItem;

            var newColor = (colorIem.Color as SolidColorBrush).Color;
            var oldColor = (colorIem.defaultColor as SolidColorBrush).Color;

            if (newColor.A != oldColor.A || newColor.B != oldColor.B || newColor.R != oldColor.R || newColor.G != oldColor.G)
            {
                colorIem.RestoreColor();
                if(colorIem == ListColor.SelectedItem as ColorItem)
                SelectedColorHandler?.Invoke(this, (colorIem.Color as SolidColorBrush).Color);
            }
               
        }

        private void listboxItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {           
            e.Handled = true;
        }

        public void SetSelectedColor(Color newColor)
        {
            if (newColor != null)
            {
                if (IsExistForContainer(newColor) == false)
                {
                    ElcustomColor.Fill = new SolidColorBrush(newColor);
                }
            }

        }

        private bool IsExistForContainer(Color newColor)
        {
            bool isSelectList = false;
            ListColor.SelectedItem = null;

            foreach (var item in ListColor.Items)
            {
                var itemcontainer = ((item as ColorItem).Color as SolidColorBrush).Color;
                if (itemcontainer.A == newColor.A && itemcontainer.R == newColor.R && itemcontainer.G == newColor.G && itemcontainer.B == newColor.B)
                {
                    isSelectList = true;
                    ListColor.SelectedItem = item;
                    break;
                }
            }

            return isSelectList;
        }

    }
}
