
export default class ConfigManager {

    private configKeys:Map<string,any>=new Map<string,any>();
    private configMap:Map<string,Map<any,any>>=new Map<string,Map<any,any>>();
    private configList:Map<string,any[]>=new Map<string,any[]>();
    private objConfigMap:Map<string,any>=new Map<string,any>();

    private jsonMap:Map<string,any>=new Map<string,any>();
    private __jsonAssets:cc.JsonAsset[];

    Init(configs:cc.JsonAsset[]):void{
        this.__jsonAssets=configs;
    }

    /**
     * 注册配置表的主KEY
     * @param configName
     * @param key
     */
    Register(configName:string,key:any="id"):void{
        this.configKeys.set(configName,key);
    }

    /**
     * 配置解码
     */
    Decode():void{
        let json:any;
        let list:any[];
        let map:Map<any,any>;
        let configKey:any;
        let configValue:any;
        this.__jsonAssets.forEach(jsonAsset => {
            json=jsonAsset.json;
            this.jsonMap.set(jsonAsset.name,jsonAsset);
            if(this.configKeys.has(jsonAsset.name)){
                configKey=this.configKeys.get(jsonAsset.name);
            }else{
                configKey="id";
            }
            if(json instanceof Array){
                list=[];
                map=new Map<any,any>();
                for (let index = 0; index < json.length; index++) {
                    const item = json[index];
                    if(Object.prototype.hasOwnProperty.call(item, configKey)){
                        configValue=item[configKey];
                        map.set(configValue,item);
                        list.push(item);
                    }else{
                        //obj配置类型表
                        if(json.length==1){
                            this.objConfigMap.set(jsonAsset.name,item);
                        }else{
                            cc.log(jsonAsset.name+"主KEY不存在！");
                            break;
                        }
                    }
                }
            }
            if(list&&list.length){
                this.configList.set(jsonAsset.name,list);
            }
            if(map&&map.size>0){
                this.configMap.set(jsonAsset.name,map);
            }
        });
    }

    /**
     * 获取obj配置中的数据
     * @param configName 
     * @param key 
     */
    GetConfigObjValue(configName:string,key:string):any{
        if(this.objConfigMap.has(configName)==false){
            throw new Error("找不到配置："+configName);
        }
        let obj=this.objConfigMap.get(configName);
        if(Object.prototype.hasOwnProperty.call(obj,key)==false){
            throw new Error("配置表："+configName+"没有"+key+"属性!");
        }
        return obj[key];
    }

    /**
     * 获取配置表中的某一项
     * @param configName
     * @param key
     */
    GetConfigItem(configName:string,key:any):any{
        let map:Map<any,any>=this.configMap.get(configName);
        if(map){
            return map.get(key);   
        }
        return null;
    }

    /**
     * 获取配置列表
     * @param configName 
     */
    GetConfigList(configName:string):any[]{
        return this.configList.get(configName);
    }

    /**
     * 直接获得原始配置数据
     * @param configName 
     */
    GetConfig(configName:string):cc.JsonAsset{
        if(this.jsonMap.has(configName)==false){
            throw new Error("没找到配置数据："+configName);
        }
        return this.jsonMap.get(configName);
    }

    /**
     * 单例
     */
    private static instance:ConfigManager;
    public static get single():ConfigManager{
        if(this.instance==null){
            this.instance=new ConfigManager();
        }
        return this.instance;
    }
}
