using FLY.Thick.Base.UI.OnInit;
using MultiLayout;
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 FLY.Thick.Base.UI.CustomSection
{
    /// <summary>
    /// UcSingleStateTab.xaml 的交互逻辑
    /// </summary>
    public partial class UcSingleStateTab : UserControl
    {
        List<ControllerStateTabItem> controllerStateTabItems;
        GageTabItem gageTabItem;
        ObservableCollection<ControllerStateTabItemHeader> controllerStateTabItemHeaders;
        public UcSingleStateTab()
        {
            InitializeComponent();
        }

        public void Init(List<ControllerStateTabItem> controllerStateTabItems, GageTabItem gageTabItem)
        {
            this.controllerStateTabItems = controllerStateTabItems;
            this.gageTabItem = gageTabItem;

            InitControllerstate();
        }


        void InitControllerstate()
        {
            controllerStateTabItemHeaders = new ObservableCollection<ControllerStateTabItemHeader>();
            foreach (var controllerStateTabItem in controllerStateTabItems)
            {
                ControllerStateTabItemHeader csh = new ControllerStateTabItemHeader();
                csh.ControllerState = controllerStateTabItem.ControllerState;
                if (controllerStateTabItem.No < gageTabItem.Items.Count() && controllerStateTabItem.No >= 0)
                {
                    csh.No = controllerStateTabItem.No;
                    csh.Header = gageTabItem.Items[controllerStateTabItem.No].Header;
                }
                else
                {
                    continue;
                }
                controllerStateTabItemHeaders.Add(csh);
            }
            this.itemsControl_controllerState.ItemsSource = controllerStateTabItemHeaders;


            combobox_controllerstate.ItemsSource = new List<string>(
                new string[] {
                    Base.Common.CTRL_STATE.FIX.ToString(),
                    Base.Common.CTRL_STATE.SCAN.ToString(),
                    Base.Common.CTRL_STATE.SYNC.ToString()
                });

            combobox_tabitemheader.ItemsSource = from i in gageTabItem.Items select i.Header;
        }
        private void button_add_Click(object sender, RoutedEventArgs e)
        {
            if (combobox_controllerstate.SelectedItem == null)
            {
                FLY.ControlLibrary.MyMessageBox.Show("请选择状态");
                return;
            }
            if (combobox_tabitemheader.SelectedItem == null)
            {
                FLY.ControlLibrary.MyMessageBox.Show("请选择页面");
                return;
            }
            //检测是否已经存在
            var v = from cs in controllerStateTabItemHeaders
                    where cs.ControllerState == combobox_controllerstate.SelectedItem as string
                    select cs;
            if (v.Count() > 0)
            {
                FLY.ControlLibrary.MyMessageBox.Show("已经存在,请删除后再添加");
                return;
            }

            var controllerStateTabItemHeader = new ControllerStateTabItemHeader()
            {
                ControllerState = combobox_controllerstate.SelectedItem as string,
                No = combobox_tabitemheader.SelectedIndex,
                Header = combobox_tabitemheader.SelectedItem as string
            };
            controllerStateTabItemHeaders.Add(controllerStateTabItemHeader);

            //添加到实际数据
            Add(controllerStateTabItemHeader);
            
            combobox_controllerstate.SelectedItem = null;
            combobox_tabitemheader.SelectedItem = null;
        }
        void Add(ControllerStateTabItemHeader item) 
        {
            //添加到实际数据
            controllerStateTabItems.Add(new ControllerStateTabItem()
            {
                ControllerState = item.ControllerState,
                No = item.No
            });
        }
        void Remove(ControllerStateTabItemHeader item) 
        {
            //删除实际数据
            var item2 = controllerStateTabItems.Find(cs => cs.ControllerState == item.ControllerState && cs.No == item.No);
            if (item2 != null)
            {
                controllerStateTabItems.Remove(item2);
            }
        }
        private void button_del_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            ControllerStateTabItemHeader controllerStateTabItemHeader = b.Tag as ControllerStateTabItemHeader;
            controllerStateTabItemHeaders.Remove(controllerStateTabItemHeader);


            //删除实际数据
            Remove(controllerStateTabItemHeader);
        }

        public class ControllerStateTabItemHeader
        {
            public string ControllerState { get; set; }

            public string Header { get; set; }

            public int No { get; set; }
        }
    }
}
