
import { Buried } from './../libs/tw/util/Buried';
import { GDispatcher } from './../libs/tc/util/GDispatcher';
import { DataManager } from './../libs/tw/manager/DataManager';
import { ABNetManager } from './../libs/tc/manager/ABNetManager';
import { INetData } from './../libs/tc/interface/INetData';
import MainBase from "../libs/tl/MainBase";
import { NetManager } from "../libs/tw/manager/NetManager";
import Alert from "./alert/Alert";
import PanelCtrl from "./ctrls/panelCtrl";
import SceneCtrl from "./ctrls/sceneCtrl";
import { ModuleTypes } from "./types/sceneTypes";
import RulePanel from './rulePanel/RulePanel';
import Msg from "./alert/Msg";
import GameConst, { wait } from './GameConst';
import Waiting from './waiting/Waiting';
import StartScene from './startScene/StartScene';
import PlayScene from './playScene/PlayScene';
import ToastCtrl from './ctrls/toastCtrl';
// import SVGACtrl from './ctrls/svgaCtrl';

import GetCreditsPanel from './recordPanel/RecordPanel';
import RecordPanel from './recordPanel/RecordPanel';
import NetConst, { yzwNet } from './NetConst';
import LoadingScene from './loadingScene/LoadingScene';
import { loadSvgaRes, loadSvga } from './loadSvga';
import { getResPath } from './utils';
import { GCache } from '../libs/tc/util/GCache';
import { GFun } from '../libs/tc/util/GFun';
import SVGACtrl from './ctrls/svgaCtrl';
class Main extends MainBase {
    

    protected registerModules() {

        // 注册Toast
        ToastCtrl.instance.registerClass();
        // 注册所有Panel
        PanelCtrl.instance.registerClass(ModuleTypes.GETCREDITS_PANEL, GetCreditsPanel)
        PanelCtrl.instance.registerClass(ModuleTypes.MSG_PANEL, Msg);
        PanelCtrl.instance.registerClass(ModuleTypes.RULE_PANEL, RulePanel);

        PanelCtrl.instance.registerClass(ModuleTypes.RECORD_PANEL, RecordPanel);

        // // 注册所有Scene
        SceneCtrl.instance.registerClass(ModuleTypes.PLAY_SCENE, PlayScene);
        SceneCtrl.instance.registerClass(ModuleTypes.START_SCENE, StartScene);

        // NetManager.ins.onNotSuccess = (msg) => {
        //     showAlertPanel(msg)
        // }
    }

    // 获取数据
    public updateCount: number = 0;
    protected async getData() {
        super.getData()
        //加载svga
        // let mv = await SVGACtrl.instance.load("http://yun.duiba.com.cn/db_games/activity/garbageSorting1.00/guideTips.svga");
        // let soundArr = ["numCountDown_mp3", "rightTips_mp3"]
        // preloadSound(soundArr);
    }



    // 设置派发事件
    protected GDispatchEvent() {
        GDispatcher.addEvent(ABNetManager.ERROR, (net: INetData, msg = '') => {
            const msgData = {
                net: net,
                msg: msg,
                // sureBtnTxt: TlLang.lang_011
            };
            //PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, msgData)
        }, this);
        // 监听当预加载资源加载完毕
        GDispatcher.addEvent('NormalLoaded', () => {
            Waiting.instance.show()
            this.createGameScene()
            //  Waiting.instance.hide()
        }, this);
        // GDispatcher.addEvent('DelayLoaded', () => {

        //     Waiting.instance.hide()
        //     this.createGameScene()
        // }, this);
    }

