using FLY.UI.Module;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace ThickTcpUiInWindow
{
    public class FLYLayoutManager
    {
        /// <summary>
        /// lmodule 本地与第3方模块
        /// </summary>
        public FLY.UI.Module.LoadUIModule lmodule = new FLY.UI.Module.LoadUIModule();
        /// <summary>
        /// 本地模块
        /// </summary>
        public Dictionary<string, UIModuleInfo> myUIModules = new Dictionary<string, UIModuleInfo>();

        public UIModule.FLYLayout mLayout;
        protected TabControl tabControl;
        protected StackPanel stackpanel_dynarea;
        protected StackPanel stackpanel_menu;

        /// <summary>
        /// 
        /// </summary>
        public FLYLayoutManager()
        {

        }

        /// <summary>
        /// 加载模块
        /// </summary>
        public void LoadModule()
        {
            lmodule.Load("main_module");


            foreach (KeyValuePair<string, UIModuleInfo> kv in myUIModules)
            {
                lmodule.UIModules.Add(kv.Key, kv.Value);
            }

            //把不支持的模块删除
            mLayout.DynAreaItems.RemoveAll(c => !lmodule.UIModules.ContainsKey(c.Module));
            foreach (var tabitem in mLayout.Items)
            {
                tabitem.Graphs.RemoveAll(c => !lmodule.UIModules.ContainsKey(c.Module));
            }




            InitializeComponent_bulkgraph();
            InitializeComponent_dynarea();
            InitializeComponent_menu();
            //加载尺寸，位置
            int SH = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            int SW = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;

            if ((mLayout.Left < 0) || (mLayout.Left >= SW))
                mLayout.Left = 0;

            if ((mLayout.Top < 0) || (mLayout.Top >= SH))
                mLayout.Top = 0;

            if ((mLayout.Width > SW))
                mLayout.Width = SW;

            if ((mLayout.Height > SH))
                mLayout.Height = SH;

            Application.Current.MainWindow.Left = mLayout.Left;
            Application.Current.MainWindow.Top = mLayout.Top;
            Application.Current.MainWindow.Width = mLayout.Width;
            Application.Current.MainWindow.Height = mLayout.Height;
            Application.Current.MainWindow.WindowState = mLayout.WindowState;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tabControl"></param>
        /// <param name="stackpanel_dynarea"></param>
        /// <param name="stackpanel_menu"></param>
        /// <param name="layout"></param>
        public void Init(TabControl tabControl, StackPanel stackpanel_dynarea, StackPanel stackpanel_menu,
            UIModule.FLYLayout layout)
        {

            this.mLayout = layout;


            this.tabControl = tabControl;
            this.stackpanel_dynarea = stackpanel_dynarea;
            this.stackpanel_menu = stackpanel_menu;
        }

        void InitializeComponent_bulkgraph()
        {
            foreach (UIModule.GraphTabItem item in mLayout.Items)
            {
                //grid
                Grid grid = new Grid();
                for (int i = 0; i < item.ColumnCount; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    if (item.ColumnDefinitions.Count() > i)
                    {
                        if (item.ColumnDefinitions[i])
                            cd.Width = GridLength.Auto;
                    }
                    grid.ColumnDefinitions.Add(cd);
                }
                for (int i = 0; i < item.RowCount; i++)
                {
                    RowDefinition rd = new RowDefinition();
                    if (item.RowDefinitions.Count() > i)
                    {
                        if (item.RowDefinitions[i])
                            rd.Height = GridLength.Auto;
                    }
                    grid.RowDefinitions.Add(rd);
                }

                //graph
                foreach (UIModule.GraphComponent gp in item.Graphs)
                {
                    if (!lmodule.UIModules.Keys.Contains(gp.Module))
                        continue;
                    UIModuleInfo info = lmodule.UIModules[gp.Module];
                    
                    Frame frame = new Frame();
                    frame.SetValue(Frame.MarginProperty, new Thickness(5));

                    frame.Content = info.UIModule.GetComponent(gp.ID);
                    info.BuildCnt++;

                    frame.DataContext = gp;
                    frame.SetBinding(Grid.ColumnProperty, new Binding("Column"));
                    frame.SetBinding(Grid.RowProperty, new Binding("Row"));
                    frame.SetBinding(Grid.ColumnSpanProperty, new Binding("ColumnSpan"));
                    frame.SetBinding(Grid.RowSpanProperty, new Binding("RowSpan"));
                    grid.Children.Add(frame);
                }
                TabItem tabitem = new TabItem()
                {
                    Header = item.Header
                };
                tabitem.Content = grid;
                this.tabControl.Items.Add(tabitem);
            }
        }

        void InitializeComponent_dynarea()
        {
            foreach (UIModule.DynAreaComponent item in mLayout.DynAreaItems)
            {
                if (!lmodule.UIModules.Keys.Contains(item.Module))
                    continue;
                UIModuleInfo info = lmodule.UIModules[item.Module];
                
                Frame frame = new Frame();
                frame.Content = info.UIModule.GetComponent(item.ID);
                info.BuildCnt++;

                stackpanel_dynarea.Children.Add(frame);
            }
        }

        void InitializeComponent_menu()
        {
            foreach (var kv in lmodule.UIModuleGroups)
            {
                //统计模块组内的模块被创建的个数
                int c = (from u in lmodule.UIModules
                         where kv.Value.ModuleNames.Contains(u.Key) && u.Value.BuildCnt > 0
                         select u).Count();
                if (c > 0)
                {
                    //找到模块组内 menu 模块，添加到 mLayout.MenuItems
                    mLayout.MenuItems.AddRange (
                        from u in lmodule.UIModules
                        where kv.Value.ModuleNames.Contains(u.Key) && u.Value.Param.IsMenuCompnonent
                        select new UIModule.MenuComponent() { Module = u.Key });
                }
            }
            
            foreach (UIModule.MenuComponent item in mLayout.MenuItems)
            {
                if (!lmodule.UIModules.Keys.Contains(item.Module))
                    continue;
                FLY.UI.Module.IUIModule pm = lmodule.UIModules[item.Module].UIModule;
                Frame frame = new Frame();
                frame.Content = pm.GetComponent(item.ID);
                stackpanel_menu.Children.Add(frame);
            }
        }

        /// <summary>
        /// 通知全部模块，告诉它们现在正在使用的控件，保存多余的参数就删除。
        /// </summary>
        public void MatchParam()
        {
            List<UIModule.FLYComponent> components = new List<UIModule.FLYComponent>();

            foreach (UIModule.GraphTabItem item in mLayout.Items)
                components.AddRange(item.Graphs);
            
            components.AddRange(mLayout.DynAreaItems);
            
            Dictionary<string, List<int>> modules = new Dictionary<string, List<int>>();
            foreach (UIModule.FLYComponent c in components)
            {
                if (modules.ContainsKey(c.Module))
                     modules[c.Module].Add(c.ID);
                else
                    modules.Add(c.Module, new List<int>(new int[] { c.ID }));
            }

            foreach (var m in modules)
            {
                
                FLY.UI.Module.IUIModule pm = lmodule.UIModules[m.Key].UIModule;
                pm.MatchParam(m.Value.ToArray());
                
            }
        }
    }



}
