// 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 BundleMgr from "../FrameWork/Mgr/BundleMgr";
import GameReport, { ENTERTYPE } from "../FrameWork/Report/ZyZyReport";

import User from "../FrameWork/User/User";
import AppPlatform from "../FrameWork/Util/AppPlatform";
import Common from "../FrameWork/Util/Common";
import Common5 from "../Platform/th/Common5";
import TTAPI from "../Platform/tt/TTAPI";
import Game from "../Scripts/Game";
import MainScene from "../Scripts/MainSceneScript";
import DayUnlock from "../ttFrame/manager/DayUnlock";
import GameAlllockView from "./GameAlllockView";
import LevelUnlock from "./UnlockLevels";

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];
            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() {
        if (this.isUnLock || DayUnlock.getDayUnlockVideoAllTime() > 0) {
            if (MainScene.ins.isLoading) {
                // Common5.showTips_custom("请稍等,资源正在加载中......");
                MainScene.ins.showLoading();
                return;
            }
            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;
            MainScene.ins.isLoading = true;
            // Common5.showTips_custom("请稍等,资源正在加载中......");
            MainScene.ins.showLoading();
           
            // User.setLevelAryy_custom(this.level);
            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) {
                        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)=>{
                            // prefab.width = Game.ins.maskNode.width;
                            // prefab.height = Game.ins.maskNode.width;
                            if (Common5.selectGameNum != 0){                        
                                User.setFirstIn(false);
                            }
                            
                            if (Common5.selectGameInfo.isTer){
                                Common5.getPrefabFromBundle("ZoomGame","prefab/view/TerrTip");
                            }
                            User.setLeveNum_custom(Common5.selectGameNum);
                            // prefab.scale = Game.ins.maskNode.width/prefab.width;
                            // prefab.setPosition(0,0);
                            // console.log("prefab.width",prefab.width,prefab.height,prefab.position,MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode").position)
                            // 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) {
                    MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode").removeAllChildren();
                    MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode").destroyAllChildren();
                    Common5.getPrefabFromBundle(Common5.selectGameInfo.bundle,Common5.selectGameInfo.url,MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode"),(prefab)=>{
                        // prefab.width = Game.ins.maskNode.width;
                        // prefab.height = Game.ins.maskNode.width;
                        if (Common5.selectGameNum != 0){                        
                            User.setFirstIn(false);
                        }
                        
                        if (Common5.selectGameInfo.isTer){
                            Common5.getPrefabFromBundle("ZoomGame","prefab/view/TerrTip");
                        }
                        User.setLeveNum_custom(Common5.selectGameNum);
                        // prefab.scale = Game.ins.maskNode.width/prefab.width;
                        // prefab.setPosition(0,0);
                        // console.log("prefab.width",prefab.width,prefab.height,prefab.position,MainScene.ins.GameNode.getChildByName("mask").getChildByName("prefabNode").position)
                        
                        MainScene.ins.hidLoading();
                        Game.ins.reset();
                        MainScene.ins.isLoading = false;
                    })
                });
            }
          
        }
        else {
            Common.getPrefabFromBundle("ZoomGame", "prefab/view/LevelUnlock", null, (node: cc.Node) => {
                node.getComponent(LevelUnlock).setLevel(this.level, this.type);
            })
            return;

        }
    }
    updateUI() {
        let allUnlock = false;
        if (DayUnlock.getDayUnlockVideoAllTime() > 0) {
            allUnlock = true;
            // return;
        }
        let use = User.getLevelAryy_custom();

        // this.node.getChildByName("titleTex").getComponent(cc.Label).string = Common5.gameConfig.zmGameConfig[this.level].titleUrl;
        // this.node.name = "点击关卡";
        // Common5.getSpriteFrameFromBundle("ZoomGame", "texure/words/gameTitle/"+Common5.gameConfig.zmGameConfig[this.level].titleUrl, this.node.getChildByName("titleTex").getComponent(cc.Sprite));

        // for (let i = 0; i < this.node.getChildByName('levNum').childrenCount; i++) {
        //     this.node.getChildByName('levNum').children[i].active = false
        // }
        // this.node.getChildByName('levNum').children[this.index%3].active = true

        //console.log("1+++++++++++++++++++>" + this.node.getChildByName("hot").active);
        

        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;
    }
}