using CommonAI;
using CommonAI.Zone;
using CommonAI.Zone.ZoneEditor;
using CommonLang;
using CommonLang.Log;
using CommonLang.Protocol;
using System;
using System.IO;
using xmds_battle_server.JSGModule;
using XmdsServerNode.CheatingDeath;
using XmdsServerNode.Node.Interface;

namespace XmdsServerNode.Node
{
    public class ZoneNodeManager
    {
        public static ZoneNodeManager Instance { get; private set; }
        public static DirectoryInfo StartupPath { get; private set; }
        public static ZoneNodeConfig NodeConfig { get; private set; }
        public static InstanceZoneFactory ZoneFactory { get; private set; }
        public static MessageFactoryGenerator MessageFactory { get; private set; }
        public static EditorTemplates Templates { get; private set; }
        private Logger log = LoggerFactory.GetLogger("ZoneNodeManager");

        public IServerProxy ServerProxy { get; private set; }
        /// <summary>
        /// 全局初始化处
        /// </summary>
        /// <param name="startup_path"></param>
        /// <param name="mb2s2b"></param>
        /// <param name="dataPath"></param>
        virtual public void Init(string startup_path, IServerProxy mb2s2b, string dataPath = null)
        {
            Console.WriteLine("logger name : " + log.GetType().Name);
            Instance = this;
            ServerProxy = mb2s2b;
            StartupPath = new DirectoryInfo(startup_path);

            //XmdsDllInit.Init();
            log.Info("********************************************************");
            log.Info("# 加载配置文件");
            log.Info("********************************************************");
            {
                NodeConfig = new ZoneNodeConfig();				
				Properties zone_server_config = Properties.LoadFromResource(startup_path + "/zone_server_config.properties");
                log.Info(startup_path + "/zone_server_config.properties");
                foreach (string key in zone_server_config.Keys)
                {
                    log.Info(string.Format("  {0} = {1}", key, zone_server_config[key]));
                }
                zone_server_config.LoadFields(NodeConfig);
                if (dataPath != null)
                {
                    NodeConfig.GAME_DATA_ROOT_PATH = dataPath;
                }
                EditorTemplates.DEFAULT_LOAD_FROM_BIN = NodeConfig.GAME_DATA_DEFAULT_LOAD_FROM_BIN;
                log.Info(" 资源目录 : " + NodeConfig.GAME_DATA_ROOT_PATH);
				XmdsConstConfig.GAME_UPDATE_INTERVAL_MS = NodeConfig.GAME_UPDATE_INTERVAL_MS;

			}
            log.Info("********************************************************");
            //log.Info("# 初始化战斗编辑器扩展");
            //log.Info("********************************************************");
            {
                ZoneFactory = new ZoneNodeServerFactory(mb2s2b);
                TemplateManager.setFactory(ZoneFactory);
                //log.Info(" 战斗编辑器插件 --> " + ZoneFactory);
            }
            //log.Info("********************************************************");
            //log.Info("# 初始化消息编解码器");
            //log.Info("********************************************************");
            {
                MessageFactory = TemplateManager.MessageCodec;
                //log.Info("\r\n" + MessageFactory.ListAll("  "));
                File.WriteAllText(StartupPath + "\\codec.txt", MessageFactory.ListAll());
            }
            log.Info("********************************************************");
            log.Info("# 加载模板数据");
            log.Info("********************************************************");
            {
                Templates = new EditorTemplates(NodeConfig.GAME_DATA_ROOT_PATH, MessageFactory);
                Templates.LoadAllTemplates();
                TemplateManager.Formula.BindLogger(LoggerFactory.GetDefLogger());
            }
            log.Info("********************************************************");
            log.Info("# 初始化防作弊");
            log.Info("********************************************************");
            {
                Properties cd_config = Properties.LoadFromResource(startup_path + "/cheating_death_config.properties");
                log.Info(startup_path + "/cheating_death_config.properties");
                CheatingDeathManager.Init(cd_config);
            }
            int out_io_tc, out_wk_tc;
            System.Threading.ThreadPool.GetMaxThreads(out out_wk_tc, out out_io_tc);
            System.Threading.ThreadPool.SetMaxThreads(NodeConfig.SYSTEM_WORK_THREAD_COUNT, out_io_tc);
            log.Info("********************************************************");
            log.Info("初始化完成");

            //反射notifyMessage
            R2bNotify.R2bNotifyManager.Init();

			//检测单位
			//CheckConfigModule.CheckActiveSkillAttackRange(Templates);
			//Templates.CheckScenePosInfo();
		}

        /// <summary>
        /// 关闭服务
        /// </summary>
        public virtual void Shutdown()
        {
        }




    }
}