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>
    /// Page_MainEditDynArea.xaml 的交互逻辑
    /// </summary>
    public partial class Page_MainEditDynArea : Page
    {
        UIModule.FLYLayout mLayout;
        FLYLayoutManager mManager;

        ObservableCollection<GraphComponentType> ToolboxItems;
        ObservableCollection<DynAreaComponent> Items;
        public Page_MainEditDynArea()
        {
            InitializeComponent();
        }
        public void Init(FLYLayoutManager manager)
        {
            mManager = manager;
            mLayout = manager.mLayout;
            ToolboxItems = GraphConverter.GetToolboxItems(mManager, false);
            Items = GraphConverter.GetDynAreaComponents(mLayout.DynAreaItems);

            itemsControl_toolbox.ItemsSource = ToolboxItems;
            UpdateDynArea();
        }

        void UpdateDynArea()
        {
            UpdateStackpanel(stackpanel_dynArea, Items);
            Items.CollectionChanged += Items_CollectionChanged;
            UpdateComponentTypeCount();
        }
        private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    {
                        foreach (DynAreaComponent c in e.NewItems)
                        {
                            GraphComponentType gct = c.Type;
                            gct.Count++;
                            ButtonComponentAdd(stackpanel_dynArea, c);
                        }
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    {
                        Button button = stackpanel_dynArea.Children[e.OldStartingIndex] as Button;
                        stackpanel_dynArea.Children.RemoveAt(e.OldStartingIndex);
                        stackpanel_dynArea.Children.Insert(e.NewStartingIndex, button);
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    {
                        foreach (DynAreaComponent c in e.OldItems)
                        {
                            GraphComponentType gct = c.Type;
                            gct.Count--;
                            Button button = c.mButton;
                            stackpanel_dynArea.Children.Remove(button);
                        }
                    }
                    break;
            }
        }

        void UpdateComponentTypeCount()
        {
            foreach (GraphComponentType type in ToolboxItems)
            {
                type.Count = Items.Count((c) => { return c.Type == type; });
            }
        }

        void UpdateStackpanel(StackPanel stackpanel, ObservableCollection<DynAreaComponent> components)
        {
            stackpanel.Children.Clear();

            //添加 控件
            for (int i = 0; i < components.Count(); i++)
            {
                DynAreaComponent c = components[i];
                //加入统计
                try
                {
                    GraphComponentType ct = ToolboxItems.First((_ct) =>
                    {
                        if (_ct.ID == c.TypeID)
                            return true;
                        else
                            return false;
                    });
                    //ct.Count++;
                    c.Type = ct;
                }
                catch
                {
                    //异常
                    continue;
                }

                ButtonComponentAdd(stackpanel, c);
            }
        }

        void ButtonComponentAdd(StackPanel stackpanel, DynAreaComponent c)
        {
            Button b = new Button();
            c.mButton = b;
            b.Name = "button_component";
            b.Style = this.Resources["ButtonStyle_component"] as Style;
            b.Background = c.Type.Background;
            b.Click += button_componentEdit_Click;
            b.DataContext = c;
            b.Tag = c;
            stackpanel.Children.Add(b);
        }

        private void button_componentDel_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            //向上找TabItem
            Button button = FLY.ControlLibrary.COMMON.GetParent<Button>(sender as DependencyObject, "button_component");
            if (button == null)
            {
                //异常
                return;
            }
            string strTipComponentDel = Application.Current.FindResource("strTipComponentDel") as string;
            if (FLY.ControlLibrary.MyMessageBox.Show(strTipComponentDel) == true)
            {
                DynAreaComponent c = button.Tag as DynAreaComponent;
                Items.Remove(c);
            }
        }

        private void button_ok_Click(object sender, RoutedEventArgs e)
        {
            GraphConverter.ToDynArea(Items, mLayout.DynAreaItems);
            mLayout.Save();
            mManager.MatchParam();
            string strTipRebootForExecute = Application.Current.FindResource("strTipRebootForExecute") as string;
            if (FLY.ControlLibrary.MyMessageBox.Show(strTipRebootForExecute) == true)
            {
                System.Windows.Forms.Application.Restart();
                Application.Current.Shutdown();
            }
            return;
        }

        private void button_componentNew_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            GraphComponentType gct = button.Tag as GraphComponentType;
            if (!gct.IsEnable)
            {
                string strTipCantAddAgain = Application.Current.FindResource("strTipCantAddAgain") as string;
                FLY.ControlLibrary.MyMessageBox.Show(strTipCantAddAgain);
                return;
            }
            DynAreaComponent c = new DynAreaComponent()
            {
                TypeID = gct.ID,
                Type = gct
            };
            Items.Add(c);
        }

        private void button_componentEdit_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            DynAreaComponent c = button.Tag as DynAreaComponent;

            int idx = Items.IndexOf(c);
            if (idx == -1)
            {
                //异常
                return;
            }
            Window_MainEditDynAreaComponent w = new Window_MainEditDynAreaComponent();
            w.Init(Items, idx);
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }
    }
}
