import { _decorator, Component, Node, game, find, loader, JsonAsset } from 'cc';
import { LayerManager } from '../../engines/gui/LayerManager';
import { UIConst } from '../ui/UIConst';
import { LoadingView } from '../../engines/loadingView/LoadingView';
import { SceneManager } from '../../engines/scenes/SceneManager';
import { GameModel } from '../models/GameModel';
import GameConfigManager from '../models/GameConfigManager';
const { ccclass, property } = _decorator;

@ccclass('PreloadingScene')
export class PreloadingScene extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;

    onLoad(){
        console.log("进入Preloading场景");
    }

    start () {
        let canvas:Node=find("Canvas");

        game.addPersistRootNode(canvas);
        
        //UI全局节点
        let uiRoot:Node=canvas.getChildByName("UIRoot");
        let layerRoot:Node=uiRoot.getChildByName("LayerRoot");

        LayerManager.single.Init(layerRoot);
        UIConst.RegisterGUI();

        this.initAds();
    }

    private initAds():void{
        GameModel.single.ClearLocalData();
        GameModel.single.gameName="gregGame";
        GameModel.single.userId="guest";

        if(window["wx"]){
            this.LoadSubPackage();
        }else{
            this.LoadConfigs();
        }
    }

    private LoadSubPackage():void{
        loader.downloader.loadSubpackage('d3d', (err: any) => {
            if (err) {
                return console.error("loadSubpackage Error:"+err);
            }
            loader.downloader.loadSubpackage('ui', (err: any) => {
                if (err) {
                    return console.error("loadSubpackage Error:"+err);
                }
                this.LoadConfigs();
            });
        });
    }

    LoadConfigs():void{
        loader.loadResDir("configs",JsonAsset,this.onConfigLoadProgress.bind(this),this.onConfigLoadComplete.bind(this));
    }

    onConfigLoadProgress(index:number,total:number):void{
        LoadingView.single.UpdateProgress(index/total);
    }

    onConfigLoadComplete(error,asset):void{
        if(error!=null){
            throw new Error("加载配置出错！");
        }
        GameConfigManager.Init(asset);
        this.CheckLevels();
    }

    private CheckLevels():void{
        //计算关卡数量
        let index:number=0;
        let levelConfig:any
        do {
            index++;
            levelConfig=loader.getRes("configs/levels/Level"+index);
        } while (levelConfig);
        GameModel.single.maxLevel=index;

        this.initLocalData();
    }

    private initLocalData():void{
        GameModel.single.ReadByLocal();
        SceneManager.single.Swicth("PrepareScene");
    }
    
    //假进度条
    private time:number=0;
    update (deltaTime: number) {
        this.time+=deltaTime;
        this.time=this.time%2;
        LoadingView.single.UpdateProgress(this.time/3);
    }
}
