import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import GameReport, { ENTERTYPE, OUTTYPE } from "../../../FrameWork/Report/ZyZyReport";
import User from "../../../FrameWork/User/User";
import Common5 from "../../../Platform/th/Common5";
import TTAPI from "../../../Platform/tt/TTAPI";
import WXAPI from "../../../Platform/weixin/WXAPI";
import Game from "./Game";
// import GameZC from "../GameZC";
import PrefabManage, { GameType } from "../../PrefabManager/PrefabManage";
import UserManager from "../../Manager/UserManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameSuccessUI extends cc.Component {

    protected gameNode: cc.Node = null;
    protected btnNext: cc.Node = null;
    protected btnBack: cc.Node = null;
    protected btnShare: cc.Node = null;
    protected fireworksNode: cc.Node = null;
    @property(cc.Label)
    timeLabel: cc.Label = null;
    curGameName = null

    protected start(): void {
        Common5.showInterstitialAd()
        TTAPI.stopRecord_custom();
        // Common5.stopMusic();

        if (!Common5.isNoMusic) {
            Common5.playEffect("success")
        }
        Common5.stopMusic();
        // Common5.playMusic("bg");
        User.setLevelAryy_custom(Common5.selectGameNum);
        this.curGameName = Common5.selectGameInfo.titleUrl
        this.gameNode = this.node.getChildByName("game");
        this.btnNext = this.gameNode.getChildByName("btnNext");
        this.btnBack = this.gameNode.getChildByName("btnBack");
        this.btnShare = this.gameNode.getChildByName("btnShare");
        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;
            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;
        this.btnNext.on("click", () => {
            console.log(Common5.selectGameNum);
            if (Common5.selectGameNum == -1) {
                Common5.showTips_custom("恭喜您已通关!")
                return;
            }
            GameReport.OUTReport(OUTTYPE.TIAOZHUAN, this.curGameName)
            let bundle = Common5.selectGameInfo.bundle
            if (bundle.includes('Level')) {

            } else {
                Game.ins.clearLastGameNode()

                PrefabManage.loadPrefabByType(GameType.WZ_GameMain, null, () => {
                    this.scheduleOnce(() => {
                        this.nextFunc();
                    })
                })

            }
            // this.nextFunc();
            // }
        }, this);

        this.btnBack.on("click", () => {
            Common5.selectGameNum = -1;
            let bundle = Common5.selectGameInfo.bundle
            if (bundle.includes('Level')) {
                //GameZC.ins.exitGame()
            } else {
                Game.ins.exitGame();
            }

            this.node.removeFromParent();
            GameReport.OUTReport(OUTTYPE.CHENGGONG, Common5.selectGameInfo.title2)
        }, this);

        this.btnShare.on("click", () => {
            //分享
            console.log("share");

            WXAPI.share_custom((finish) => {
                if (finish) {
                } else {
                }
            }, '', '');

            WXAPI.share_custom((finish) => {
            }, '', '');

            TTAPI.shareRecord_custom(() => {
                // this.btnBackFunc();
            }, () => {
                Common5.showTips_custom("录屏分享失败,请稍后再试!");
                // this.btnBackFunc();
            })
        }, this);
        EventMgr.emitEvent_custom(ryw_Event.updateLevel);
        // Common5.getSoundFromBundle_custom("ZoomGame", "Sounds/fail", false);
    }
    nextFunc() {
        let inGameFunc = () => {
            Common5.stopMusic();
            let bundle = Common5.selectGameInfo.bundle
            let example = null
            if (bundle.includes('Level')) {
                //example = GameZC
            } else {
                example = Game
            }

            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) {
                        //GameReport.EnterReport(ENTERTYPE.TONGGUAN,Common5.selectGameInfo.title2)
                        Common5.getPrefabFromBundlePrefabLayer(Common5.selectGameInfo.bundle, Common5.selectGameInfo.url, example.ins.node.getChildByName("mask").getChildByName("prefabNode"), (prefab) => {
                            example.ins.reset();
                        })
                    });
                })
            }
            else {
                cc.assetManager.loadBundle(Common5.selectGameInfo.bundle, function (err: Error, bundle: cc.AssetManager.Bundle) {
                    //GameReport.EnterReport(ENTERTYPE.TONGGUAN,Common5.selectGameInfo.title2)
                    Common5.getPrefabFromBundlePrefabLayer(Common5.selectGameInfo.bundle, Common5.selectGameInfo.url, example.ins.node.getChildByName("mask").getChildByName("prefabNode"), (prefab) => {
                        example.ins.reset();
                    })
                });
            }
        }
        if (Common5.isNoEnergyMode == 0) {
            inGameFunc();
        } else {
            inGameFunc();
        }
        Common5.stopMusic();
        this.btnBackFunc();
    }
    setSuccessImg() {

    }
    setSuccessTime(time) {
        let secondStr = "";
        let second = time % 60;
        if (second < 10) {
            secondStr = "0" + second;
        }
        else {
            secondStr = second.toString();
        }
        this.timeLabel.string = Math.floor(time / 60) + ":" + secondStr;
    }
    btnBackFunc() {
        this.node.removeFromParent();
    }
}