using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace MultiLayout.MainEdit
{
    /// <summary>
    /// PgMainEditGage.xaml 的交互逻辑
    /// </summary>
    public partial class PgMainEditGage : Page
    {
        FlyLayoutManager manager;
        FlyLayout layout;
        ObservableCollection<GageTabItem> Items = new ObservableCollection<GageTabItem>();

        public PgMainEditGage()
        {
            InitializeComponent();
        }

        public void Init(FlyLayoutManager manager)
        {
            this.manager = manager;
            layout = this.manager.layout;
            if (layout.GageTabItems == null)
            {
                //完成没有设备

            }
            else
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(layout.GageTabItems);
                var gageTabItems = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GageTabItem>>(json);
                foreach (var gageTabItem in gageTabItems)
                {
                    Items.Add(gageTabItem);
                }
            }
            UpdateTabControl();
        }
        void UpdateTabControl()
        {
            tabcontrol.Items.Clear();
            for (int i = 0; i < Items.Count(); i++)
            {
                TabControlAdd(Items[i]);
            }
            Items.CollectionChanged += Items_CollectionChanged;
        }
        private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    {
                        foreach (GageTabItem item in e.NewItems)
                        {
                            TabControlAdd(item);
                        }
                        tabcontrol.SelectedIndex = tabcontrol.Items.Count - 1;
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    {
                        TabItem ti_old = tabcontrol.Items[e.OldStartingIndex] as TabItem;
                        if (tabcontrol.SelectedItem == ti_old)
                        {
                            tabcontrol.Items.RemoveAt(e.OldStartingIndex);
                            tabcontrol.Items.Insert(e.NewStartingIndex, ti_old);
                            tabcontrol.SelectedItem = ti_old;
                        }
                        else
                        {
                            tabcontrol.Items.RemoveAt(e.OldStartingIndex);
                            tabcontrol.Items.Insert(e.NewStartingIndex, ti_old);
                        }

                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    {
                        tabcontrol.Items.RemoveAt(e.OldStartingIndex);
                    }
                    break;
            }
        }

        void TabControlAdd(GageTabItem item)
        {
            TabItem ti = new TabItem()
            {
                Tag = item
            };
            
            ti.SetBinding(TabItem.HeaderProperty, new Binding("Title") { Source = item });
            UcMeGageTabItem uc = new UcMeGageTabItem();
            uc.Init(item, manager);
            ti.Content = uc;

            tabcontrol.Items.Add(ti);
        }
        private void button_ok_Click(object sender, RoutedEventArgs e)
        {
            string strTipRebootForExecute = "需要重启，才能生效";
            if (FLY.ControlLibrary.MyMessageBox.Show(strTipRebootForExecute) == true)
            {
                //保存！！！！
                layout.GageTabItems = Items.ToList();
                layout.Save();
                manager.MatchParam();

                System.Windows.Forms.Application.Restart();
                Application.Current.Shutdown();
            }
            return;
        }

        private void button_itemAdd_Click(object sender, RoutedEventArgs e)
        {
            GageTabItem item = new GageTabItem();
            Items.Add(item);
        }

        private void button_itemEdit_Click(object sender, RoutedEventArgs e)
        {
            //向上找TabItem
            TabItem tabitem = FLY.ControlLibrary.COMMON.GetParent<TabItem>(sender as DependencyObject);
            if (tabitem == null)
            {
                //异常
                return;
            }
            int idx = Items.IndexOf(tabitem.Tag as GageTabItem);
            if (idx < 0)
            {
                //异常
                return;
            }

            WdGageTabItemConfig w = new WdGageTabItemConfig();
            w.Init(Items, idx, manager.serviceNames);
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }

        private void button_itemDel_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            //向上找TabItem
            TabItem tabitem = FLY.ControlLibrary.COMMON.GetParent<TabItem>(sender as DependencyObject);
            if (tabitem == null)
            {
                //异常
                return;
            }
            int idx = Items.IndexOf(tabitem.Tag as GageTabItem);
            if (idx < 0)
            {
                //异常
                return;
            }
            //提示是否删除
            string strTipTabItemDel = "设备页面将要被删除, 是否确定？";
            if (FLY.ControlLibrary.MyMessageBox.Show(strTipTabItemDel) == true)
            {
                Items.RemoveAt(idx);
            }
        }

        private void btnBackClick(object sender, RoutedEventArgs e)
        {

            if (FLY.ControlLibrary.MyMessageBox.Show("将要退出编辑,是否保存,然后重启生效?") == true)
            {
                //保存！！！！
                layout.GageTabItems = Items.ToList();
                layout.Save();
                manager.MatchParam();

                System.Windows.Forms.Application.Restart();
                Application.Current.Shutdown();
            }
            else 
            {
                NavigationService.GoBack();
            }
        }
    }

    
}
