import { JsonAsset, __internal } from "cc";


export default class GameConfigManager
{
    private static jsonMap:Map<string,JsonAsset>=new Map<string,JsonAsset>();

    /**
     * 武器配置
     */
    private static WeaponMap:Map<number,any>=new Map<number,any>();
    private static WeaponLevelMap:Map<number,any>=new Map<number,any>();

    /**
     * 怪物模板配置
     */
    private static MonsterMap:Map<number,any>=new Map<number,any>();

    /**
     * 栅栏配置
     */
    private static FenceMap:Map<number,any>=new Map<number,any>();

    /**
     * 技能配置
     */
    private static SkillMap:Map<number,any>=new Map<number,any>();

    /**
     * 关卡
     */
    private static LevelMap:Map<string,any>=new Map<string,any>();

    /**
     * 商城配置
     */
    private static ShopMap:Map<number,any>=new Map<number,any>();

    /**
     * 商城配置
     */
    private static SignMap:Map<number,any>=new Map<number,any>();

    /**
     * 商城列表
     */
    public static ShopList:any[]=[];

    /**
     * 全局配置
     */
    private static GameGlobal:any;

    constructor(){

    }

    /**
     * 初始化
     * @param jsons
     */
    public static Init(jsons:JsonAsset[]):void{
        jsons.forEach(element => {
            jsons[element.name]=element;
            this.jsonMap.set(element.name,element);
        });
        this.InitGlobalConfig();
        this.InitWeaponConfig();
        this.InitMonsterConfig();
        this.InitFenceConfig();
        this.InitSkillConfig();
        this.InitLevelConfig();
        this.initShopConfig();
        this.InitSignConfig();
    }

    private static InitSignConfig():void{
        if(this.jsonMap.has("Sign")==false){
            return;
        }
        let jsonAsset:JsonAsset=this.GetConfig("Sign");
        let list:any=jsonAsset.json;
        list.forEach(element => {
            this.SignMap.set(element.id,element);
        });
    }

    private static InitGlobalConfig():void{
        if(this.jsonMap.has("GameGlobal")==false){
            return;
        }
        let jsonAsset:JsonAsset=this.GetConfig("GameGlobal");
        let list:any=jsonAsset.json;
        this.GameGlobal=list[0];
    }

    /**
     * 初始化
     */
    private static InitWeaponConfig():void{
        if(this.jsonMap.has("Weapons")==false){
            return;
        }
        let jsonAsset:JsonAsset=this.GetConfig("Weapons");
        let list:any=jsonAsset.json;
        list.forEach(element => {
            this.WeaponMap.set(element.id,element);
            this.WeaponLevelMap.set(element.level,element);
        });
    }

    private static InitMonsterConfig():void{
        if(this.jsonMap.has("Monsters")==false){
            return;
        }
        let jsonAsset:JsonAsset=this.GetConfig("Monsters");
        let list:any=jsonAsset.json;
        list.forEach(element => {
            this.MonsterMap.set(element.id,element);
        });
    }

    private static InitFenceConfig():void{
        if(this.jsonMap.has("Fence")==false){
            return;
        }
        let jsonAsset:JsonAsset=this.GetConfig("Fence");
        let list:any=jsonAsset.json;
        list.forEach(element => {
            this.FenceMap.set(element.id,element);
        });
    }

    private static InitSkillConfig():void{
        if(this.jsonMap.has("Skill")==false){
            return;
        }
        let jsonAsset:JsonAsset=this.GetConfig("Skill");
        let list:any=jsonAsset.json;
        list.forEach(element => {
            this.SkillMap.set(element.id,element);
        });
    }

    private static InitLevelConfig():void{
        if(this.jsonMap.has("Levels")==false){
            return;
        }
        let jsonAsset:JsonAsset=this.GetConfig("Levels");
        let list:any=jsonAsset.json;
        list.forEach(element => {
            this.LevelMap.set(element.id,element);
        });
    }

    private static initShopConfig():void{
        if(this.jsonMap.has("Shop")==false){
            return;
        }
        let jsonAsset:JsonAsset=this.GetConfig("Shop");
        let list:any=jsonAsset.json;
        list.forEach(element => {
            this.ShopMap.set(element.id,element);
            this.ShopList.push(element);
        });
    }

    /**
     * 获取商城配置
     * @param weaponId 
     */
    public static GetShopConfig(weaponId:number):any{
        if(this.ShopMap.has(weaponId)){
            return this.ShopMap.get(weaponId);
        }
        return null;
    }

    /**
     * 获取7日登录奖励
     * @param id 
     */
    public static GetSignConfig(id:number):any{
        if(this.SignMap.has(id)){
            return this.SignMap.get(id);
        }
        return null;
    }

