const { ccclass, property } = cc._decorator

@ccclass
export default class ADmultiPanel extends cc.Component {

    private ns: { [key: string]: cc.Node }
    private touchCount = 0

    start() {
        this.ns = ZUI.getNodes(this.node)
        this.ns.btn_version.on(cc.Node.EventType.TOUCH_START, () => {
            console.log("click")
            this.touchCount++
            if (this.touchCount == 8) {
                this.show()
                this.setInfoVisible(true)
                this.updateView(ADmultiTool.getAdData(sdk.ANDROID_AD_CHANNEL.CSJ))
                this.updateView(ADmultiTool.getAdData(sdk.ANDROID_AD_CHANNEL.YLH))
                this.updateView(ADmultiTool.getAdData(sdk.ANDROID_AD_CHANNEL.KS))
            }
            if (this.touchCount >= 10) {
                this.hide()
                this.touchCount = 0
            }
        })

        this.ns.btn_csj.on(cc.Node.EventType.TOUCH_START, () => {
            sdk.showVideoAdMulti(sdk.ANDROID_AD_CHANNEL.CSJ, (channel: string) => {
                if (channel) {
                    this.updateView(ADmultiTool.saveEcpm(channel))
                }
            }, null)
        })

        this.ns.btn_ylh.on(cc.Node.EventType.TOUCH_START, () => {
            sdk.showVideoAdMulti(sdk.ANDROID_AD_CHANNEL.YLH, (channel: string) => {
                if (channel) {
                    this.updateView(ADmultiTool.saveEcpm(channel))
                }
            }, null)
        })

        this.ns.btn_ks.on(cc.Node.EventType.TOUCH_START, () => {
            sdk.showVideoAdMulti(sdk.ANDROID_AD_CHANNEL.KS, (channel: string) => {
                if (channel) {
                    this.updateView(ADmultiTool.saveEcpm(channel))
                }
            }, null)
        })

        this.ns.btn_showinfo.on(cc.Node.EventType.TOUCH_START, () => {
            this.setInfoVisible(!this.isInfoVisible())
        })

        this.ns.t_id.getComponent(cc.Label).string = "ID:" + sdk.getDeviceId()
        this.ns.t_date.getComponent(cc.Label).string = "日期:" + ZTime.getTimeYMD()
        this.hide()
        this.setInfoVisible(false)
    }

    updateView(data: AdData) {
        switch (data.channel) {
            case sdk.ANDROID_AD_CHANNEL.CSJ:
                this.ns.t_csj_price.getComponent(cc.Label).string = data.price + ""
                this.ns.t_csj_today_earning.getComponent(cc.Label).string = data.getTodayEarning() + ""
                this.ns.t_csj_count_today.getComponent(cc.Label).string = data.getCountToday() + ""
                this.ns.t_csj_count_month.getComponent(cc.Label).string = data.getCountMonth() + ""
                break;
            case sdk.ANDROID_AD_CHANNEL.YLH:
                this.ns.t_ylh_price.getComponent(cc.Label).string = data.price + ""
                this.ns.t_ylh_today_earning.getComponent(cc.Label).string = data.getTodayEarning() + ""
                this.ns.t_ylh_count_today.getComponent(cc.Label).string = data.getCountToday() + ""
                this.ns.t_ylh_count_month.getComponent(cc.Label).string = data.getCountMonth() + ""
                break;
            case sdk.ANDROID_AD_CHANNEL.KS:
                this.ns.t_ks_price.getComponent(cc.Label).string = data.price + ""
                this.ns.t_ks_today_earning.getComponent(cc.Label).string = data.getTodayEarning() + ""
                this.ns.t_ks_count_today.getComponent(cc.Label).string = data.getCountToday() + ""
                this.ns.t_ks_count_month.getComponent(cc.Label).string = data.getCountMonth() + ""
                break;
            default:
                break;
        }
        this.ns.t_date.getComponent(cc.Label).string = "日期:" + ZTime.getTimeYMD()
    }

    private setInfoVisible(b: boolean) {
        this.ns.node_info.active = b
    }
    private isInfoVisible(): boolean {
        return this.ns.node_info.active
    }

    private hide() {
        this.ns.root.opacity = 0
        this.ns.root.scale = 0
        this.ns.root.active = false
    }

    private show() {
        this.ns.root.active = true
        this.ns.root.opacity = 255
        this.ns.root.scale = 1
    }


}