    protected onResourceLoadComplete(event: RES.ResourceEvent): void {
        if (event.groupName == "Preload") {
            this.trace("preload组加载完成");
            this.trace("开始加载Normal资源");
            // this.loadBg("startBg.png");
            GDispatcher.dispatchEvent('PreloadLoaded');
            window['isPreloadLoaded'] = true;
            RES.loadGroup("Normal");
        } else if (event.groupName == "Normal") {
            this.trace("Normal组加载完成");
            this.trace("开始加载Garbage资源");
            GDispatcher.dispatchEvent('NormalLoaded');
            // RES.loadGroup("Garbage");
        // }else if (event.groupName == "Garbage") {
        //     this.trace("Garbage组加载完成");
        //     this.trace("开始加载Delay资源");
        //     GDispatcher.dispatchEvent('GarbageLoaded');
        //     RES.loadGroup("Delay");
        // }  else if (event.groupName == "Delay") {
        //     this.trace("Delay组加载完成");
        //     this.trace("开始加载Map资源");
        //     GDispatcher.dispatchEvent('DelayLoaded');
        //     RES.loadGroup("Map");
        // }  else if (event.groupName == "Map") {
        //     this.trace("Map组加载完成");
        //     this.trace("开始加载Record资源");
        //     GDispatcher.dispatchEvent('MapLoaded');
        //     RES.loadGroup("Record");
        // } else if (event.groupName == "Record") {
        //     this.trace("Record组加载完成");
        //     this.trace("开始加载Garbageing资源");
        //     GDispatcher.dispatchEvent('GarbageingLoaded');
        //     RES.loadGroup("Garbageing");
        // }else if (event.groupName == "Garbageing") {
        //     this.trace("Garbageing组加载完成");
        //     this.trace("开始加载Delaying资源");
        //     GDispatcher.dispatchEvent('GarbageingLoaded');
        //     RES.loadGroup("Delaying");
        // }  else if (event.groupName == "Delaying") {
        //     this.trace("Delaying组加载完成");
        //     this.trace("开始加载Mapping资源");
        //     GDispatcher.dispatchEvent('DelayingLoaded');
        //     RES.loadGroup("Mapping");
        // }  else if (event.groupName == "Mapping") {
        //     this.trace("Map组加载完成");
            // GDispatcher.dispatchEvent('MappingLoaded');
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        }
    }

    public onecow;
    // 创建游戏场景
    protected createGameScene() {
        super.createGameScene();

        egret.lifecycle.addLifecycleListener((context) => {
            console.log("生命周期--------1","addLifecycleListener");
            // custom lifecycle plugin
        })

        egret.lifecycle.onPause = () => {
            GDispatcher.dispatchEvent("addpause",null);
            egret.ticker.pause();
            console.log("生命周期--------2","pause");
            
        }

        egret.lifecycle.onResume = () => {
            GDispatcher.dispatchEvent("addresume",null);
            egret.ticker.resume();
            console.log("生命周期--------2","resume");
            
        }

        // SceneCtrl.instance.change(ModuleTypes.ShareStarterScene);
        // PanelCtrl.instance.show(ModuleTypes.SloganPanel);
        // Waiting.instance.hide();
        // return;
        Buried.init();

        GameConst.stage = this.stage;
        setTimeout(() => {
            console.log(GameConst.stage.width, GameConst.stage.height)
        }, 100);
        
        SceneCtrl.instance.change(ModuleTypes.START_SCENE,this.onecow);

    }

    /**
     * 资源组加载出错
     */
    protected onResourceLoadError(event: RES.ResourceEvent): void {
        this.trace("preload组加载失败");
        //忽略加载失败的项目
        this.onResourceLoadComplete(event);
    }

    isIOS() {
        const u = navigator.userAgent;
        let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
        return isiOS;
    }

}

window['Main'] = Main;
egret.ImageLoader.crossOrigin = "anonymous";
egret.runEgret({
    renderMode: "webgl", audioType: 0, calculateCanvasScaleFactor: function (context: any) {
        var backingStore = context.backingStorePixelRatio ||
            context.webkitBackingStorePixelRatio ||
            context.mozBackingStorePixelRatio ||
            context.msBackingStorePixelRatio ||
            context.oBackingStorePixelRatio ||
            context.backingStorePixelRatio || 1;
        return (window.devicePixelRatio || 1) / backingStore;
    }
});