import { eventTarget, RESET_BOTTLE, SHOW_SLOT_JAR } from "../Events";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SlotJar extends cc.Component {
    private _slotBreak2thNodes: cc.Node[] = [];
    private _slotBreak1thNodes: cc.Node[] = [];
    private _slotShowder: { [key: number]: number } = {};

    protected onLoad(): void {
        this._slotBreak1thNodes = this.node.children;
        this._slotBreak2thNodes = this._slotBreak1thNodes.map(node => node.children[0]);

        this.resetSlots();
        eventTarget.on(RESET_BOTTLE, this.resetSlots, this);
        eventTarget.on(SHOW_SLOT_JAR, this.showSlotByIndex, this);
    }

    private showSlotByIndex(indexs: number[], breakCount: number): void {
        indexs.forEach(index => {
            if(this._slotShowder[index] > 1){
                return;
            }
            this._slotShowder[index] = breakCount;
            this._slotBreak1thNodes[index].active = true;
            this._slotBreak2thNodes[index].active = breakCount >= 2;
        })
    }

    public showSlots(breakCount: number): void {
        this._slotBreak1thNodes.forEach(node => node.active = true);
        this._slotBreak2thNodes.forEach(node => node.active = breakCount >= 2);
    }

    private resetSlots(): void {
        this._slotBreak1thNodes.forEach(node => node.active = false);
        this._slotBreak2thNodes.map(node => node.active = false);
        this._slotShowder = {};
    }
}
