import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import GameReport from "../../../FrameWork/Report/ZyZyReport";
import Common5 from "../../../Platform/th/Common5";
import ChaDianScript from "./ChaDianScript";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ChaDianListScript extends cc.Component {

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

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

    chadianData = []

    protected onDisable(): void {
        console.log('onEnable++++')
        EventMgr.offEvent_custom(ryw_Event.touchChaDianCheck, this.checkFunc, this)
    }

    checkFunc = null

    start() {
        //监听
        this.checkFunc = (data_) => {
            this.refreshChaDian(data_.touchIndex)
        }
        EventMgr.onEvent_custom(ryw_Event.touchChaDianCheck, this.checkFunc, this)


        let scrollview = this.node.getChildByName('scrollView')
        scrollview.getComponent(cc.Sprite).enabled = false

    }

    setChaDianData(chadianData) {

        this.node.getChildByName('scrollView').height = 140
        this.node.getChildByName('scrollView').getChildByName('view').height = 140

        if (chadianData.length < 5) {
            let scrollView = this.node.getChildByName('scrollView')
            scrollView.width = chadianData.length * 135
            let view = scrollView.getChildByName('view')
            view.width = chadianData.length * 135
        }

        this.chadianData = chadianData
        for (let i = 0; i < chadianData.length; i++) {
            let chadianNode = cc.instantiate(this.chadianNode)
            chadianNode.active = true
            this.chadianNodeContent.addChild(chadianNode)
            chadianNode.setPosition(cc.v2(0, -23))

            let fugaiNode = chadianNode.getChildByName("fugaiNode")
            fugaiNode.active = true

            let scr: ChaDianScript = chadianNode.getComponent('ChaDianScript')
            scr.setChadianIconData(chadianData[i])


        }



    }

    //茶点刷新
    refreshChaDian(index) {
        let node = this.chadianNodeContent.children[index]
        let labName = node.getChildByName("labName")
        let icon = node.getChildByName("icon")
        let fugaiNode = node.getChildByName("fugaiNode")
        fugaiNode.active = false
        labName.active = true
        icon.active = true

        let scrollview = this.node.getChildByName('scrollView')
        if (scrollview) {
            let percent = index / this.chadianNodeContent.childrenCount
            scrollview.getComponent(cc.ScrollView).scrollToPercentHorizontal(percent, 0.3)
        }

        let data_ = this.chadianData[index]
        if(Common5.selectGameInfo){
            GameReport.BtnsReport(data_.name, Common5.selectGameInfo.titleUrl);
        }
        

 
        console.log("[GameReport]++++++++++++++++++++++>" + data_.name);
        if (data_.effectUrl) {
            Common5.playEffectCustom('WordGame', data_.effectUrl)
        }

    }

    // update (dt) {}
}