using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonAI.RTS.Manhattan;
using CommonAI.Zone;
using CommonAI.Zone.Helper;
using CommonAI.Zone.ZoneEditor;
using CommonAI.ZoneClient;
using XmdsBattleClient.Battle;
using XmdsCommon.Plugin;
using XmdsCommon.ZoneClient;
using CommonLang;
using CommonLang.IO;
using System.IO;

namespace XmdsBattleClientBot
{
    public static class BotClientManager
    {
        public static void Init(BotConfig cfg)
        {
            XmdsBattleManager.Init(cfg.DataRoot, new XmdsZoneFactoryBot());
            LoadTaskLua(cfg.LuaRoot);
            LoadItemLua(cfg.LuaRoot);
            LoadItemQualityLua(cfg.LuaRoot);
            //LoadDataLua(cfg.LuaRoot+ "\\data\\Section.lua");
            XmdsZoneFactoryBot.SaveMemory = cfg.SaveMemory;
        }
        //---------------------------------------------------------------------------------------------------------
        public static void LoadLuaTable<K>(HashMap<K, Dictionary<string, object>> dst, string file)
        {
            try
            {
                var text = Resource.LoadAllText(file);
                LuaInterface.Lua lua = new LuaInterface.Lua();
                LuaInterface.LuaTable src = lua.DoString(text)[0] as LuaInterface.LuaTable;
                LuaInterface.LuaTable keys = src["_key_"] as LuaInterface.LuaTable;
                foreach (var k in src.Keys)
                {
                    K id;
                    if (!k.ToString().Equals("_key_") && Parser.StringToObject<K>(k.ToString(), out id))
                    {
                        LuaInterface.LuaTable line = src[k] as LuaInterface.LuaTable;
                        Dictionary<string, object> fields = new Dictionary<string, object>();
                        foreach (var i in keys.Keys)
                        {
                            fields[keys[i].ToString()] = line[i].ToString();
                        }
                        dst[id] = fields;
                    }
                }
            }
            catch (Exception err)
            {
                throw new Exception("Load Lua Error : " + file + "\r\n" + err.Message, err);
            }
        }

        //---------------------------------------------------------------------------------------------------------
        #region Task
        private static HashMap<int, Dictionary<string, object>> s_AllTasksTemplate;
        private static void LoadTaskLua(string lua_root)
        {
            if (s_AllTasksTemplate == null)
            {
                s_AllTasksTemplate = new HashMap<int, Dictionary<string, object>>();
                foreach (var file in Resource.ListFiles(lua_root + @"/Data/Tasks/"))
                {
                    Console.WriteLine(lua_root + @"/Data/Tasks/" + file);
                    BotClientManager.LoadLuaTable(s_AllTasksTemplate, lua_root + @"/Data/Tasks/" + file);
                }
                //                 BotClientManager.LoadLuaTable(s_AllTasksTemplate, lua_root + @"/Data/Tasks/BranchLine.lua");
                //                 BotClientManager.LoadLuaTable(s_AllTasksTemplate, lua_root + @"/Data/Tasks/Daily.lua");
                //                 BotClientManager.LoadLuaTable(s_AllTasksTemplate, lua_root + @"/Data/Tasks/MainLine.lua");
            }
        }
        public static Dictionary<string, object> GetTaskTemplate(int tid)
        {
            return s_AllTasksTemplate.Get(tid);
        }
        #endregion   

        #region Item
        private static HashMap<string, Dictionary<string, object>> s_AllItemTemplate;
        private static void LoadItemLua(string lua_root)
        {
            if (s_AllItemTemplate == null)
            {
                s_AllItemTemplate = new HashMap<string, Dictionary<string, object>>();
                foreach (var file in Resource.ListFiles(lua_root + @"/Data/Items/"))
                {
                    Console.WriteLine(lua_root + @"/Data/Items/" + file);
                    BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/" + file);
                }
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/BlueEquip.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/BlueEquipU.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Book.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Chest.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/GreenEquip.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/GreenEquipU.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Jewel.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/LegendEquip.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/LegendEquipU.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Mate.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Misc.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Paper.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/PetBook.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/PetItem.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Potion.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/PurpleEquip.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/PurpleEquipU.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Rank.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Ride.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Task.lua");
                //                 BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/VipItem.lua");
            }
        }
        public static Dictionary<string, object> GetItemTemplate(string code)
        {
            return s_AllItemTemplate.Get(code);
        }
        #endregion

        #region ItemQualityConfig
        private static HashMap<int, Dictionary<string, object>> s_AllItemQualityTemplate;
        private static void LoadItemQualityLua(string lua_root)
        {
            if (s_AllItemQualityTemplate == null)
            {
                s_AllItemQualityTemplate = new HashMap<int, Dictionary<string, object>>();
                Console.WriteLine(lua_root + @"/Data/ItemQualityConfig.lua");
                BotClientManager.LoadLuaTable(s_AllItemQualityTemplate, lua_root + @"/Data/ItemQualityConfig.lua");
            }
        }
        public static Dictionary<string, object> GetItemQuality(int tid)
        {
            return s_AllItemQualityTemplate.Get(tid);
        }
        #endregion   
        //---------------------------------------------------------------------------------------------------------

        #region AllLuaConfig
        
        private static HashMap<string,HashMap<int, Dictionary<string, object>>> s_AllLuaConfig;
        private static void LoadDataLua(string lua_root)
        {
            if (s_AllLuaConfig == null)
            {
                s_AllLuaConfig = new HashMap<string, HashMap<int, Dictionary<string, object>>>();
                if (lua_root.EndsWith(".lua"))
                {
                    HashMap<int, Dictionary<string, object>> fileData = new HashMap<int, Dictionary<string, object>>();
                    Console.WriteLine("load luadata file name ---->>{0}", lua_root);
                    BotClientManager.LoadLuaTable(fileData, lua_root);
                    s_AllLuaConfig[lua_root.Substring(lua_root.LastIndexOf('\\') + 1)] = fileData;
                }
                else
                {
                    foreach (var file in Resource.ListFiles(lua_root))
                    {
                        HashMap<int, Dictionary<string, object>> fileData = new HashMap<int, Dictionary<string, object>>();
                        string fileName = file.ToString().Substring(1);
                        Console.WriteLine("load luadata file name ---->>{0}", fileName);
                        BotClientManager.LoadLuaTable(fileData, lua_root + file);
                        s_AllLuaConfig[fileName] = fileData;
                    }
                }
            }
        }

        public static HashMap<int, Dictionary<string, object>> GetLuaData(string fileName)
        {
            return s_AllLuaConfig.Get(fileName);
        }

        public static Dictionary<string, object> GetLuaData(string fileName,int tid)
        {
            HashMap<int, Dictionary<string, object>> fileData = GetLuaData(fileName);
            if (fileData != null) {
                return fileData.Get(tid);
            }
            return null;
        }
        #endregion   
    }
}