// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import GameReport, { ENTERTYPE } from "../../../FrameWork/Report/ZyZyReport";
import User from "../../../FrameWork/User/User";
import Common5 from "../../../Platform/th/Common5";
import Game from "./Game";
import DayUnlock from "../../../FrameWork/Mgr/DayUnlock";
import LevelUnlock from "./UnlockLevels";
import PrefabManage, { GameType } from "../../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelSelectNew extends cc.Component {


    // onLoad () {}
    isUnLock: boolean = false;
    level: number = 0;
    index: number = 0;
    type: number = 0;
    isLoading = false;
    @property(cc.Sprite)
    levelImg: cc.Sprite = null;
    
    @property(cc.Node)
    newLevel: cc.Node = null;

    @property(cc.Node)
    hotLevel: cc.Node = null;

    @property(cc.Label)
    levelName: cc.Label = null;
    start() {
        this.updateUI();
        EventMgr.onEvent_custom(ryw_Event.unLockLevel, () => {
            this.updateUI();
        }, this)
        EventMgr.onEvent_custom(ryw_Event.unLockAllLevelTmp, () => {
            this.updateUI();
        }, this)
        EventMgr.onEvent_custom(ryw_Event.updateLevel, () => {
            this.updateUI();
        }, this)
        // console.log("this.level-------"+this.level,this.index)
        Common5.btnRegister_custom(this.node, () => {
            Common5.selectGameInfo = Common5.gameConfig.zmGameConfig[this.level];

            let bundle = Common5.selectGameInfo.bundle
            if(bundle.includes('Level')){
       
            }else{
                PrefabManage.loadPrefabByType(GameType.WZ_GameMain,null,()=>{
                    this.scheduleOnce(()=>{
                        this.clickFunc();
                    })
                })
         
            }
      
            
        })
    }
    setLevel(lvl, index) {
        this.level = lvl;
        this.index = index;
        this.levelName.string = Common5.gameConfig.zmGameConfig[this.level].title2;
        // console.log("this.index",this.index);
    }

    clickFunc() {
        PrefabManage.showLoading(true)
        if (this.isUnLock || DayUnlock.getDayUnlockVideoAllTime() > 0) {
            Common5.stopMusic();
            //GameReport.EnterReport(ENTERTYPE.XUANGUAN,Common5.selectGameInfo.title2);
            Common5.showAllGameUnLock = true
            EventMgr.emitEvent_custom(ryw_Event.loadGame)
            Common5.selectGameNum = this.level;
            Common5.wordGameType = this.type;
            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) {
                        Common5.getPrefabFromBundlePrefabLayer(Common5.selectGameInfo.bundle,Common5.selectGameInfo.url,Game.ins.node.getChildByName("mask").getChildByName("prefabNode"),(prefab)=>{
                            // if (Common5.selectGameNum != 0){                        
                            //     User.setFirstIn(false);
                            // }
                            PrefabManage.showLoading(false)
                            if (Common5.selectGameInfo.isTer){
                                PrefabManage.loadPrefabByType(GameType.WZ_TerrTip)
                            }
                            User.setLeveNum_custom(Common5.selectGameNum);
                   
                            Game.ins.reset();
              
                        })
                    });
                })
            }
            else {
                cc.assetManager.loadBundle(Common5.selectGameInfo.bundle, function (err: Error, bundle: cc.AssetManager.Bundle) {
                  Common5.getPrefabFromBundlePrefabLayer(Common5.selectGameInfo.bundle,Common5.selectGameInfo.url,Game.ins.node.getChildByName("mask").getChildByName("prefabNode"),(prefab)=>{
                        // if (Common5.selectGameNum != 0){                        
                        //     User.setFirstIn(false);
                        // }
                        PrefabManage.showLoading(false)
                        if (Common5.selectGameInfo.isTer){
                            PrefabManage.loadPrefabByType(GameType.WZ_TerrTip)
                        }
                        User.setLeveNum_custom(Common5.selectGameNum);
       
                        Game.ins.reset();
           
                    })
                });
            }
          
        }
        else {
            PrefabManage.loadPrefabByType(GameType.WZ_LevelUnlock,null,(node: cc.Node) => {
                node.getComponent(LevelUnlock).setLevel(this.level, this.type);
            })
            return;
        }
    }
    updateUI() {
        let allUnlock = false;

        console.log(DayUnlock.getDayUnlockVideoAllTime(), 'getDayUnlockVideoAllTime')
        
        
        if (DayUnlock.getDayUnlockVideoAllTime() > 0) {
            allUnlock = true;
            // return;
        }
        let use = User.getLevelAryy_custom();



        if (use[this.level] == 1) {
            this.isUnLock = true;
        }
        if (this.index == 0 && Common5.curWordGameType == 0) {
            this.isUnLock = true;
            User.setLeveNum_custom(this.level);
        }
        
        if (this.isUnLock || allUnlock){            
            let allSuccess = User.getSuccessLevels()
            for (let i = 0; i < allSuccess.length; i++){
                if (this.level == allSuccess[i]){
                    this.node.getChildByName("success").active = true;
                    break;
                }
            }
            if (!this.node.getChildByName("success").active){
                this.node.getChildByName("xuanzhong").active = true;
            }else{                
                this.node.getChildByName("xuanzhong").active = false;
            }
        }
        // if (this.level == Common5.UNLOCKGAME_ID) {
        //     this.isUnLock = true;
        //     User.setlevelNum_Word_Array_custom(this.level);
        // }
        if (Common5.gameConfig.zmGameConfig[this.level].tag){
            if (Common5.gameConfig.zmGameConfig[this.level].tag == "hot"){
                this.hotLevel.active = true;
                this.newLevel.active = false;
            }
            else if (Common5.gameConfig.zmGameConfig[this.level].tag && Common5.gameConfig.zmGameConfig[this.level].tag == "new"){
                this.newLevel.active = true;
                this.hotLevel.active = false;
            }
        } 
        else {
            this.newLevel.active = false;
            this.hotLevel.active = false;
        }
        Common5.getSpriteFrameFromBundle("ZoomGame", "texure/zoomLevel/" + Common5.gameConfig.zmGameConfig[this.level].unlockImg, this.levelImg);
        if (this.isUnLock || allUnlock) {
            this.node.getChildByName("lock").active = false;
            this.node.getChildByName("unlock").active = true;
        }
        else {
            this.node.getChildByName("lock").active = true;
            this.node.getChildByName("unlock").active = false;
        }
    }
    updateUITmpUnlock() {
        this.node.getChildByName("lock").active = false;
        this.node.getChildByName("unlock").active = true;
    }
    setType(type) {
        this.type = type;
    }
}