using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Shapes;

namespace CollaborativePlatformMain.Form.UserControlForm
{
    /// <summary>
    /// NativeCombox.xaml 的交互逻辑
    /// </summary>
    public partial class NativeCombox : UserControl
    {
        /// <summary>
        /// 初始化渲染
        /// </summary>
        public NativeCombox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void comboxHeand(object sender, EventArgs e);

        /// <summary>
        /// checkboc按钮点击命令
        /// </summary>
        public event comboxHeand UserControlComBoxClick;

        /// <summary>
        /// 下拉按钮点击命令
        /// </summary>
        public event comboxHeand UserControlToggleClick;

        /// <summary>
        /// 当前 NativeCombox 选中的值
        /// </summary>
        public List<string> listKey;

        /// <summary>
        /// ItemsSource属性
        /// </summary>
        public List<NativeDataInfo> ItemsSource
        {
            get { return (List<NativeDataInfo>)GetValue(ItemsSourceProperty); }
            set
            {
                SetValue(ItemsSourceProperty, value);
                SetText();
            }
        }

        /// <summary>
        /// ItemsSource实体属性
        /// </summary>
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(object), typeof(NativeCombox), new FrameworkPropertyMetadata(null, ItemsSourcePropertyChangedCallback));

        /// <summary>
        /// ItemsSource实体属性改变事件
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <param name="e"></param>
        private static void ItemsSourcePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var multioleCheckbox = dependencyObject as NativeCombox;
            if (multioleCheckbox == null) return;
            multioleCheckbox.CheckableCombo.ItemsSource = multioleCheckbox.ItemsSource;
        }

        /// <summary>
        /// Text属性
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        /// <summary>
        /// text标题实体
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(NativeCombox), new FrameworkPropertyMetadata("", TextPropertyChangedCallback));

        private static void TextPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var multioleCheckbox = dependencyObject as NativeCombox;
            if (multioleCheckbox == null) return;
        }

        /// <summary>
        /// 默认标题
        /// </summary>
        public string DefaultText
        {
            get { return (string)GetValue(DefaultTextProperty); }
            set { SetValue(DefaultTextProperty, value); }
        }

        /// <summary>
        /// 默认标题实体
        /// </summary>
        public static readonly DependencyProperty DefaultTextProperty =
            DependencyProperty.RegisterAttached("DefaultText", typeof(string), typeof(NativeCombox), new UIPropertyMetadata(string.Empty));

        /// <summary>
        /// checkBox 选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            var checkbox = sender as CheckBox;
            if (checkbox == null) return;
            SetText();
            if (UserControlComBoxClick != null)
                UserControlComBoxClick(sender, new EventArgs());
        }

        /// <summary>
        /// 设置text值
        /// </summary>
        public void SetText()
        {
            Text = "";
            listKey = new List<string>();
            foreach (var item in ItemsSource)
            {
                if (item.IsSelect)
                {
                    Text += item.Name + ",";
                    listKey.Add(item.Key);
                }
            }
            Text = string.IsNullOrEmpty(Text) ? DefaultText : Text.TrimEnd(new[] { ',' });
        }

        /// <summary>
        /// 下拉按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            if (UserControlToggleClick != null)
                UserControlToggleClick(sender, new EventArgs());
        }
    }

    /// <summary>
    ///
    /// <para>文件名(File Name):     NativeDataInfo.cs</para>
    ///
    /// <para>描述(Description):     combox控件实体数据源</para>
    ///
    /// <para>数据表(Tables):        nothing</para>
    ///
    /// <para>作者(Author):          Li Wen Jin</para>
    ///
    /// <para>日期(Create Date):     2023年3月4日09:37:20</para>
    ///
    /// 修改记录(Revision History):
    ///     R1:
    ///         修改作者:
    ///         修改日期:
    ///         修改理由:
    ///
    /// </summary>
    public class NativeDataInfo : INotifyPropertyChanged
    {
        /// <summary>
        /// 唯一标识
        /// </summary>
        private string key;

        /// <summary>
        /// 唯一标识
        /// </summary>
        public string Key
        {
            get { return key; }
            set { key = value; }
        }

        /// <summary>
        /// 展示名称
        /// </summary>
        private string name;

        /// <summary>
        /// 展示名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 是否选中
        /// </summary>
        private bool isSelect;



        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsSelect
        {
            get { return isSelect; }
            set
            {
                if (value != isSelect)
                {
                    isSelect = value;
                    NotifyPropertyChanged("IsSelect");
                }
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="key">唯一标识</param>
        /// <param name="name">界面展示内容</param>
        public NativeDataInfo(string key, string name)
        {
            this.key = key;
            this.name = name;
        }

        public NativeDataInfo()
        {
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="isSelect"></param>
        /// <param name="name"></param>
        public NativeDataInfo(bool isSelect, string name)
        {
            this.name = name;
            this.isSelect = isSelect;
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
