import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import GameReport from "../../../FrameWork/Report/ZyZyReport";
import User from "../../../FrameWork/User/User";
import AppPlatform from "../../../FrameWork/Util/AppPlatform";
import Common5 from "../../../Platform/th/Common5";
import TaskManager from "../../JuQingChat/TaskManager";
import PrefabManage, { GameType } from "../../PrefabManager/PrefabManage";
import Game from "./Game";

// import MainScene from "./MainSceneScript";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ExitTip extends cc.Component {

    protected start(): void {

    }
    nextFunc() {
        let levels = Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels;
        let nextLevel = -1;
        for (let i = 0; i < levels.length; i++) {
            if (levels[i] == Common5.selectGameNum) {
                if (i < levels.length - 1) {
                    nextLevel = levels[i + 1];
                }
                break;
            }
        }
        if (nextLevel != -1) {
            User.setLeveNum_custom(nextLevel);
            User.setLevelAryy_custom(nextLevel);
            // User.saveDataToServer()
            console.log('保存下一个+++')
            Common5.selectGameInfo = Common5.gameConfig.zmGameConfig[nextLevel];
            Common5.selectGameNum = nextLevel;
            EventMgr.emitEvent_custom(ryw_Event.updateLevel);
            Common5.wordGameType = 0;
        }
        else {
            let levelArr = User.getLevelAryy_custom();
            for (let i = 0; i < Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels.length; i++) {
                let level = Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels[i]
                if (levelArr[level] == 0) {
                    nextLevel = level;
                    Common5.selectGameInfo = Common5.gameConfig.zmGameConfig[nextLevel];
                    Common5.selectGameNum = nextLevel;
                    User.setLeveNum_custom(nextLevel);
                    User.setLevelAryy_custom(nextLevel);
                    Common5.wordGameType = 0;
                    EventMgr.emitEvent_custom(ryw_Event.updateLevel);
                    break;
                }
            }
        }
        if (nextLevel == -1) {
            let levelArr = User.getSuccessLevels();
            let find = false;
            for (let i = 0; i < Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels.length; i++) {
                let level = Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels[i]
                for (let j = 0; j < levelArr.length; j++) {
                    if (level == levelArr[j]) {
                        find = true;
                        break;
                    }
                }
                if (!find) {
                    nextLevel = level;
                    break;
                }
            }
        }

        Common5.selectGameNum = nextLevel;
        if (Common5.selectGameNum == -1) {
            Common5.showTips_custom("恭喜您已通关!")
            return;
        }
        let inGameFunc = () => {
            Common5.stopMusic();
            let bundle = Common5.selectGameInfo.bundle
            let example = null
            if (bundle.includes('Level')) {
                //example = GameZC
            } else {
                example = Game
            }
            example.ins.node.getChildByName("mask").getChildByName("prefabNode").removeAllChildren()
            if (Common5.selectGameInfo.subbundle != "" && Common5.selectGameInfo.subbundle != undefined) {
                let subbundle = cc.assetManager.loadBundle(Common5.selectGameInfo.subbundle, (err, subbundle) => {
                    cc.assetManager.loadBundle(Common5.selectGameInfo.bundle, function (err: Error, bundle: cc.AssetManager.Bundle) {
                        console.log("remove all children!")
                        Common5.getPrefabFromBundlePrefabLayer(Common5.selectGameInfo.bundle, Common5.selectGameInfo.url, example.ins.node.getChildByName("mask").getChildByName("prefabNode"), (prefab) => {
                            // if (Common5.selectGameNum != 0){                        
                            //     User.setFirstIn(false);
                            // }
                            if (Common5.selectGameInfo.isTer) {
                                PrefabManage.loadPrefabByType(GameType.WZ_TerrTip)
                            }
                            User.setLeveNum_custom(Common5.selectGameNum);
                            example.ins.reset();
                        })
                    });
                })
            } else {
                cc.assetManager.loadBundle(Common5.selectGameInfo.bundle, function (err: Error, bundle: cc.AssetManager.Bundle) {
                    console.log("remove all children!")
                    Common5.getPrefabFromBundlePrefabLayer(Common5.selectGameInfo.bundle, Common5.selectGameInfo.url, example.ins.node.getChildByName("mask").getChildByName("prefabNode"), (prefab) => {
                        // if (Common5.selectGameNum != 0){                        
                        //     User.setFirstIn(false);
                        // }
                        if (Common5.selectGameInfo.isTer) {
                            PrefabManage.loadPrefabByType(GameType.WZ_TerrTip)
                        }
                        User.setLeveNum_custom(Common5.selectGameNum);
                        example.ins.reset();
                    })
                });
            }
        }


        if (Common5.isNoEnergyMode == 0) {


            inGameFunc();

        } else {
            inGameFunc();
        }
        this.btnCloseFunc();
    }
    btnVideo() {
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    
                    if (Common5.selectGameInfo) {
                        let bundle = Common5.selectGameInfo.bundle
                        if (bundle.includes('Level')) {
                            GameReport.ADReport(Common5.selectGameInfo.titleUrl + '跳过本关', 1, "失物招领")

                            // Common5.ReportDY("inLevel", '失物招领-AD-跳过本关');
                            Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-失物招领跳过本关`)
                        } else {
                            // Common5.ReportDY("inLevel", '总裁训练营-AD-跳过本关');
                            Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-总裁训练营跳过本关`)
                            GameReport.ADReport(Common5.selectGameInfo.title2 + '跳过本关', 1, "总裁训练营")

                        }
                    }

                    this.nextFunc();
                }
                else {

                    let bundle = Common5.selectGameInfo.bundle
                    if (bundle.includes('Level')) {
                        GameReport.ADReport(Common5.selectGameInfo.titleUrl + '跳过本关', 0, "失物招领")
                    } else {
                        GameReport.ADReport(Common5.selectGameInfo.title2 + '跳过本关', 0, "总裁训练营")
                    }

                    Common5.showTips_custom("广告未看完");
                }
                this.node.removeFromParent();
            }, onFailed: () => {
                this.node.removeFromParent();
            }
        }
        AppPlatform.playVideo_custom(tab);
    }
    btnCloseFunc() {
        this.node.removeFromParent();
    }
    btnExit() {

        let bundle = Common5.selectGameInfo.bundle
        if (bundle.includes('Level')) {
            //GameZC.ins.exitGame()
        } else {
            Game.ins.exitGame();
        }

        this.node.removeFromParent();
    }


    btnShare() {
        AppPlatform.PlatformShare(() => {
        }, '', '')
    }
}