import GameController from "../GameController"
import { gameMethod } from "./gameMethod"
// type for attr.excel
export type xlsAttrAttr = {id:number,name:string,showName:string,attrDesc:string,numType:number,upper:number,icon:string,powerConversion:number,#:备注}
// type for audio.excel
export type xlsAudioInfo = {id:string,name:string,vol:number}
// type for chapter.excel
export type xlsChapterInfo = {id:number,grid:number[],map:string,name:string}
export type xlsChapterLayout = {id:number,infoId:number,grid:number,type:number,correlationId:number}
// type for drop.excel
// type for emitter.excel
export type xlsEmitterInfo = {id:number,name:string,icon:string,quality:number,type:number,upLimit:number,restoreInterval:number,restoreNum:number[],prop:number[],demand:number[],chargingType:number,chargingCost:number[],expiryTime:number}
// type for equip.excel
export type xlsEquipInfo = {id:number,name:string,icon:string,desc:string,quality:number}
// type for fish.excel
// type for guide.excel
export type xlsGuideInfo = {id:string,event:string,way:string,variable:number[],ytype:number,condition:number[],type:number,pram:any[],pram1:any[]}
// type for help.excel
// type for item.excel
export type xlsItemKind = {kind:string,name:string}
export type xlsItemInfo = {id:string,name:string,icon:string,desc:string,pinzhi:number,type:number,from:number[],produce:string}
// type for kaiqi.excel
export type xlsKaiqiInfo = {id:string,name:string,desc:string,icon:string,needGuide:number,type1:number,param1:number,sevOpenDay:number,tip:string,record:number}
// type for languageClient.excel
export type xlsLanguageClientText = {key:string,cn:string,us:string,kr:string,tw:string,jp:string}
// type for languageSever.excel
export type xlsLanguageSeverHouduan = {}
// type for language.excel
export type xlsLanguageLang = {key:string,cn:string,kr:string,tw:string,jp:string}
// type for level.excel
export type xlsLevelLevel = {level:number,exp:number}
// type for math.excel
export type xlsMathInfo = {key:string,pram:number[]}
// type for mergeProp.excel
export type xlsMergePropInfo = {id:number,name:string,icon:string,type:number,quality:number,price:number}
// type for merge.excel
export type xlsMergeInfo = {id:number,formula:number[],item1:number[],rate1:number,item2:number[],rate2:number,item3:number[],rate3:number,item4:number[],rate4:number}
// type for monster.excel
// type for name.excel
export type xlsNameName = {}
// type for package.excel
export type xlsPackageInfo = {id:string,group:string,plat:string,wayhttp:string,cfgUrl:string,params:any[],logo:string,loadingTips1:string,loadingTips2:string}
// type for pet.excel
// type for pingbici.excel
export type xlsPingbiciInfo = {}
// type for shop.excel
// type for skill.excel
// type for task.excel
export type xlsTaskDesc = {kind:string,name:string,wgId:number,weekJumpId:string,jumpId:string}
export type xlsTaskMain = {id:string,nextid:string,kind:string,need:number,item:number[]}
// type for wing.excel

export class XlsConfig {
    xlsKaiqiInfo: xlsKaiqiInfo[]
    xlsEquipInfo: xlsEquipInfo[]
    xlsAudioInfo: xlsAudioInfo[]
    xlsItemKind: xlsItemKind[]
    xlsItemInfo: xlsItemInfo[]
    xlsMathInfo: xlsMathInfo[]
    xlsAttrAttr: xlsAttrAttr[]
    xlsLanguageSeverHouduan: xlsLanguageSeverHouduan[]
    xlsLanguageClientText: xlsLanguageClientText[]
    xlsLevelLevel: xlsLevelLevel[]
    xlsGuideInfo: xlsGuideInfo[]
    xlsChapterInfo: xlsChapterInfo[]
    xlsChapterLayout: xlsChapterLayout[]
    xlsMergePropInfo: xlsMergePropInfo[]
    xlsEmitterInfo: xlsEmitterInfo[]
    xlsNameName: xlsNameName[]
    xlsMergeInfo: xlsMergeInfo[]
    xlsPackageInfo: xlsPackageInfo[]
    xlsTaskDesc: xlsTaskDesc[]
    xlsTaskMain: xlsTaskMain[]
    xlsPingbiciInfo: xlsPingbiciInfo[]
    xlsLanguageLang: xlsLanguageLang[]

    constructor(bundleName:string, callback: Function, progressCallback?: Function) {
        cc.assetManager.loadBundle(bundleName, (err, bundle) => {
			if (err) {
                GameController.clear()
                cc.game.restart()
                if (progressCallback) {
                    progressCallback(0, 0.99);
                }
                return
            }
            if (gameMethod.isEmpty(bundle) && cc.sys.platform == cc.sys.WECHAT_GAME) {
                //小游戏平台，缓存过期，自动清理缓存，重启游戏
				console.error("加载配置表的bundle失败，清理缓存重新加载")
                cc.assetManager.cacheManager.clearCache()
                GameController.clear()
                cc.game.restart()
                if (progressCallback) {
                    progressCallback(0, 0.99);
                }
                return
            }
            bundle.loadDir("",(finish: number, total: number, item: any)=>{
                if (progressCallback) {
                    progressCallback(finish, total);
                }
            }, (err2, assets: cc.JsonAsset[]) => {
				if (err2) {
					GameController.clear()
                    cc.game.restart()
                    if (progressCallback) {
                        progressCallback(0, 0.99);
                    }
				    return
				}
                let current = 0;
                assets.forEach(element => {
                    this["xls" + this.firstUpCase(element.name)] = element.json;
                    current++;
                });
                callback(this)
            })
        })
    }
    // 首字母大写
    private firstUpCase(str: string) {
        return str[0].toUpperCase() + str.substring(1)
    }
}