using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MECF.Framework.UI.Core.Control
{
    /// <summary>
    /// Interaction logic for ColorPicker.xaml
    /// </summary>
    public partial class ColorPicker : UserControl
    {
        // 定义一个简单的数据类，用于ComboBox显示
        public class ColorItem
        {
            public string Name { get; set; }
            public Color Color { get; set; }
        }

        public ObservableCollection<ColorItem> AvailableColors { get; private set; }


        public ColorPicker()
        {
            AvailableColors = new();
            InitializeComponent();
            LoadColors();
        }

        // --- 核心：DependencyProperty ---
        // 这让 SelectedColor 可以被外部绑定，例如 <my:Picker SelectedColor="{Binding ThemeColor}"/>
        public static readonly DependencyProperty SelectedColorProperty =
            DependencyProperty.Register(nameof(SelectedColor), typeof(Color), typeof(ColorPicker),
                new FrameworkPropertyMetadata(
                    Colors.Black, 
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
                    PropertyChangedCallback,
                    CoerceSelectedColor));

        private static object CoerceSelectedColor(DependencyObject d, object baseValue)
        {
            if (d is not ColorPicker sender)
                return baseValue;

            try
            {
                var oldColor = (Color)sender.GetValue(SelectedColorProperty);
                var newColor = (Color)baseValue;

                // Update IsColorChanged without breaking any binding on that DP
                sender.SetCurrentValue(IsColorChangedProperty, oldColor != newColor);
            }
            catch
            {
                // swallow any unexpected cast issues to avoid runtime crash;
                // coerce should be lightweight and safe.
            }

            return baseValue;
        }

        private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is not ColorPicker sender)
                return;

            if (e.Property.Name == nameof(SelectedColor))
            {
                var oldColor = (Color)e.OldValue;
                var newColor = (Color)e.NewValue;
                // Use SetCurrentValue so existing bindings on IsColorChanged are preserved.
                sender.SetCurrentValue(IsColorChangedProperty, oldColor != newColor);
            }
        }

        //这是CLR包装器，方便代码直接访问
        public Color SelectedColor
        {
            get => (Color)GetValue(SelectedColorProperty);
            set => SetValue(SelectedColorProperty, value);
        }


        public static readonly DependencyProperty IsColorChangedProperty = DependencyProperty.Register(
            nameof(IsColorChanged), typeof(bool), typeof(ColorPicker),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public bool IsColorChanged
        {
            get => (bool)GetValue(IsColorChangedProperty);
            set => SetValue(IsColorChangedProperty, value);
        }

        private void LoadColors()
        {
            // 反射获取 System.Windows.Media.Colors 的所有静态属性
            var colorProperties = typeof(Colors).GetProperties(BindingFlags.Public | BindingFlags.Static);

            foreach (var prop in colorProperties)
            {
                AvailableColors.Add(new ColorItem
                {
                    Name = prop.Name,
                    Color = (Color)prop.GetValue(null, null)
                });
            }
        }
    }
}
