import { JsonAsset } 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 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>();

    constructor(){

    }

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

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

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

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

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

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

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

    /**
     * 获取关卡配置
     * @param id 
     */
    public static GetLevelConfig(id:number):any{
        return this.GetConfig("Level"+id);
    }

    /**
     * 获取怪物模板配置
     * @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);
    }

    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[]{
        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.id=element.id+"_"+index;
                    monsterConfig.monsterId=element.monsterId;
                    monsterConfig.createTime=time;
                    monsterConfig.hp=element.hp;
                    monsterConfig.damage=element.damage;
                    monsterConfig.anger=element.anger;
                    if(element.startPosType==1){
                        let r:number=Math.random()*9;
                        if(r<3){
                            monsterConfig.startPos=0;
                        }if(r<6){
                            monsterConfig.startPos=1;
                        }else{
                            monsterConfig.startPos=2;
                        }
                    }else{
                        monsterConfig.startPos=element.startPos;
                    }
                    time+=element.intervalTime;
                    monsters.push(monsterConfig);
                }
            }
        });
        return monsters;
    }
}