// 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 User from "../../FrameWork/User/User";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import TaskManager from "../JuQingChat/TaskManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class selectButtons extends cc.Component {

    @property(cc.Node)
    button: cc.Node = null;
    @property(cc.Node)
    bigbutton: cc.Node = null;

    @property(cc.Node)
    buttonparent: cc.Node = null;
    // @property
    // text: string = 'hello';
    @property(cc.Label)
    title: cc.Label = null;


    m_callfunc = []
    m_isvideo = []
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
    }

    /**
     * 
     * @param strs 文字提示数组
     * @param actives 按钮显示数组
     * @param funcs 回调数组
     * @param isvideo 是否显示播放数组
     */
    init(strs: string[], actives: boolean[], funcs: Function[], isvideo: boolean[]) {
        this.button.active = false
        this.bigbutton.active = false
        this.m_callfunc = funcs
        this.m_isvideo = isvideo
        let maxsize = 0
        for (let i = 0; i < strs.length; i++) {
            if (actives[i]) {
                let kuang = cc.instantiate(this.button)
                kuang.active = true
                let button = kuang.getChildByName("blueBtn")
                button.attr({ buttonIndex: i })
                this.buttonparent.addChild(kuang)
                let lab = button.getChildByName("lab").getComponent(cc.Label)
                lab.string = strs[i]
                lab["_forceUpdateRenderData"](true);
                let labsize = lab.node.getContentSize()
                let btnsizewidth = labsize.width + 80
                if (maxsize < btnsizewidth) {
                    maxsize = btnsizewidth
                }
                button.setContentSize(cc.size(btnsizewidth, button.getContentSize().height))
                button.getChildByName("播放").getComponent(cc.Widget).updateAlignment()
                if (!isvideo[i]) {
                    button.getChildByName("播放").active = false
                    lab.node.setPosition(cc.v3(0, 0, 0))
                }
                if (i % 2 == 1) {
                    button.getChildByName("red").active = true
                }
            } else {
                this.bigbutton.active = true
                this.bigbutton.attr({ buttonIndex: i })
            }
        }

        for (let i = 0; i < this.buttonparent.children.length; i++) {
            let button = this.buttonparent.children[i].getChildByName("blueBtn")
            button.setContentSize(cc.size(maxsize, button.getContentSize().height))
            button.getChildByName("播放").getComponent(cc.Widget).updateAlignment()
        }
    }

    setTitle(str) {
        this.title.string = str
        this.title.node.active = true
        this.buttonparent.getComponent(cc.Layout).updateLayout()
        this.title.node.setPosition(cc.v3(this.title.node.position.x, this.buttonparent.position.y + this.buttonparent.height + 50, 0))
    }

    onTouch(event) {
        let node = event.target
        let index = node.buttonIndex
        if (this.m_isvideo[index]) {
            let tab = {
                onClose: (finish) => {
                    if (finish) {
                        User.setShowAdNum(User.getShowAdNum() + 1)
                        Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-剧情选择按钮观看视频`)
                        // Common5.ReportDY("inLevel", '选择框-AD-剧情选择框看视频');
                        if (this.m_callfunc[index]) {
                            this.node.removeFromParent()
                            this.node.destroy()
                            this.m_callfunc[index]()
                        }
                    }
                    else {
                        Common5.showTips_custom("广告未观看完");
                    }
                }, onFailed: () => {

                }
            }
            AppPlatform.playVideo_custom(tab)
        } else {
            if (this.m_callfunc[index]) {
                this.node.removeFromParent()
                this.node.destroy()
                this.m_callfunc[index]()
            }
        }
    }
    // update (dt) {}
}