import { WinLineRecord, WinsData } from '../types/WinsData';
import BaseSystem from './BaseSystem';

/**
 * @class AllWinningSymbolSystem
 * @description
 * System responsible for marking winning symbols in the `winningSymbols` map.
 *
 * This system:
 * - Reads the `wins` input (`WinsData`) when entering the state.
 * - Iterates through all winning lines and marks corresponding symbols as winning.
 * - Uses `grid.fromCRtoMapIndex()` to convert reel/slot positions into map indices.
 *
 * This system does **not** handle animations or visual effects, it only updates the state.
 */
export default class AllWinningSymbolSystem extends BaseSystem
{
    public state: string = 'stopped';

    static override requiredMaps: string[] = ['winningSymbols'];
    static override requiredInputs: string[] = ['wins'];

    override onEnter()
    {
        this.checkIsMapSetup();
        if (!this.machine.inputs.wins) return;
        this.processWinMap();
    }

    override onLeave()
    {
    // Do nothing
    }

    update(_dt: number)
    {
    // Do nothing
    }

    private processWinMap()
    {
        const winsData: WinsData = this.machine.inputs.wins;

        winsData.wins.forEach((win: WinLineRecord) =>
        {
            win.lineSlotsIndexMap.forEach((slot: number, reel: number) =>
            {
                const index = this.machine.utils.grid.fromCRtoMapIndex(reel, slot);

                this.machine.maps.winningSymbols[index] = true;
            });
        });
    }

    private checkIsMapSetup()
    {
        if (!this.machine.maps.winningSymbols || this.machine.maps.winningSymbols.length === 0)
        {
            this.machine.maps.winningSymbols = new Array(this.machine.maps.symbols.length).fill(false);
        }
    }
}

