import Reel from '../../systems/reels/Reel';
import AnimationProgressHook from '../../utils/hooks/AnimationProgressHook';
import { REEL_STATES } from './states';
import { State } from '@popiplay/state-machine';

/**
 * @class Deceleration
 * @extends State
 * @description Represents the bouncing phase of a reel. In this state, the symbols bouncing
 */
export default class Bouncing extends State
{
    private reel: Reel;
    private hooks: AnimationProgressHook[] = [];
    /**
     * @class
     * @param {Reel} reel - The reel instance that this state belongs to.
     */
    constructor(reel: Reel)
    {
        super('Bouncing');
        this.reel = reel;
    }

    override enter()
    {
        this.hooks = [];
        if (!this.reel.target.settings.bounce) return;
        this.hooks = this.reel.bounce();
    }

    override update(_dt: number): void
    {
        this.hooks.forEach((hook) => hook.update(_dt));
        this.reel.position = this.reel.stopPosition;
    }

    /**
     * Handles the transition to the stopped state once the reel reaches its stop position.
     * @param {unknown} _input - Optional input for state handling, unused in this implementation.
     */
    override handleInput(_input: unknown): void
    {
        if (this.hooks.every((hook) => hook.isComplete()))
        {
            this.reel.setState(REEL_STATES.STOPPED); // Transition to the STOPPED state
        }
    }
}
