// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { ScreenName } from "../UI/Screen/ScreenUI";
import UIManager from "../UI/UIManager";
import DataLevel from "./DataLevel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {

    @property(UIManager)
    uiManager: UIManager = null;

    dataLevels: DataLevel[] = [];

    async onLoad() {
       await this.initialize();
    }

    protected start(): void {
        this.backHone()
    }

    async initialize() {
        this.dataLevels =  await this.loadLevel() as DataLevel[];
        this.uiManager.initialize(this);
    }

    backHone() {
        this.uiManager.activeScreen(ScreenName.MAINMENU);
    }

    async loadLevel() {
        return new Promise(function (resolve, reject){
             cc.loader.loadRes("Data/LevelConfigs",  (err, objects) => {
                if (err) {
                    cc.log('Error when loading JSON:', err);
                    reject([]);
                }
                let dataLevels = objects.json.map((object: any) => {
                    let dataLevel = new DataLevel();
                    Object.assign(dataLevel, object);
                    return dataLevel;
                });
                resolve(dataLevels);
            });
        })
    }

}