import AssetAdapter from "./AssetAdapter";
import PanelCtrl from "./ctrls/PanelCtrl";
import SceneCtrl from "./ctrls/SceneCtrl";
import Loading from "./loading/Loading";
import ThemeAdapter from "./ThemeAdapter";
import layers from "./views/layers";
import TopLayerCtrl from './ctrls/TopLayerCtrl';
import { getResPath } from "./utils";
import { customClassPrototype } from "./components/Custom";
import { NetManager } from "../libs/tw/manager/NetManager";
import { DataManager } from "../libs/tw/manager/DataManager";
import GameMgr from "./Mgr/GameMgr";
import LoadScene from "./Scene/LoadScene";
import MainScene from "./Scene/MainScene";
import { NetName } from "../libs/tw/enum/NetName";
import SoundMgr from "./Mgr/SoundMgr";
import { MTweenMgr, MTweenObject } from "./Game/MTween";
import { Decorator } from "./Decorator/Decorator";

@Decorator.OnUpdate
class Main extends eui.UILayer {
    constructor() {
        super();
    }
    protected createChildren() {
        super.createChildren();
        this.stage.orientation = egret.OrientationMode.LANDSCAPE_FLIPPED;
        GameMgr.instance.stage = this.stage;

        //inject the custom material parser
        //注入自定义的素材解析器
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());

        egret.ImageLoader.crossOrigin = "anonymous";

        layers.init(this);
        Loading.init(layers.topLayer);
        PanelCtrl.instance.init(layers.popupLayer);
        SceneCtrl.instance.init(layers.sceneLayer);
        TopLayerCtrl.instance.init(layers.topLayer);

        //TODO
        this.stage.scaleMode = egret.StageScaleMode.FIXED_HEIGHT;

        this.runGame().catch(e => {
            console.log(e);
        });
    }

    onUpdate() {
        MTweenMgr.update();
    }

    private async runGame() {
        await this.loadResource()
        this.createGameScene();
    }

    private async loadResource() {
        try {
            await RES.loadConfig("default.res.json", getResPath() + "resource/");
            await this.loadTheme();
        }
        catch (e) {
            console.error(e);
        }
    }

    private loadTheme() {
        return new Promise((resolve, reject) => {
            // load skin theme configuration file, you can manually modify the file. And replace the default skin.
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
            let theme = new eui.Theme("resource/default.thm.json", this.stage);
            theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                resolve();
            }, this);

        })
    }

    private textfield: egret.TextField;
    /**
     * 创建场景界面
     * Create scene interface
     */
    protected async createGameScene() {
        DataManager.ins.customCfgData = window["CFG"];
        NetManager.ins.ajaxElement((success: any, res: any) => {
            if (!success && res["code"] != "100201001") { //错误码不是未登录
                return;
            }
            SceneCtrl.instance.change(LoadScene);
        });
    }
}

window['Main'] = Main;
window["netData"] = {};
customClassPrototype();
