using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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 ThickTcpUiInWindow.MainEdit
{
    /// <summary>
    /// Window_MainSet.xaml 的交互逻辑
    /// </summary>
    public partial class WdMainConfig : FLY.ControlLibrary.WindowBigClose
    {
        ObservableCollection<ControllerStateTabItemHeader> mControllerStates = new ObservableCollection<ControllerStateTabItemHeader>();

        FLYLayoutManager mManager;
        public WdMainConfig()
        {
            InitializeComponent();
            
        }
        public void Init(FLYLayoutManager manager)
        {
            mManager = manager;
            this.DataContext = mManager.mLayout;
            InitControllerstate();
        }

        private void button_save_Click(object sender, RoutedEventArgs e)
        {
            ToControllerStates();
            mManager.mLayout.Save();
            this.Close();
        }

        //private void button_mainedit_Click(object sender, RoutedEventArgs e)
        //{
        //    this.Close();
        //    NavigationService navigationService = Application.Current.Properties["NavigationService"] as
        //        NavigationService;

        //    Page_MainEdit p = new Page_MainEdit();
        //    p.Init(mManager);
        //    navigationService.Navigate(p);
        //}

        private void button_marksize_Click(object sender, RoutedEventArgs e)
        {
            UIModule.FLYLayout mLayout = mManager.mLayout;
            mLayout.Left = (int)Application.Current.MainWindow.Left;
            mLayout.Top = (int)Application.Current.MainWindow.Top;
            mLayout.Width = (int)Application.Current.MainWindow.Width;
            mLayout.Height = (int)Application.Current.MainWindow.Height;
            mLayout.WindowState = Application.Current.MainWindow.WindowState;
            mLayout.Save();
            FLY.ControlLibrary.MyMessageBox.Show("保存位置尺寸成功!");
        }


        public class ControllerStateTabItemHeader
        {
            public string ControllerState
            {
                get;set;
            }
            public string Header
            {
                get;set;
            }
            public int No
            {
                get;set;
            }
        }

        void InitControllerstate()
        {
            List<UIModule.ControllerStateTabItem> ControllerStates = mManager.mLayout.ControllerStates;
            mControllerStates.Clear();
            for (int i = 0; i < ControllerStates.Count(); i++)
            {
                ControllerStateTabItemHeader csh = new ControllerStateTabItemHeader();
                csh.ControllerState = ControllerStates[i].ControllerState;
                if (ControllerStates[i].No < mManager.mLayout.Items.Count() && ControllerStates[i].No >= 0)
                {
                    csh.No = ControllerStates[i].No;
                    csh.Header = mManager.mLayout.Items[ControllerStates[i].No].Header;
                }
                else
                {
                    continue;
                }
                mControllerStates.Add(csh);
            }
            this.itemsControl_controllerState.ItemsSource = mControllerStates;

            combobox_controllerstate.ItemsSource = new List<string>(new string[] { "FIX", "SCAN", "SYNC" });
            combobox_tabitemheader.ItemsSource = from i in mManager.mLayout.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 mControllerStates
                    where cs.ControllerState == combobox_controllerstate.SelectedItem as string
                    select cs;
            if (v.Count() > 0)
            {
                FLY.ControlLibrary.MyMessageBox.Show("已经存在,请删除后再添加");
                return;
            }


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

        private void button_del_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            ControllerStateTabItemHeader sch = b.Tag as ControllerStateTabItemHeader;
            mControllerStates.Remove(sch);
        }

        void ToControllerStates()
        {
            mManager.mLayout.ControllerStates.Clear();
            for (int i = 0; i < mControllerStates.Count(); i++)
            {
                ControllerStateTabItemHeader csh = mControllerStates[i];
                UIModule.ControllerStateTabItem cst = new UIModule.ControllerStateTabItem();
                cst.ControllerState = csh.ControllerState;
                cst.No = csh.No;
                mManager.mLayout.ControllerStates.Add(cst);
            }
        }
    }


}
