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 AppPlatform from "../FrameWork/Util/AppPlatform";
import Utilit from "../FrameWork/Util/Utilit";
import Common5 from "../Platform/th/Common5";
import TTAPI from "../Platform/tt/TTAPI";
import WXAPI from "../Platform/weixin/WXAPI";
import Game from "../Scripts/Game";
import MainScene from "../Scripts/MainSceneScript";
import PhysicalPowerManger from "../ttFrame/manager/PhysicalPowerManger";

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 {
        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)
            this.nextFunc();
            // }
        }, this);

        this.btnBack.on("click", () => {
            Common5.selectGameNum = -1;       
            // Game.ins.clickBack(null,true);
            
            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{ 
                }
            }, '','');

            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.showTips_custom("请稍等,资源正在加载中......");
            MainScene.ins.showLoading();
            Common5.stopMusic();
            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)
                
                        MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode").destroyAllChildren();
                        MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode").removeAllChildren();
                        Common5.getPrefabFromBundle(Common5.selectGameInfo.bundle,Common5.selectGameInfo.url,MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode"),(prefab)=>{
                            
                            // Common5.hideTips_custom();                            
                            MainScene.ins.hidLoading();
                            Game.ins.reset();
                            MainScene.ins.isLoading = false;
                        })
                    });
                })
            }
            else {                 
                cc.assetManager.loadBundle(Common5.selectGameInfo.bundle, function (err: Error, bundle: cc.AssetManager.Bundle) {
                    GameReport.EnterReport(ENTERTYPE.TONGGUAN,Common5.selectGameInfo.title2)
            
                    MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode").destroyAllChildren();
                    MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode").removeAllChildren();
                    Common5.getPrefabFromBundle(Common5.selectGameInfo.bundle,Common5.selectGameInfo.url,MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode"),(prefab)=>{
                        
                        Common5.hideTips_custom();
                        Game.ins.reset();
                        MainScene.ins.isLoading = false;                         
                        MainScene.ins.hidLoading();
                    })
                });                
            }
        }
        if (Common5.isNoEnergyMode == 0) {
            
            if (PhysicalPowerManger.isCanUserPhysical()) {
                inGameFunc();                
            } else {
                console.log("体力不够");
                Common5.getPrefabFromBundle("ZoomGame", "Prefabs/UI/WxtlUI", null, (prefab) => {
                })
            }
        } else {
            inGameFunc();
        }
        Common5.stopMusic();
        this.btnBackFunc();
    }
    setSuccessImg(){
        let img = Common5.gameConfig.jqGameConfig[Common5.selectGameNum].succssImg;
        if (img == "" || img == undefined){
            // this.node.getChildByName("success").getChildByName("label").getComponent(cc.Label).string = Common5.selectGameInfo.succssText;
            // this.node.getChildByName("success").getChildByName("success").active = false;
        }
        else{
            // this.node.getChildByName("success").getChildByName("label").active =false;
            // Common5.getSpriteFrameFromBundle("ZoomGame", img, this.node.getChildByName("success").getChildByName("success").getComponent(cc.Sprite));
        }
    }
    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();
    }
}