    /**
     * 获取快速购买的武器ID
     * @param weaponId 
     */
    public static GetQuickBuyWeaponId(weaponId:number):number{
        if(this.ShopMap.has(weaponId)==false){
            throw new Error("找不到快速购买配置："+weaponId);
        }
        let config:any=this.ShopMap.get(weaponId);
        return config.quickId;
    }

    /**
     * 获取最大关卡数
     */
    public static get MaxLevel():number{
        return this.LevelMap.size;
    }

    /**
     * 获取配置数据
     * @param sheet
     * @param key
     */
    public static GetConfig(sheet:string):any{
        return this.jsonMap.get(sheet);
    }


    /**
     * 获取全局配置表中的值
     * @param key 
     */
    public static getGlobalValue(key:string):any{
        return this.GameGlobal[key];
    }

    /**
     * 获取武器ID
     * @param id 
     */
    public static GetWeaponConfig(id:number):any{
        if(this.WeaponMap.has(id)==false){
            console.error("找不到ID为："+id+"的武器！");
            return null;
        }
        return this.WeaponMap.get(id);
    }

    /**
     * 通过武器等级获取武器配置
     * @param level 
     */
    public static GetWeaponConfigByLevel(level:number):any{
        if(this.WeaponLevelMap.has(level)==false){
            return null;
        }
        return this.WeaponLevelMap.get(level);
    }

    /**
     * 获取下一级的武器ID
     * @param id 
     */
    public static GetNextLevelWeaponId(id:number):number{
        let weaponConfig:any=this.GetWeaponConfig(id);
        let nextLevel:number=weaponConfig.level+1;
        let nextConfig=this.GetWeaponConfigByLevel(nextLevel);
        return nextConfig.id;
    }

    /**
     * 武器是否已到达最高等级
     * @param id 
     */
    public static WeaponIsMaxLevel(id:number):boolean{
        let weaponConfig:any=this.GetWeaponConfig(id);
        let trylevel:number=weaponConfig.level+1;
        if(this.GetWeaponConfigByLevel(trylevel)==null){
            return true;
        }
        return false;
    }
    
    /**
     * 获取关卡配置
     * @param id 
     */
    public static GetLevelConfig(id:number):any{
        let strId:string="level"+id;
        if(this.LevelMap.has(strId)==false){
            throw new Error("找不到ID为："+strId+"的关卡配置！");
        }
        return this.LevelMap.get(strId);
    }

    /**
     * 获取怪物模板配置
     * @param id 
     */
    public static GetMonsterConfig(id:number):any{
        if(this.MonsterMap.has(id)==false){
            throw new Error("找不到ID为："+id+"的怪物！");
        }
        return this.MonsterMap.get(id);
    }
    
    public static GetFenceConfig(id:number):any{
        if(this.FenceMap.has(id)==false){
            throw new Error("找不到ID为："+id+"的栅栏！");
        }
        return this.FenceMap.get(id);
    }

    /**
     * 获取下一个栅栏ID
     * @param id 
     */
    public static GetNextFenceID(id:number):number{
        let jsonAsset:JsonAsset=GameConfigManager.GetConfig("Fence");
        let config:any=jsonAsset.json;
        let configList:any[]=config;
        for (let index = 0; index < configList.length; index++) {
            const element = configList[index];
            if(element.id>id){
                return element.id;
            }
        }
        return -1;
    }

    public static GetSkillConfig(id:number):any{
        if(this.SkillMap.has(id)==false){
            throw new Error("找不到ID为："+id+"的技能！");
        }
        return this.SkillMap.get(id);
    }

    /**
     * 通过怪物刷新规则构建怪物配置
     */
    public static CreateMonsterByList(config:any,currentTime:number):any[]{
        if(config==undefined||config==null||config.length==0){
            throw new Error("关卡配置中怪物列表为空!");
        }
        let monsters:any[]=[];
        let monsterConfig;
        let time:number=currentTime;
        config.forEach(element => {
            //单个配置
            if(element.type==undefined||element.type==0){
                monsters.push(element);
            }else if(element.type==1){
                if(element.count==0){
                    throw new Error("怪物数量不能为0");
                }
                for (let index = 0; index < element.count; index++) {
                    monsterConfig={};
                    monsterConfig.mId=element.mId+"_"+index;
                    monsterConfig.monsterId=element.monsterId;
                    monsterConfig.createTime=time;
                    monsterConfig.hp=element.hp;
                    monsterConfig.damage=element.damage;
                    monsterConfig.anger=element.anger;
                    monsterConfig.integral=element.integral;
                    if(element.startPosType==1){
                        let r:number=Math.random()*9;
                        if(r<3){
                            monsterConfig.startPos=0;
                        }else if(r<6){
                            monsterConfig.startPos=1;
                        }else{
                            monsterConfig.startPos=2;
                        }
                    }else{
                        monsterConfig.startPos=element.startPos;
                    }
                    time+=element.intervalTime;
                    monsters.push(monsterConfig);
                }
            }
        });
        return monsters;
    }
}