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

const { ccclass, property } = cc._decorator;

@ccclass
export default class MultipleChooseLayer extends cc.Component {

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

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

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

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

    @property()
    delayTime: number = 1;

    private isStart = false;
    private touchIndexs: Set<number> = null;

    // onLoad () {}

    // protected onEnable(): void {
    // }

    // start() {}

    setChooseBtnView(chooseArray: string[], chooseTitle?: string, canClose?: boolean) {
        this.btnNodeContent.removeAllChildren();
        this.btnNode.active = false;
        this.isStart = true;
        this.touchIndexs = new Set();
        this.node.active = true;

        for (let i = 0; i < chooseArray.length; i++) {
            let btnNode = cc.instantiate(this.btnNode);
            btnNode.active = true;
            this.btnNodeContent.addChild(btnNode);
            btnNode.setPosition(cc.v2(0, 0));

            let lab = btnNode.getChildByName('lab').getComponent(cc.Label);
            lab.string = chooseArray[i];
            btnNode['touchIndex'] = i;
        }

        this.titleNode.parent.active = false;
        if (chooseTitle) {
            this.titleNode.parent.active = true;
            this.titleNode.active = true;
            this.titleNode.getComponent(cc.Label).string = chooseTitle;
        }

        this.closeBtn.active = false;
        if (this.closeBtn) {
            this.closeBtn.active = canClose;
        }
    }

    closeBtnClickEvent() {
        Common5.playEffect("点击音效");
        this.node.active = false;
        EventMgr.emitEvent_custom(ryw_Event.chooseResult, { touchIndexs: [-1] });
    }

    chooseBtnClickEvent(event) {
        Common5.playEffect("点击音效");
        let target = event.target;
        this.touchIndexs.add(target['touchIndex']);
        this.startTimer();
    }

    startTimer() {
        if (this.isStart) {
            this.isStart = false;
            this.scheduleOnce(() => {
                EventMgr.emitEvent_custom(ryw_Event.chooseResult, { touchIndexs: this.touchIndexs });
                console.log('ChooseBtnLayer chooseBtnClickEvent++++++++====');
                this.node.active = false;
            }, this.delayTime);
        }
    }
}