import { eventTarget, SHOW_ACTION_SCREEN, SHOW_SELECT_POPUP, SHOW_START_SCREEN } from "../Events";
import { Global } from "../Global";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StartScreen extends cc.Component {
    @property(cc.Node)
    private selectBtnNode: cc.Node = null;

    @property(cc.Node)
    private contentNode: cc.Node = null;
    @property(cc.Node)
    private checkedNode: cc.Node = null;


    protected onLoad(): void {
        this.selectBtnNode.on('click', this.onSelectBtnClick, this);

        this.contentNode.children.forEach((item: cc.Node, index: number) => {
            item.on(cc.Node.EventType.TOUCH_START, () => this.onItemNodeClick(item, index), this);
        });

        eventTarget.on(SHOW_START_SCREEN, () => this.node.active = true, this);
        eventTarget.on(SHOW_ACTION_SCREEN, () => this.node.active = false, this);
    }

    private onItemNodeClick(item: cc.Node, index: number) {
        this.checkedNode.active = true;
        this.checkedNode.setPosition(item.getPosition());
        Global.tool = index;
    }


    private onSelectBtnClick() {
        eventTarget.emit(SHOW_SELECT_POPUP);
    }


}
