import { ryw_Event } from "../FrameWork/Event/EventEnum";
import EventMgr from "../FrameWork/Event/EventMgr";
import GameMgr from "../FrameWork/Mgr/GameMgr";
import GameReport 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 DayUnlock from "../ttFrame/manager/DayUnlock";
import PhysicalPowerManger from "../ttFrame/manager/PhysicalPowerManger";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelUnlock extends cc.Component {
    level:number = 0;
    type:number = 0;
    backBtn:cc.Node = null;
    videoBtnOne:cc.Node = null;
    videoBTN:cc.Node = null;
    isLoading = false;
    
    @property(cc.Sprite)
    levelSpript: cc.Sprite = null;
    private needVideoNum = 3;
    protected start(): void {
        this.needVideoNum = Common5.videoDayUnlockNum;
        this.backBtn = this.node.getChildByName("show").getChildByName("btnClose");
        this.videoBtnOne = this.node.getChildByName("show").getChildByName("btnNode").getChildByName("unlockOne");
        this.videoBTN = this.node.getChildByName("show").getChildByName("btnNode").getChildByName("unlockAll");
        Common5.btnRegister_custom(this.backBtn,()=>{
            this.btnCloseFunc();
        })
        Common5.btnRegister_custom(this.videoBtnOne,()=>{
            this.btnVideo();
        })
        Common5.btnRegister_custom(this.videoBTN,()=>{
            this.btnVideoUnlockAll();
        })
        if (this.level == -1 && this.type == -1){
            this.videoBtnOne.active = false;
        }
        else{
            let bundle = cc.assetManager.loadBundle(Common5.gameConfig.zmGameConfig[this.level].bundle,(err,bundle)=>{                
                bundle.load(Common5.gameConfig.zmGameConfig[this.level].url, cc.Prefab, () => {
                    console.log(this.level + "  preloadSuccess!")
                })
            })
            if (Common5.gameConfig.zmGameConfig[this.level].subbundle && Common5.gameConfig.zmGameConfig[this.level].subbundle!=""){                
                let subbundle = cc.assetManager.loadBundle(Common5.gameConfig.zmGameConfig[this.level].subbundle,(err,subbundle)=>{                
                    subbundle.load(Common5.gameConfig.zmGameConfig[this.level].suburl, cc.Prefab, () => {
                    })
                })
            }
        }
        this.updateVideoTimes();
    }
    updateVideoTimes(){
        this.node.getChildByName("show").getChildByName("btnNode").getChildByName("unlockAll").getChildByName("label").getComponent(cc.Label).string = DayUnlock.getDayUnlockVideoAllNum().toString() + "/" + Common5.videoDayUnlockNum;
    }
    setLevel(levelIn,typeIn){
        this.level = levelIn;
        this.type = typeIn;
        Common5.getSpriteFrameFromBundle("ZoomGame", "texure/zoomLevel/" + Common5.gameConfig.zmGameConfig[this.level].unlockImg, this.levelSpript);
    }
    btnVideo(){
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    // Common5.selectGameNum = this.level;
                    GameReport.ADReport('解锁本关',Common5.selectGameInfo.title2,1)

                    if(Common5.selectGameInfo){
                        Common5.ReportDY("inLevel", Common5.selectGameInfo.title2 + "-AD-解锁本关");
                    }

                    Common5.wordGameType = this.type;
                    this.isLoading = true;
                    User.setLevelAryy_custom(this.level);
                    User.saveDataToServer()
                    EventMgr.emitEvent_custom(ryw_Event.unLockLevel,this.level);
                }
                else {
                    GameReport.ADReport('解锁本关',Common5.selectGameInfo.title2,0)
                    Common5.showTips_custom("广告未观看完");
                }
                this.node.removeFromParent();
            }, onFailed: () => {
                this.node.removeFromParent();
            }
        }
        AppPlatform.playVideo_custom(tab);
    }
    btnVideoUnlockAll(){
        let tab = {
            onClose: (finish)=>{
                if (finish) {

                    if(Common5.selectGameInfo){
                        Common5.ReportDY("inLevel", Common5.selectGameInfo.title2 + "-AD-解锁全部");
                    }
      
                    
                    if (AppPlatform.is_TT_GAME_custom()) {
                        window["tt"].reportAnalytics('watchAd', {
                            type: '看视频无限体力',
                        });
                    }
                    let num = DayUnlock.getDayUnlockVideoAllNum();
                    num = num + 1;
                    if (DayUnlock.getDayUnlockVideoAllTime() == 0) {
                        if (num == 1) {
                            DayUnlock.setDayUnlocVideoAllNum(num);
                            GameMgr.getInstance_custom().saveGameData_custom();
                        } else if (num == 2) {
                            DayUnlock.setDayUnlocVideoAllNum(num);
                            GameMgr.getInstance_custom().saveGameData_custom();
                        }
                    }
    
                    if (num >= this.needVideoNum) {
                        GameReport.ADReport('解锁全部',Common5.selectGameInfo.title2,1)
                        Common5.isNoPower = false;
                        num = this.needVideoNum;
                        DayUnlock.setDayUnlocVideoAllNum(num);
                        this.updateVideoTimes();
                        DayUnlock.setDayUnlocVideoAllNum(0);
                        DayUnlock.addNewDayUnlocksicalAll();
                        GameMgr.getInstance_custom().saveGameData_custom();
                        this.scheduleOnce(() => {
                            this.btnCloseFunc();
                        }, 0.3)
                        EventMgr.emitEvent_custom(ryw_Event.unLockAllLevelTmp);
                    } else {
                        GameReport.ADReport('解锁全部',Common5.selectGameInfo.title2,0)
                        DayUnlock.setDayUnlocVideoAllNum(num);
                        this.updateVideoTimes();
                        GameMgr.getInstance_custom().saveGameData_custom();
                    }

                    User.saveDataToServer()

                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    btnCloseFunc() {
        this.node.removeFromParent();
    }
}