import { DataManager } from '../libs/tw/manager/DataManager';
import { NetManager } from '../libs/tw/manager/NetManager';
import Alert from "./alert/Alert";
import Msg from "./alert/Msg";
import AssetAdapter from "./AssetAdapter";
import Loading from "./loading/Loading";
import ThemeAdapter from "./ThemeAdapter";
import GameConst from "./GameConst";
import { ModuleTypes } from "./types/sceneTypes";
import { getResPath } from "./utils";
import layers from "./views/layers";
import PanelCtrl from "./ctrls/panelCtrl";
import SceneCtrl from "./ctrls/sceneCtrl";
import RulePanel from "./rulePanel/RulePanel";
import PlayScene from "./playScene/PlayScene";
import StartScene from "./startScene/StartScene";
import ResultPanel from "./resultPanel/ResultPanel";
import MovieClipManager from "./ctrls/MovieClipManager";
import {Before} from "./Before";

class Main extends eui.UILayer {
    protected createChildren(): void {
        super.createChildren();

        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin
        })

        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }

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

        egret.ImageLoader.crossOrigin = "anonymous";

        DataManager.ins.gameCfgData = window['CFG'];
        DataManager.ins.gameCfgData.gameInfo.gameId = window["gameId"];
        NetManager.ins.getOptions(()=>{});
        NetManager.ins.getCredits(()=>{});

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

        // 固定宽
        this.stage.scaleMode = egret.StageScaleMode.FIXED_WIDTH;

        // 注册
        PanelCtrl.instance.registerPanelClass(ModuleTypes.MSG_PANEL, Msg);
        PanelCtrl.instance.registerPanelClass(ModuleTypes.RULE_PANEL, RulePanel);
        PanelCtrl.instance.registerPanelClass(ModuleTypes.RESULT_PANEL, ResultPanel);
        SceneCtrl.instance.registerSceneClass(ModuleTypes.START_SCENE, StartScene);
        SceneCtrl.instance.registerSceneClass(ModuleTypes.PLAY_SCENE, PlayScene);
        
        Before.ins.init();

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

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

    private async loadResource() {
        try {
            // const loadingView = new LoadingUI();
            // this.stage.addChild(loadingView);
            await RES.loadConfig("default.res.json", getResPath() + "resource/");
            await this.loadTheme();
            await RES.loadGroup("msg", 10);
            await MovieClipManager.instance.init();
            
            // this.stage.removeChild(loadingView);
        }
        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 createGameScene(): void {
        
        NetManager.ins.getInfo(() => {
            // 等待开奖 ↓
            if (DataManager.ins.getInfoData.status.code == 4) { 

            // 已开奖 ↓
            } else if (DataManager.ins.getInfoData.status.code == 5) {

            // 正常游戏 ↓
            } else {
                let href = window['location'].href;
                let basePicUrl = href.split('basePic');
                if(basePicUrl[1]){
                    
                }else{
                    SceneCtrl.instance.change(ModuleTypes.START_SCENE)
                }
                
            }
        });
        
        // setTimeout(() => {
        //     // panelCtrl.showAlertPanel('te111st');
        //     // Loading.instace.show()

        // }, 3000);
    }
}

window['Main'] = Main;