using Prism.Commands;
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.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 ComboNumberItem
    {
        public double Value { get; set; }
        public string Content { get; set; }
        public string Unit { get; set; }
        public ComboNumberItem(double value,string unitStr = "")
        {
            Content = value + unitStr;
        }
    }
    /// <summary>
    /// SlidComboControl.xaml 的交互逻辑
    /// </summary>
    public partial class SlidComboControl : UserControl
    {
        public List<ComboNumberItem> Items { get;private set; }    
        public event RoutedEventHandler ValueChanged;
        
        public SlidComboControl()
        {
            InitializeComponent();
           
            Items = new List<ComboNumberItem>();
            DefaultItems();
        }

        public void SetItemSource(List<ComboNumberItem> items)
        {
            Items = items;
            ThicknessBox.ItemsSource = Items;
        }

        private void DefaultItems()
        {
            var item = new ComboNumberItem(3);
            Items.Add(item);
            item = new ComboNumberItem(6);
            Items.Add(item);
            item = new ComboNumberItem(9);
            Items.Add(item);
            item = new ComboNumberItem(12);
            Items.Add(item);
            item = new ComboNumberItem(15);
            Items.Add(item);
            item = new ComboNumberItem(18);
            Items.Add(item);
            item = new ComboNumberItem(21);
            Items.Add(item);
            item = new ComboNumberItem(24);
            Items.Add(item);

            ThicknessBox.ItemsSource = Items;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ThicknessBox.SelectionChanged -= ThicknessBox_SelectionChanged;
            ThicknessBox.SelectionChanged += ThicknessBox_SelectionChanged;
            ThicknessSlider.ValueChanged += ThicknessSlider_ValueChanged;
            ThicknessBox.SelectedIndex = 0;
        }

        private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            foreach(var item in ThicknessBox.Items)
            {

                var itemData = item as ComboNumberItem;
                if(itemData != null)
                {
                    if(itemData.Value == Value)
                    {
                        ThicknessBox.SelectedItem = itemData;
                        break;
                    }
                }
            }
            ThicknessText.Text = Value.ToString();
        }

        private void ThicknessBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = ThicknessBox.SelectedItem as ComboNumberItem;
            if(item != null)
            {
                Value = item.Value;
                ThicknessText.Text = item.Content;
                ValueChanged?.Invoke(Value, e);
            }
           

        }

        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        public static readonly DependencyProperty ValueProperty =
           DependencyProperty.Register("Value", typeof(double), typeof(SlidComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged));
       
        private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as SlidComboControl;
            var value = (double)e.NewValue;
            if (control != null)
            {
                control.ValueChanged?.Invoke(value, null);
            }
        }
    }
}
