using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;

namespace FLY.UI.Module
{
    public class UIModuleParam
    {
        public string dll_filename;
        public string assembly_name;
        public string class_name;
        /// <summary>
        /// 动态区
        /// </summary>
        public bool IsDynAreaComponent;
        /// <summary>
        /// 图表区
        /// </summary>
        public bool IsGraphCompnonent;
        /// <summary>
        /// 参数设置页
        /// </summary>
        public bool IsMenuCompnonent;
        /// <summary>
        /// 只能有一个
        /// </summary>
        public bool IsUnique;
        public static List<UIModuleParam> Load(string path)
        {
            List<UIModuleParam> mplist = null;
            try
            {
                using (Stream fStream = new FileStream(path, FileMode.Open))
                {
                    XmlSerializer xmlFormat = new XmlSerializer(typeof(List<UIModuleParam>));//创建XML序列化器，需要指定对象的类型
                    mplist = (List<UIModuleParam>)xmlFormat.Deserialize(fStream);
                    fStream.Dispose();//关闭文件
                    return mplist;
                }
            }
            catch
            {
                return null;
            }
        }
        public static void Save(string path, List<UIModuleParam> mplist)
        {
            using (
             Stream fStream = new FileStream(path, FileMode.Create))
            {
                XmlSerializer xmlFormat = new XmlSerializer(typeof(List<UIModuleParam>));//创建XML序列化器，需要指定对象的类型
                xmlFormat.Serialize(fStream, mplist);
                fStream.Dispose();//关闭文件
            }
        }
        public static void Test()
        {
            FLY.UI.Module.UIModuleParam module1 = new FLY.UI.Module.UIModuleParam()
            {
                dll_filename = "FLY.FeedbackRenZiJia.UI.Client.dll",
                assembly_name = "FLY.FeedbackRenZiJia.UI.Client.UIModule",
                class_name = "UIModule_GraphFB",
                IsDynAreaComponent = false,
                IsGraphCompnonent = true,
                IsUnique = true
            };
            FLY.UI.Module.UIModuleParam module2 = new FLY.UI.Module.UIModuleParam()
            {
                dll_filename = "FLY.FeedbackRenZiJia.UI.Client.dll",
                assembly_name = "FLY.FeedbackRenZiJia.UI.Client.UIModule",
                class_name = "UIModule_DynAreaFB",
                IsDynAreaComponent = true,
                IsGraphCompnonent = false,
                IsUnique = true
            };

            FLY.UI.Module.UIModuleParam.Save("test_module.xml", new List<FLY.UI.Module.UIModuleParam>(
                new FLY.UI.Module.UIModuleParam[] { module1, module2 })
                );

            //LoadModule lmodule = new LoadModule();
            //lmodule.Load("test_module.xml");
        }
    }

    public class LoadUIModule
    {

        /// <summary>
        /// 赋值一个文件夹路径，文件夹下面就是每个模块文件夹
        /// 模块文件夹下面应该包含 module.xml 文件
        /// 里面标示着 IUIModule 所在 的dll 文件, 程序集名，类名
        /// </summary>
        /// <param name="path"></param>
        public void Load(string path)
        {
            DirectoryInfo dinfo = new DirectoryInfo(path);
            UIModules.Clear();
            UIModuleGroups.Clear();

            if (!dinfo.Exists)
                return;
            if (dinfo.GetDirectories().Count() <= 0)
                return;

            foreach (DirectoryInfo di in dinfo.GetDirectories())
            {
                string module_param_path = di.FullName + @"\module.xml";

                List<UIModuleParam> mplist = UIModuleParam.Load(module_param_path);
                if (mplist == null)
                    continue;
                UIModuleGroup group = new UIModuleGroup();
                
                foreach (UIModuleParam mp in mplist)
                {
                    Assembly assembly = Assembly.LoadFrom(mp.dll_filename);
                    Type type = assembly.GetType(mp.assembly_name + "." + mp.class_name);
                    if (type == null)
                        continue;
                    string module_name = mp.assembly_name + "." + mp.class_name;
                    var uimodule = Activator.CreateInstance(type) as IUIModule;
                    if (uimodule == null)
                    {
                        //加载失败
                        continue;
                    }
                    UIModules.Add(
                        module_name,
                        new UIModuleInfo()
                        {
                            Param = mp,
                            UIModule = uimodule
                        });

                    group.ModuleNames.Add(module_name);
                }

                if (group.ModuleNames.Count() > 0)
                {
                    UIModuleGroups.Add(di.Name,group);
                }
            }
        }

        public Dictionary<string, UIModuleInfo> UIModules = new Dictionary<string, UIModuleInfo>();
        /// <summary>
        /// 模块组，代表一个 module.xml 文件
        /// </summary>
        public Dictionary<string, UIModuleGroup> UIModuleGroups = new Dictionary<string, UIModuleGroup>();
    }
    /// <summary>
    /// 模块描述
    /// </summary>
    public class UIModuleInfo
    {
        public UIModuleParam Param;
        public IUIModule UIModule;
        /// <summary>
        /// 这个模块，被界面创建了多少个
        /// </summary>
        public int BuildCnt;
    }
    /// <summary>
    /// 模块组
    /// </summary>
    public class UIModuleGroup
    {
        public List<string> ModuleNames = new List<string>();
    }
}
