import { Point } from 'pixi.js';
import BaseMachine from '../slotMachines/BaseMachine';
import BaseSystem from '../systems/BaseSystem';
import { Entity } from '../types/Entity';

/**
 * @class GridSystem
 * @extends BaseSystem
 * @description Manages the positioning of symbols on a grid for a slot machine.
 */
export default class GridSystem extends BaseSystem
{
    /** Required utility paths. */
    static override requiredUtils: string[] = ['grid'];

    /** Required map keys. */
    static override requiredMaps: string[] = ['symbols'];

    /**
     * @class
     * @param {BaseMachine} machine - The slot machine instance.
     */
    constructor(machine: BaseMachine)
    {
        super(machine);
    }

    /** Updates the positions of all symbols in the grid. */
    override update(): void
    {
        if (!this.machine.maps.symbols)
        {
            this.machine.maps.symbols = [];
        }

        this.machine.maps.symbols.forEach((name: string, i: number) =>
        {
            this._updatePosition(name, i);
        });
    }

    /**
     * Updates the position of a symbol based on its grid index.
     * @private
     * @param {string} name - The name of the symbol to position.
     * @param {number} i - The index in the grid.
     */
    private _updatePosition(name: string, i: number): void
    {
        const symbol: Entity = this.machine.getChildByName(name);

        if (!symbol) return;

        const point: Point = this.machine.utils.grid.fromMapIndexToXY(i);

        if (point)
        {
            point.copyTo(symbol.position);
        }
    }

    onEnter(): void
    {
    }

    onLeave(): void
    {
    }
}
