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;
using MultiLayout;
using MultiLayout.UiModule;

namespace MultiLayout.MainEdit
{
    /// <summary>
    /// Page_MainEditDynArea.xaml 的交互逻辑
    /// </summary>
    public partial class PgMainEditDynArea : Page
    {
        FlyLayout layout;
        FlyLayoutManager manager;
        GageTabItem gageTabItem;

        ObservableCollection<MeComponentType> ToolboxItems;
        ObservableCollection<MeFlyComponent> Items;
        public PgMainEditDynArea()
        {
            InitializeComponent();
        }
        public void Init(GageTabItem gageTabItem, FlyLayoutManager manager)
        {
            this.manager = manager;
            layout = manager.layout;
            this.gageTabItem = gageTabItem;
            this.DataContext = gageTabItem;
            ToolboxItems = GraphConverter.GetToolboxItems(this.manager, ComponentType.DynArea);
            Items = GraphConverter.GetMeFlyComponents(gageTabItem, gageTabItem.DynAreaItems, ToolboxItems);

            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 (MeFlyComponent c in e.NewItems)
                        {
                            var 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 (MeFlyComponent c in e.OldItems)
                        {
                            var gct = c.Type;
                            gct.Count--;
                            Button button = c.mButton;
                            stackpanel_dynArea.Children.Remove(button);
                        }
                    }
                    break;
            }
        }

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

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

            //添加 控件
            for (int i = 0; i < components.Count(); i++)
            {
                var c = components[i];
                ButtonComponentAdd(stackpanel, c);
            }
        }

        void ButtonComponentAdd(StackPanel stackpanel, MeFlyComponent 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("组件将要被删除,是否确定?") == true)
            {
                MeFlyComponent c = button.Tag as MeFlyComponent;
                Items.Remove(c);
            }
        }

        private void button_componentNew_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            var gct = button.Tag as MeComponentType;
            if (!gct.IsEnable)
            {
                string strTipCantAddAgain = "不能再次添加";
                FLY.ControlLibrary.MyMessageBox.Show(strTipCantAddAgain);
                return;
            }
            var c = new MeFlyComponent()
            {
                Type = gct,
                ServiceContainerName = gageTabItem.ServiceContainerName
            };
            Items.Add(c);
        }

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

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

        private void btnBackClick(object sender, RoutedEventArgs e)
        {
            GraphConverter.ToFlyComponents(Items, gageTabItem, gageTabItem.DynAreaItems);
            NavigationService.GoBack();
        }
    }
}
