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

import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import Common5 from "../../Platform/th/Common5";
import List from "../../Platform/th/List";
import UiBase from "../GameBase/UiBase";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import ChallengeManager from "../Manager/ChallengeManager";
import LevelUpManager from "../Manager/LevelUpManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import ChallengeChooseItem from "./ChallengeChooseItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ChallengeChoose extends UiBase {

    @property(cc.Label)
    clickMoneyLab: cc.Label = null;
    
    @property(List)
    listG: List = null; //物品
   
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    dataListG = []
    start () {
        PrefabManage.preloadPrefabByType(GameType.ChallengeGame)
        Common5.loadRemoteSpine('Spine/challenge/男主3','男主')

        EventMgr.onEvent_custom(ryw_Event.challengeAllQuit,()=>{
            if(cc.isValid(this.node, true)){
                this.node.removeFromParent()
                this.node.destroy()
            }
        },this)


        EventMgr.onEvent_custom(ryw_Event.challengeQuit,()=>{
            this.loadSpine()
        },this)

      
        this.setListG()
        this.loadSpine()
        this.guideTask()

        
        let str1 = LevelUpManager.getCurClickIncome()
        let hanzi = Common5.getNumberChangeHanzi(str1,'1',4)
        this.clickMoneyLab.string = `我的战力(点击):${hanzi}/次`//'' + Common5.getNumberChangeHanzi(str1) + '/次'
    }

    guideTask(){
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if(mainId == MainTaskIdEnum.MainTask_506){
            
            let node = this.node.getChildByName('guideClick')
            this.guideView([node],0)

        }else{
            //获取当前需要挑战的人
            let idIndex = ChallengeManager.getCurChallengeId()
            if(idIndex>1){
                idIndex = idIndex-2
            }
            this.listG.scrollTo(idIndex)
        }
    }

    guideView(nodeArray, opacity = -1){
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab)=>{
            // prefab.zIndex = 199
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            if(opacity!=-1){
                prefab.getComponent(NewGuideScript).setOpacityMaskNode(opacity)
            }
        })
    }

    
    loadSpine(){
        let config = ChallengeManager.getCurChallengeConfigDate()
        if(config){
            Common5.loadRemoteSpine(config.spineUrl,config.spineName)
        }
        

    }
    onListGGridRender(item: cc.Node, idx: number) {
        let config =  this.dataListG[idx]
        let danItem:ChallengeChooseItem = item.getComponent('ChallengeChooseItem')
        danItem.initView(config)
        
    }


    setListG(){
      
        let challengeList = ChallengeManager.getManagerConfigs()
   
        this.dataListG = []
        for (let n = 0; n < challengeList.length; n++) {
            this.dataListG.push(challengeList[n]);
        }
        this.listG.numItems = this.dataListG.length;
     
    }

    closeUiView(){
        this.node.removeFromParent()
        this.node.destroy()
        EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, '');
    }
    // update (dt) {}
}