using CommonLang;
using CommonLang.Property;
using CommonLang.Xml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XmdsBotTest.Runner;

namespace XmdsBotTest
{
    public class AddBotConfig
    {
        [Desc("前缀", "Add")]
        public string name_format = "ai_{0}";
        [Desc("登录密码", "Add")]
        public string password = "123456";
        [Desc("起始ID", "Add")]
        public int index = 0;
        [Desc("数量", "Add")]
        public int count = 10;
        [Desc("名字格式", "Add")]
        public string digit_format = "D5";      

        //-------------------------------------------------------------------------------------

        [Desc("随机角色名", "角色")]
        public bool RandomRoleName = true;
        [Desc("角色名格式", "角色")]
        [DependOnProperty("RandomRoleName", false)]
        public string[] RoleNameFormat = new string[] { "角色{0}" };

        //-------------------------------------------------------------------------------------

        [Desc("道具配置", "模块")]
        public ModuleInventory.Config Inventory = new ModuleInventory.Config();
        [Desc("任务配置", "模块")]
        public ModuleTask.Config Task = new ModuleTask.Config();
        [Desc("重连配置", "模块")]
        public ModuleReconnect.Config Reconnect = new ModuleReconnect.Config();
        [Desc("坐骑配置", "模块")]
        public ModuleMount.Config mount = new ModuleMount.Config();
        [Desc("装备配置", "模块")]
        public ModuleEquip.Config equip = new ModuleEquip.Config();
        [Desc("技能配置", "模块")]
        public ModuleSkill.Config skill = new ModuleSkill.Config();
        [Desc("公会配置", "模块")]
        public ModuleGuild.Config guild = new ModuleGuild.Config();
        [Desc("排行榜配置", "模块")]
        public RankWarn.Config rank = new RankWarn.Config();



        // 功能
//        [Desc("问道大会配置", "功能")]
//        public SoloFunc.Config solo = new SoloFunc.Config();
//        [Desc("五岳一战配置", "功能")]
//        public AreanFunc.Config arean = new AreanFunc.Config();
//        [Desc("试炼大赛配置", "功能")]
//        public Five2FiveFunc.Config five2five = new Five2FiveFunc.Config();
////         [Desc("师门任务配置", "功能")]
////         public MasterTaskFunc.Config masterTask = new MasterTaskFunc.Config();
//        [Desc("组队副本配置", "功能")]
//        public TeamDungeonFunc.Config teamDungeon = new TeamDungeonFunc.Config();

        // 协议
        //[Desc("商城购买玉神石配置", "协议")]
        //public BuyMallProto.Config buyMallProto = new BuyMallProto.Config();
        //[Desc("积分商城配置", "协议")]
        //public BuyIntergalItemProto.Config buyIntergalProto= new BuyIntergalItemProto.Config();
        //[Desc("寄卖行寄卖物品配置", "协议")]
        //public AutionProto.Config autionProto = new AutionProto.Config();


        // 警告
        [Desc("充值订单配置", "充值")]
        public PrepaidOrderIdWarn.Config prepaidOrderId = new PrepaidOrderIdWarn.Config();

        //-------------------------------------------------------------------------------------

        public static AddBotConfig TryLoadAddConfig()
        {
            var add = new AddBotConfig();
            try
            {
                var saved = XmlUtil.LoadXML(Application.StartupPath + "/bot_add.xml");
                if (saved != null)
                {
                    add = XmlUtil.XmlToObject<AddBotConfig>(saved);
                }
                LoadProp("ModuleInventory", typeof(ModuleInventory.Config));
                LoadProp("ModuleTask", typeof(ModuleTask.Config));
                LoadProp("ModuleReconnect", typeof(ModuleReconnect.Config));
                LoadProp("ModuleMount", typeof(ModuleMount.Config));
                LoadProp("ModuleEquip", typeof(ModuleEquip.Config));
                LoadProp("ModuleSkill", typeof(ModuleSkill.Config));
                LoadProp("ModuleGuild", typeof(ModuleGuild.Config));
                LoadProp("ModuleRank", typeof(RankWarn.Config));

                // 功能
                LoadProp("SoloFunc", typeof(SoloFunc.Config));   // 
                LoadProp("AreanFunc", typeof(AreanFunc.Config));
                LoadProp("Five2FiveFunc", typeof(Five2FiveFunc.Config));
                //LoadProp("MasterTaskFunc", typeof(MasterTaskFunc.Config));
                LoadProp("TeamDungeonFunc", typeof(TeamDungeonFunc.Config));

                // 协议
                LoadProp("BuyMallProto",typeof(BuyMallProto.Config));
                LoadProp("BuyIntergalItemProto", typeof(BuyIntergalItemProto.Config));
                LoadProp("AutionProto",typeof(AutionProto.Config));

                // 警告
                LoadProp("PrepaidOrderIdWarn", typeof(PrepaidOrderIdWarn.Config));
    }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            return add;
        }
        public static void TrySaveAddConfig(AddBotConfig add)
        {
            var save = XmlUtil.ObjectToXml(add);
            XmlUtil.SaveXML(Application.StartupPath + "/bot_add.xml", save);
            SaveProp("ModuleInventory", typeof(ModuleInventory.Config));
            SaveProp("ModuleTask", typeof(ModuleTask.Config));
            SaveProp("ModuleReconnect", typeof(ModuleReconnect.Config));
            SaveProp("ModuleMount", typeof(ModuleMount.Config));
            SaveProp("ModuleEquip", typeof(ModuleEquip.Config));
            SaveProp("ModuleSkill", typeof(ModuleSkill.Config));
            SaveProp("ModuleGuild", typeof(ModuleGuild.Config));
            SaveProp("ModuleRank", typeof(RankWarn.Config));

            // 功能
            SaveProp("SoloFunc", typeof(SoloFunc.Config));
            SaveProp("AreanFunc", typeof(AreanFunc.Config));
            SaveProp("Five2FiveFunc", typeof(Five2FiveFunc.Config));
            //SaveProp("MasterTaskFunc", typeof(MasterTaskFunc.Config));
            SaveProp("TeamDungeonFunc", typeof(TeamDungeonFunc.Config));

            // 协议
            SaveProp("BuyMallProto",typeof(BuyMallProto.Config));
            SaveProp("BuyIntergalItemProto",typeof(BuyIntergalItemProto.Config));
            SaveProp("AutionProto",typeof(AutionProto.Config));

            // 警告
            SaveProp("PrepaidOrderIdWarn",typeof(PrepaidOrderIdWarn.Config));
        }
        public static void SaveProp(string name, Type type)
        {
            Properties prop = new Properties();
            prop.SaveStaticFields(type);
            File.WriteAllText(Application.StartupPath + "/" + name + ".properties", prop.ToParseString(), CUtils.UTF8);
        }
        private static void LoadProp(string name, Type type)
        {
            if (File.Exists(Application.StartupPath + "/" + name + ".properties"))
            {
                var text = File.ReadAllText(Application.StartupPath + "/" + name + ".properties", CUtils.UTF8);
                if (text != null)
                {
                    Properties prop = new Properties();
                    prop.ParseText(text);
                    prop.LoadStaticFields(type);
                }
            }
        }

    }

    public class BotModuleConfig
    {
        [Desc("Modules")]
        public HashMap<string, bool> Modules = new HashMap<string, bool>();
    }

}