// 储物区
import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import GameReport from "../../FrameWork/Report/ZyZyReport";
import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import List from "../../Platform/th/List";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import BagManager, { GoodsLocalProperty } from "../Manager/BagManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import GameBase from "../SCommon/GameBase";
import CangKuItem from "./CangKuItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BagScript extends GameBase {

    @property(List)
    listG: List = null; //物品
   
    @property(cc.Node)
    listGContent: cc.Node = null;
   
    
    // @property(List)
    // listS: List = null; //原料

    // @property(cc.Node)
    // listSContent: cc.Node = null;
   
    // LIFE-CYCLE CALLBACKS:
    
    @property(List)
    listP: List = null; //食谱

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

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

    @property(cc.Node)
    cangBaoTu: cc.Node = null;
 
    curTaskGoodId = null
    tiJiaoCallFunc = null

    chooseGoodId = null
    // onLoad () {}
    dataListG = []
    dataListS = []
    dataListP = []

    needHandin = false
    maskGuideNode:cc.Node = null
    onDestroy(){
        if(cc.isValid(this.maskGuideNode)){
            this.maskGuideNode.removeFromParent()
            this.maskGuideNode.destroy()
        }
        super.onDestroy()
    }

    onLoad () {
        super.onLoad()
    }

    start () {
        //背包默认放入1710


        this.setListG()
        EventMgr.onEvent_custom(ryw_Event.ViewpPoperty,(goodId,goodNum)=>{
            this.reportKey(()=>{
                GameReport.BtnsReport('点击道具')
            })  
            this.chooseGoodId = goodId
            console.log(goodId, 'goodId++++++++++++===')
            this.descView(goodId, goodNum)
        },this)

        
        EventMgr.onEvent_custom(ryw_Event.updateBagDate,()=>{
            this.refreshView()
        },this)
        this.guideStep()
        EventMgr.onEvent_custom(ryw_Event.RefreshJuQingDuiHua,()=>{
            this.guideStep()
        },this)
    }
    
    guideStep(){
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        let isHave1706 = (BagManager.getBagGoodNums(1706)>0)//是否有乌鸡
        //狩猎乌鸡回家
     
    }
    guideView(nodeArray){
        if(this.maskGuideNode == null){
            PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab)=>{
                let guideNodeArray = nodeArray
                this.maskGuideNode = prefab
                let firstNode = guideNodeArray.shift()
                prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            })
        }else{
            this.maskGuideNode.active = true
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            this.maskGuideNode.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
        }
    }
    setHandIn(needHandin,taskGoodId,tiJiaoCallFunc){
        this.needHandin = needHandin
        this.curTaskGoodId = taskGoodId
        this.tiJiaoCallFunc = tiJiaoCallFunc
    }

    refreshView(){
        let goodNum = BagManager.getBagGoodNums(this.chooseGoodId)
        if(goodNum <= 0){
            
            this.descNode.active = false
        }
        if(this.listG.node.active){
            this.setListG()
        }

        // if(this.listS.node.active){
        //     this.setListQ()
        // }

        if(this.listP.node.active){
            this.setListP()
        }

        if(this.cangBaoTu.active){
            this.setCangBaoTu()
        }

    }

    descView(goodId,goodNum){
        this.descNode.active = true
        let  labName = this.descNode.getChildByName('labName').getComponent(cc.Label)
        let  labDesc = this.descNode.getChildByName('labDesc').getComponent(cc.Label)
        let  labPrice = this.descNode.getChildByName('labPrice').getComponent(cc.Label)
        let  icon = this.descNode.getChildByName('icon').getComponent(cc.Sprite)
        let _pro = BagManager.getGoodsProperty(goodId)
        labName.string = _pro.goodName
        let goodDescData = BagManager.getGoodsDesc(goodId)
        labDesc.string = goodDescData.descStr
        //Common5.getSpriteFrameFromBundle("CommonRes","GoodIcon/texture/icon/"+ goodId, icon); 

        Common5.addUrlSprite_custom("GoodIcon/texture/icon/"+ goodId, icon)

        labPrice.string = '售价:'+Common5.getNumberChangeHanzi( _pro.priceNum)+''

        let  numLab = this.descNode.getChildByName('numLab').getComponent(cc.Label)
        numLab.string = goodNum+''

        if(this.needHandin){
            
            if(goodId == this.curTaskGoodId){
                this.descNode.getChildByName('tijiaoBtn').active = true
            }else{
                this.descNode.getChildByName('tijiaoBtn').active = false
            }
        }else{
            this.descNode.getChildByName('tijiaoBtn').active = false
        }
    }

    onBtnCloseClick(){

        //Common5.playEffect("sound/按键点击")
        this.node.removeFromParent()
        this.node.destroy()
    }
    
    setListG(){
        this.listGContent.removeAllChildren()

        let getBagList = BagManager.getBagProList()

        //console.log(getBagList, 'getBagList++++++++++++===')

   
        this.dataListG = []
        for (let n = 0; n < getBagList.length; n++) {
            //let goodId = getBagList[n].goodId
            // let goodData = BagManager.getGoodsProperty(goodId)
            
            this.dataListG.push(getBagList[n]);
         
        
        }
        if(this.dataListG.length<=12){
            this.listG.numItems = 12;
        }else{
            this.listG.numItems = this.dataListG.length;
        }
      
       
    }

    setListQ(){
        // this.listSContent.removeAllChildren()
        // this.dataListS = []
        // let getBagList = BagManager.getBagList()
        // if(this.needHandin){
        //     let num = BagManager.getBagGoodNums(this.curTaskGoodId)
        //     getBagList = [
        //         {
        //             goodId: this.curTaskGoodId,
        //             goodNum: num,
        //         }
        //     ]
        // }

        // for (let n = 0; n < getBagList.length; n++) {
        //     this.dataListS.push(getBagList[n]);
        // }

        // this.listS.numItems = this.dataListS.length;
 
    }

    setListP(){
        this.listPContent.removeAllChildren()
        this.dataListP = []
        let getBagList = BagManager.getBagTaskProList()

        for (let n = 0; n < getBagList.length; n++) {
      
            this.dataListP.push(getBagList[n]);
             
        
        }
   
        if(this.dataListP.length<=12){
            this.listP.numItems = 12;
        }else{
            this.listP.numItems = this.dataListP.length;
        }
        //this.listP.numItems = this.dataListP.length;
  
    
    }
    setCangBaoTu(){
    
        
    }

    firstXiangQing(numItems, goodId, isCaidan){
        //选中第一个
        if(goodId){
            this.descView(goodId,isCaidan)
        }
 
    }

    chooseClickEvent(event, data){
        if(data == 'listG'){
        
     
            this.listG.node.active = true
            //this.listS.node.active = false
            this.listP.node.active = false
            this.cangBaoTu.active = false
            this.setListG()
        }else if(data == 'listS'){
            
          
            //this.listS.node.active = true
            this.listG.node.active = false
            this.listP.node.active = false
            this.cangBaoTu.active = false
            this.setListQ()
        }else if(data == 'listP'){
            
       
            //this.listS.node.active = false
            this.listG.node.active = false
            this.listP.node.active = true
            this.cangBaoTu.active = false
            this.setListP()
        }else if(data == 'listD'){
            //this.listS.node.active = false
            this.listG.node.active = false
            this.listP.node.active = false
            this.cangBaoTu.active = true
            this.setCangBaoTu()
        }
    }


    onListGGridRender(item: cc.Node, idx: number) {
        //item.getComponent(ListItem).title.getComponent(cc.Label).string = this.data[idx] + '';

        let config =  this.dataListG[idx]
        let danItem:CangKuItem = item.getComponent('CangKuItem')
        danItem.initView(config)
    }


    onListSGridRender(item: cc.Node, idx: number) {
        //item.getComponent(ListItem).title.getComponent(cc.Label).string = this.data[idx] + '';

        let config =  this.dataListS[idx]
        let danItem:CangKuItem = item.getComponent('CangKuItem')
        danItem.initView(config)
    }

    onListPGridRender(item: cc.Node, idx: number) {
        //item.getComponent(ListItem).title.getComponent(cc.Label).string = this.data[idx] + '';

        let config =  this.dataListP[idx]
        let danItem:CangKuItem = item.getComponent('CangKuItem')
        danItem.initView(config)
    }


    closeDetailClickEvent(){
        this.descNode.active = false
    }


    tijiaoBtnClickEvent(){
        //狩猎乌鸡回家提交
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id

        BagManager.subBagList(this.curTaskGoodId)
        //更新任务

        this.descNode.active = false
        this.tiJiaoCallFunc && this.tiJiaoCallFunc()
        
        this.node.removeFromParent()
        this.node.destroy()
    }

    closeBtnClickEvent(){
        this.node.removeFromParent()
        this.node.destroy()
    }
    // update (dt) {}
}