import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import BagManager from "../Manager/BagManager";

import InterfaceManager from "../Manager/InterfaceManager";
import { GameType } from "../PrefabManager/PrefabManage";


const {ccclass, property} = cc._decorator;

@ccclass
export default class CurGameJinHuoDesc extends cc.Component {

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

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

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

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

    @property([cc.String])
    curGameName:string[] = []
    // LIFE-CYCLE CALLBACKS:
    haveTaskNum = 0
    onLoad () {
        let taskId = TaskManager.getCurUnLockMainTaskId()
        if(taskId == MainTaskIdEnum.MainTask_535){
            this.haveTaskNum = Common5.DescCommonIndex
        }

   
    }

    start () {
        let isView = this.refreshView()
        if(!isView){
            this.refreshDaoyeView()
        }
        EventMgr.onEvent_custom(ryw_Event.RefreshCurJinHuoDesc,(num)=>{
            if(typeof(num) !== 'undefined'  && typeof(num) === 'number' &&!isNaN(num)){
                this.haveTaskNum = num
            }
            
            let isView = this.refreshView()
            if(!isView){
                this.refreshDaoyeView()
            }
        },this)

   
    }
    refreshDaoyeView(){
        this.descView.removeAllChildren()
        this.descView.destroyAllChildren()
        
        let daoyeGoodList  = User.getDaoyeGoodList()

     
        let isHaveData = false
        for(let i=0;i<daoyeGoodList.length;i++){
            let goodId = daoyeGoodList[i].goodId
            let gameName = BagManager.getJuQingGoodConfig(goodId).huoYuan
            if(this.curGameName.indexOf(gameName) < 0){
                continue
            }
            isHaveData = true
            let item = cc.instantiate(this.itemNode)
            item.parent = this.descView
            item.active = true
            this.refeshNode(item,goodId)
        }

        if(isHaveData){
            this.node.active = true
            let jianTouShang = cc.instantiate(this.jianTouShang)
            jianTouShang.active = true
            jianTouShang.parent = this.descView
        }else{
            this.node.active = false
        }
    }
    refreshView(){
        this.descView.removeAllChildren()
        this.descView.destroyAllChildren()
        
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        
        if(mainTaskInfo){
            let taskInfo = TaskManager.getTaskConfigById(mainTaskInfo.Id)
            if(!taskInfo){
                this.node.active = false
                return false
            }
            if(!taskInfo.gameName){
                this.node.active = false
                return false
            }
            if(this.curGameName.indexOf(taskInfo.gameName) < 0){
                this.node.active = false
                return false
            }
       
            if(taskInfo.gameName == '翻垃圾' || taskInfo.gameName == '洗盘子' 
                || taskInfo.gameName == '集团' || taskInfo.gameName == '散修城'
                || taskInfo.gameName == '浩瀚产业' || taskInfo.gameName == '海洋馆'
            
                
            ){
                let item = cc.instantiate(this.itemNode)
                item.parent = this.descView
                item.active = true
                this.refeshNodeBuyNum(item, this.haveTaskNum, taskInfo.allNum, taskInfo.taskGoodName)
    
                this.node.active = true
                let jianTouShang = cc.instantiate(this.jianTouShang)
                jianTouShang.active = true
                jianTouShang.parent = this.descView
            }else{
                for(let i=0;i<taskInfo.taskCaiGouDan.length;i++){
                    let goodId = taskInfo.taskCaiGouDan[i]
                    let item = cc.instantiate(this.itemNode)
                    item.parent = this.descView
                    item.active = true
                    this.refeshNode(item,goodId)
                }
    
                this.node.active = true
                let jianTouShang = cc.instantiate(this.jianTouShang)
                jianTouShang.active = true
                jianTouShang.parent = this.descView
            }
            return true
            
        }
        else{
            this.node.active = false
            return false
        }
    }
    onOpenDescView(){
        this.jianTouXia.active = false
        this.descView.active = true
    }
    onCloseDescView(){
        this.jianTouXia.active = true
        this.descView.active = false
    }

    refeshNodeBuyNum(node_, haveNum, allNum, taskGoodName){



        let nameLab = node_.getChildByName('goodName').getComponent(cc.Label)
        let numLab = node_.getChildByName('goodNum').getComponent(cc.RichText)


        nameLab.string = taskGoodName
        if(haveNum>=allNum){
            numLab.string = '<color=#67C119>'+allNum+'</c><color=#67C119>/'+allNum+'</color>'
        }else{
            numLab.string =  '<color=#D65C44>'+haveNum+'</c><color=#67C119>/'+allNum+'</color>'
        }

    }
    refeshNode(node_, goodId){
        let goodConfig = BagManager.getGoodsProperty(goodId)
        let userGoodData = BagManager.getBagGoodConfig(goodId)

  
        let name = goodConfig.goodName
        let buyNum = goodConfig.buyNum
 

        
        let haveNum = 0
        if(userGoodData){
            haveNum = userGoodData.goodNum
        }


        let nameLab = node_.getChildByName('goodName').getComponent(cc.Label)
        let numLab = node_.getChildByName('goodNum').getComponent(cc.RichText)


        nameLab.string = name
        if(haveNum>=buyNum){
            numLab.string = '<color=#67C119>'+buyNum+'</c><color=#67C119>/'+buyNum+'</color>'
        }else{
            numLab.string =  '<color=#D65C44>'+haveNum+'</c><color=#67C119>/'+buyNum+'</color>'
        }
       
    }
}