/*
 * @Descripttion: 
 * @version: 1.0.0
 * @Author: YeeChan
 * @Date: 2020-07-09 18:54:40
 */

import User from "../User/User";
import { LogUtils } from "../Util/LogUtils";
import AppConfig from "../Config/AppConfig";
import TTAPI from "../../Platform/tt/TTAPI";
import Common5 from "../../Platform/th/Common5";
import EventMgr from "../Event/EventMgr";
import { ryw_Event } from "../Event/EventEnum";

import { TimerUtils } from "./TimerUtils";
import { DateUtils } from "../Util/DateUtils";
import GameReport from "../Report/ZyZyReport";


//游戏管理器,游戏代码的入口
export default class GameMgr {
    public isFirstLoadAD: boolean = false;
    public isWinner: boolean = false;
    private static _instance_custom: GameMgr = new GameMgr();

    public static getInstance_custom(): GameMgr {
        return GameMgr._instance_custom;
    }

    /**
     * 预加载一些场景 在Main.ts中
     */
    public preloadScene_custom() {
        // cc.director.preloadScene("MainScene");
        // cc.director.preloadScene("FMMainScene");
        // cc.director.preloadScene("FMGameScene");
        // cc.director.preloadScene("FMGameSettleWinScene");
        // cc.director.preloadScene("FMGameSettleFailScene");
        LogUtils.warn_custom("GameMgr.getInstance().preloadScene_custom这里可以加载需要预加载的场景");
    }

    //在合适的时候  cc.director.preloadScene 预加载你的场景

    /**
     * 登录加载后跳转自己的场景
     */
    public onLoadToWorldScene_custom(level?,callFunc?) {
        // cc.director.loadScene("FMMainScene", function (err, scene) {
        //     console.log("跳转游戏场景 FMMainScene")
        // });
        Common5.isBackLevel = level;
        cc.director.loadScene("MainScene", function (err, scene) {
            console.log("跳转游戏场景 MainScene");
          
            console.log('oldTime',GameReport.getLoadingTime() )
            console.log('newTime',DateUtils.getNowTime_custom())
            GameReport.setLoadingFinishTime(DateUtils.getNowTime_custom())
            GameReport.getUserId()
            
            User.initLevel();
            TTAPI.onShow((res)=>{
                //Common5.getSoundFromBundle_custom("ZoomGame", "Sounds/bgm", true);
                //Common5.playMusic("bg");
                //该判断为从侧边栏进入游戏
                if(res.launch_from == 'homepage' && res.location == 'sidebar_card'){
                    Common5.isCeBianLanEnter = true
                    EventMgr.emitEvent_custom(ryw_Event.CheckIsEnterFromCeBianLan)
                }
            })
            //检测侧边栏是否可用
            TTAPI.checkScene(()=>{
                EventMgr.emitEvent_custom(ryw_Event.CheckCeBianLan, true)
            },()=>{
                EventMgr.emitEvent_custom(ryw_Event.CheckCeBianLan, false)
            })
            callFunc && callFunc()
        });

    }



    //游戏存档,合适的时候调用它来保存
    public saveGameData_custom() {
        //存本地
        cc.sys.localStorage.setItem("data" + AppConfig.AppID_custom, User.getSaveData_custom());
    }


}