import { DisplayObject } from 'pixi.js';
import { Entity } from 'src/types/Entity';
import BaseSystem from '../systems/BaseSystem';

export default class LayeringSystem extends BaseSystem
{
    public state: 'watching' | 'stopped' = 'stopped';

    /** Static list of flags that the machine should provide. Subclasses can override to specify their own requirements. */
    static override requiredFlags: string[] = ['isSpinning'];

    /** Static list of maps that the machine should provide. Subclasses can override to specify their own requirements. */
    static override requiredMaps: string[] = ['symbols'];

    /** Static list of inputs that the machine should provide. Subclasses can override to specify their own requirements. */
    static override requiredInputs: string[] = [];

    /** Static list of methods that the machine should provide. Subclasses can override to specify their own requirements. */
    static override requiredMethods: string[] = [];

    /** Static list of utils that the machine should provide. Subclasses can override to specify their own requirements. */
    static override requiredUtils: string[] = [];

    override update(_dt: number): void
    {
        if (this.state === 'stopped') return;

        this.machine.children.forEach((entity: DisplayObject) =>
        {
            const e = entity as Entity;

            if (!e.key) return;
            if (this.machine.config.symbols[e.key])
            {
                entity.zIndex = this.machine.config.symbols[e.key].zIndex || 0;
            }
        });

        if (!this.machine.flags.isSpinning)
        {
            this.state = 'stopped';

            return;
        }
    }
    override onEnter(): void
    {
        this.state = 'watching';
    }

    override onLeave(): void
    {

    }
}
