using MultiLayout.UiModule;
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 MultiLayout.MainEdit
{
    /// <summary>
    /// PgMeMenu.xaml 的交互逻辑
    /// </summary>
    public partial class PgMeMenu : Page
    {
        FlyLayout layout;
        FlyLayoutManager manager;
        GageTabItem gageTabItem;

        ObservableCollection<MeComponentType> ToolboxItems;
        ObservableCollection<MeFlyComponent> Items;
        public PgMeMenu()
        {
            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.Menu);
            Items = GraphConverter.GetMeFlyComponents(gageTabItem, gageTabItem.MenuItems, ToolboxItems);

            itemsControl_toolbox.ItemsSource = ToolboxItems;
            UpdateMenu();
        }

        void UpdateMenu()
        {
            UpdateStackpanel(spMenu, 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(spMenu, c);
                        }
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    {
                        Button button = spMenu.Children[e.OldStartingIndex] as Button;
                        spMenu.Children.RemoveAt(e.OldStartingIndex);
                        spMenu.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;
                            spMenu.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.Background = c.Type.Background;
            b.Click += button_componentEdit_Click;
            b.DataContext = c;
            b.Tag = c;
            stackpanel.Children.Add(b);
        }

        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;
            }
            WdMenuConfig w = new WdMenuConfig();
            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.MenuItems);
            NavigationService.GoBack();
        }
    }
}
