import { PetIndexData } from './../libs/tw/data/pet/PetIndexData';
import { GTime } from './../libs/tc/util/GTime';
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 PigThemeScene from "./chooseScene/PigThemeScene";
import PigVipLevelScene from "./chooseScene/PigVipLevelScene"
import LoadingScene from "./loadingScene/LoadingScene";
import { ModuleTypes } from "./types/sceneTypes";
import SwitchPigPanel from "./chooseScene/SwitchPigPanel";
import PunchCardSucPanel from "./punchCard/PunchCardSucPanel";
import PunchCardFailPanel from "./punchCard/PunchCardFailPanel";
import PlayScene from "./playScene/PlayScene";
import RuleScene from "./rule/RuleScene";
import PrizePanel from "./prizePanel/PrizePanel";
import NoPrizePanel from "./prizePanel/NoPrizePanel";
import WarningPanel from "./warningPanel/WarningPanel";
import CampPanel from './campPanel/CampPanel';
import SharePanel from './share/SharePanel';
// import GameEndWaitingScene from "./gameEnd/gameEndWaitingScene";
// import ExGameEndScene from "./gameEnd/ExGameEndScene";
import CalendarScene from "./calendar/CalendarScene";
// import EndScene from "./endScene/EndScene";
import Msg from "./alert/Msg";
import GameConst from './GameConst';

import GameOverScene from './overScene/OverScene';
import Waiting from './waiting/Waiting';
import Toast from './alert/Toast';

class Main extends MainBase {

    protected registerModules() {
        // 注册所有Panel
        PanelCtrl.instance.registerClass(ModuleTypes.ALERT_PANEL, Alert);
        PanelCtrl.instance.registerClass(ModuleTypes.MSG_PANEL, Msg);
        PanelCtrl.instance.registerClass(ModuleTypes.TOAST_PANEL, Toast);
        PanelCtrl.instance.registerClass(ModuleTypes.WARNING_PANEL, WarningPanel);
        PanelCtrl.instance.registerClass(ModuleTypes.SHARE_PANEL, SharePanel);
        PanelCtrl.instance.registerClass(ModuleTypes.SWITCHPIG_PANEL, SwitchPigPanel);
        PanelCtrl.instance.registerClass(ModuleTypes.PUNCHCARDSUC_PANEL, PunchCardSucPanel);
        PanelCtrl.instance.registerClass(ModuleTypes.PUNCHCARDFAIL_PANEL, PunchCardFailPanel);
        PanelCtrl.instance.registerClass(ModuleTypes.PRIZE_PANEL, PrizePanel);
        PanelCtrl.instance.registerClass(ModuleTypes.NOPRIZE_PANEL, NoPrizePanel);
        PanelCtrl.instance.registerClass(ModuleTypes.CAMP_PANEL, CampPanel);
        // 注册所有Scene
        SceneCtrl.instance.registerClass(ModuleTypes.LOADING_SCENE, LoadingScene);
        SceneCtrl.instance.registerClass(ModuleTypes.PIGTHEME_SCENE, PigThemeScene);
        SceneCtrl.instance.registerClass(ModuleTypes.PIGVIPLEVEL_SCENE, PigVipLevelScene);
        SceneCtrl.instance.registerClass(ModuleTypes.RULE_SCENE, RuleScene);
        SceneCtrl.instance.registerClass(ModuleTypes.CALENDAR_SCENE, CalendarScene);
        SceneCtrl.instance.registerClass(ModuleTypes.PLAY_SCENE, PlayScene);
        // SceneCtrl.instance.registerClass(ModuleTypes.GAMEOVER_SCENE, GameOverScene);
        // SceneCtrl.instance.registerClass(ModuleTypes.END_SCENE, EndScene);
        // SceneCtrl.instance.registerClass(ModuleTypes.GAMEEND_SCENE, ExGameEndScene);
        // SceneCtrl.instance.registerClass(ModuleTypes.GAMEENDWAITING_SCENE, GameEndWaitingScene);
        // NetManager.ins.onNotSuccess = (msg) => {
        //     showAlertPanel(msg)
        // }
    }

    // 获取数据
    protected getData() {
        super.getData();
    }

    // 设置派发事件
    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('PreloadLoaded', () => {
            // Waiting.instance.show()

            this.createGameScene()
        }, this);
        GDispatcher.addEvent('NormalLoaded', () => {
            // 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("开始加载YdPig资源");
            GDispatcher.dispatchEvent('NormalLoaded');
            window['isNormalLoaded'] = true;
            RES.loadGroup("YdPig");
        } else if (event.groupName == "YdPig") {
            this.trace("YdPig组加载完成");
            this.trace("开始加载CmPig资源");
            GDispatcher.dispatchEvent('YdPigLoaded');
            window['isYdPigLoaded'] = true;
            RES.loadGroup("CmPig");
        } else if (event.groupName == "CmPig") {
            this.trace("CmPig组加载完成");
            this.trace("开始加载ShPig资源");
            GDispatcher.dispatchEvent('CmPigLoaded');
            window['isCmPigLoaded'] = true;
            RES.loadGroup("ShPig");
        } else if (event.groupName == "ShPig") {
            this.trace("ShPig组加载完成");
            this.trace("开始加载Delay资源");
            GDispatcher.dispatchEvent('ShPigLoaded');
            window['isShPigLoaded'] = true;
            RES.loadGroup("Delay");
        } else if (event.groupName == 'Delay') {
            this.trace("Delay组加载完成");
            GDispatcher.dispatchEvent('DelayLoaded');
            window['isDelayLoaded'] = true;
            // console.log('ddd' + window['isDelayLoaded'])
            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);
        }
    }

    // 创建游戏场景
    protected createGameScene() {
        super.createGameScene();
        //获取UA信息
        let userAgent = navigator.userAgent.toLowerCase();
        console.log(userAgent);
        GameConst.stage = this.stage;
        setTimeout(() => {
            console.log(GameConst.stage.width, GameConst.stage.height)
        }, 100);


        if (window['dev']) {
            GameConst.setCookie('vip', '100014', 5);
        }
        // NetManager.ins.getInfo(() => {
            SceneCtrl.instance.change(ModuleTypes.LOADING_SCENE);
        // })


        // NetManager.ins.getSignInfo(
        //     () => {
        //         console.log(DataManager.ins.signInfoData)
        //         if (DataManager.ins.signInfoData.signInfoVO.acmDay > 0) {
        //             // 已经打过卡，即已领养
        //             GameConst.getAllPetsInfo(window['CFG'].gameInfo.oaId, () => {
        //                 //查询当前宠物信息
        //                 console.log(DataManager.ins.getData('getAllPetsInfo').data)
        //                 let petId;
        //                 DataManager.ins.getData('getAllPetsInfo').data.forEach(pig => {
        //                     if (pig.petLevel < 14) {
        //                         petId = pig.petId;
        //                     }
        //                 });
        //                 GameConst.getPetInfo(petId, () => {
        //                     SceneCtrl.instance.change(ModuleTypes.LOADING_SCENE);
        //                 })
        //             })
        //         } else {
        //             // 未打过卡，即未领养宠物，让用户领养宠物
        //             SceneCtrl.instance.change(ModuleTypes.PIGTHEME_SCENE);
        //         }

        //         // SceneCtrl.instance.change(ModuleTypes.PIGTHEME_SCENE);
        //     }
        //     , window['CFG'].gameInfo.oaId
        // );
    }

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

}

window['Main'] = Main;

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;
    }
});