using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Media;
using MECF.Framework.Common.Aitex.Core.RT.EMS;
using DocumentFormat.OpenXml.EMMA;

namespace MECF.Framework.UI.Client.CenterViews.Maintain.Behavior
{
    public class ItemChangedTargetItemChangedBehavior : Behavior<Control>
    {

        public ListBox TargetListBox
        {
            get { return (ListBox)GetValue(TargetListBoxProperty); }
            set { SetValue(TargetListBoxProperty, value); }
        }

        public static readonly DependencyProperty TargetListBoxProperty =
            DependencyProperty.Register("TargetListBox", typeof(ListBox), typeof(ItemChangedTargetItemChangedBehavior), new PropertyMetadata(null));

        public List<MaintainerItemConfigEnableInfo> Target
        {
            get { return (List<MaintainerItemConfigEnableInfo>)GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        public static readonly DependencyProperty TargetProperty =
            DependencyProperty.Register("Target", typeof(List<MaintainerItemConfigEnableInfo>), typeof(ItemChangedTargetItemChangedBehavior), new PropertyMetadata(null));

        //public Color FalseColor
        //{
        //    get { return (Color)GetValue(FalseColorProperty); }
        //    set { SetValue(FalseColorProperty, value); }
        //}

        //public static readonly DependencyProperty FalseColorProperty =
        //    DependencyProperty.Register("FalseColor", typeof(Color), typeof(ItemChangedTargetItemChangedBehavior), new PropertyMetadata(Colors.Black));

        //public Color TrueColor
        //{
        //    get { return (Color)GetValue(TrueColorProperty); }
        //    set { SetValue(TrueColorProperty, value); }
        //}

        //public static readonly DependencyProperty TrueColorProperty =
        //    DependencyProperty.Register("TrueColor", typeof(Color), typeof(ItemChangedTargetItemChangedBehavior), new PropertyMetadata(Colors.Green));

        protected override void OnAttached()
        {
            if (AssociatedObject is ListBox lb)
            {
                lb.SelectionChanged += SelectedOperation;
            }
            //else if (AssociatedObject is TabControl tb)
            //{
            //    tb.SelectionChanged += TabControlSelectedOperation;
            //}

        }

        //private void TabControlSelectedOperation(object sender, RoutedEventArgs e)
        //{
        //    var source = e.OriginalSource as TabControl;
        //    if (source.SelectedValue is KeyValuePair<string,ObservableCollection<MaintainerInfo>> kv)
        //    {
        //        var item = kv.Value.ToList().Find(i => i.IsSelected);
        //        if (item != null && Target != null)
        //        {
        //            string type = item.Type;
        //            string name = item.Name;
        //            string module = item.Module;

        //            SelectPage(type, name, module);
        //        }

        //    }
        //}

        private void SelectPage(string type,string name, string module)
        {
            //变更checkbox显示内容
            if (Target == null)
            {
                return;
            }
            foreach (var item in Target)
            {
                item.SelectPage(type, name, module);
            }

        }

        private void SelectedOperation(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 1)
            {
                if (e.AddedItems[0] is MaintainerInfo maintenanceInfo)
                {
                    string type = maintenanceInfo.Type;
                    string name = maintenanceInfo.Name;
                    string module = maintenanceInfo.Module;
                    SelectPage(type, name, module);

                    //选择项高亮显示
                    if (TargetListBox != null)
                    {
                        foreach (var item in TargetListBox.ItemsSource)
                        {
                            var info = item as MaintainerInfo;
                            info.IsSelected = info.Equals(TargetListBox.SelectedItem as MaintainerInfo);
                            info.InvokePropertyChanged(nameof(info.IsSelected));
                        }
                        (TargetListBox.SelectedItem as MaintainerInfo).IsSelected = true;
                    }
                }
            };
        }

    }
}
