using System;
using CommonAIEditor.Plugins.Win32;
using CommonAI.Zone;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using CommonLang.Property;
using CommonAIServer.Node;
using CommonLang.Protocol;

namespace CommonAIEditor
{
    public static class EditorPlugin
    {
        private static IGameEditorPlugin mGameEditorPlugin;
        private static IGameEditorPlugin mDefaultGameEditorPlugin;

        public static string PluginDLL { get; private set; }
        public static string ServerDLL { get; private set; }

        public static void Init()
        {
            InitPlugins();
            InitDataPath();
        }

        private static void InitPlugins()
        {

            string data_plugin = ConfigurationManager.AppSettings["data_plugin"] + "";
            string edit_plugin = ConfigurationManager.AppSettings["edit_plugin"] + "";
            string node_plugin = ConfigurationManager.AppSettings["node_plugin"] + "";
            //-------------------------------------------------------------------------------------
            try
            {
                if (!string.IsNullOrEmpty(data_plugin))
                {
                    EditorPlugin.PluginDLL = Application.StartupPath + "\\" + data_plugin;
                    TemplateManager.IsEditor = true;
                    var factory = ReflectionUtil.LoadInterfaceFromDLL<InstanceZoneFactory>(PluginDLL);
                    if (factory == null) throw new Exception(data_plugin);
                    TemplateManager.setFactory(factory);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("数据插件初始化失败 : " + data_plugin + "\n" + err.Message);
            }
            //-------------------------------------------------------------------------------------
            try
            {
                if (!string.IsNullOrEmpty(node_plugin))
                {
                    EditorPlugin.ServerDLL = Application.StartupPath + "\\" + node_plugin;
                    var factory = ReflectionUtil.LoadInterfaceFromDLL<ZoneNodeFactory>(ServerDLL);
                    if (factory == null) throw new Exception(node_plugin);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("服务器插件初始化失败 : " + node_plugin + "\n" + err.Message);
            }
            //-------------------------------------------------------------------------------------
            try
            {
                if (!string.IsNullOrEmpty(edit_plugin))
                {
                    mDefaultGameEditorPlugin = new Win32EditorGamePlugin();
                    try
                    {
                        Type type = ReflectionUtil.LoadTypeFromDLL<IGameEditorPlugin>(edit_plugin);
                        mGameEditorPlugin = (IGameEditorPlugin)ReflectionUtil.CreateInstance(type);
                        if (mGameEditorPlugin == null) throw new Exception(type.FullName);
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                        mGameEditorPlugin = new Win32EditorGamePlugin();
                    }
                    mGameEditorPlugin.PluginDLL = (PluginDLL);
                    mDefaultGameEditorPlugin.PluginDLL = (PluginDLL);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("编辑器插件初始化失败 : " + edit_plugin + "\n" + err.Message);
            }
            //-------------------------------------------------------------------------------------

        }

        private static void InitDataPath()
        {
            string data_root = ConfigurationManager.AppSettings["data_root"];

            Editor.SetDataRoot(Application.StartupPath);
            if (!string.IsNullOrEmpty(data_root) && Directory.Exists(Application.StartupPath + "\\" + data_root + "\\data"))
            {
                Editor.SetDataRoot(Path.GetFullPath(Application.StartupPath + "\\" + data_root));
            }
            else if (Directory.Exists(Application.StartupPath + "\\..\\data"))
            {
                Editor.SetDataRoot(Path.GetFullPath(Application.StartupPath + "\\.."));
            }
            MessageFactoryGenerator codec = TemplateManager.MessageCodec;
            File.WriteAllText(Editor.EditorRootDir + "/codec.txt", codec.ListAll());

            mGameEditorPlugin.DataRoot = (Editor.EditorRootDir);
            mDefaultGameEditorPlugin.DataRoot = (Editor.EditorRootDir);
        }

        public static IGameEditorPlugin CurrentPlugin
        {
            get { return mGameEditorPlugin; }
        }
        public static IGameEditorPlugin DefaultPlugin
        {
            get { return mDefaultGameEditorPlugin; }
        }

    }
}