import { ITrackEntry } from '../../types/ITrackEntry';
import Hook from './Hook';

/**
 * @class AnimationProgressHook
 * @extends Hook
 * @description Hook that tracks the progress of an animation based on its track time and duration.
 */
class AnimationProgressHook extends Hook
{
    private entry: ITrackEntry;
    private callback: () => ITrackEntry;
    /**
     * @class
     * @param onStart
     */
    constructor(onStart: () => ITrackEntry)
    {
        super();
        this.callback = onStart;
    }

    /**
     * Updates the animation's progress. No specific update action is required,
     * as the animation progress is tracked automatically by the entry's track time.
     * @param {number} _dt - Delta time since the last update in milliseconds.
     */
    update(_dt: number): void
    {
        if (!this.entry) this.entry = this.callback();
    }

    /**
     * Checks if the animation has completed.
     * @returns {boolean} - Returns `true` if the animation is complete, otherwise `false`.
     */
    isComplete(): boolean
    {
        return this.checkAnimationProgress() >= 1;
    }

    /**
     * Calculates the progress of the animation.
     * @returns {number} - Returns a progress value between 0 and 1.
     */
    private checkAnimationProgress(): number
    {
        if (!this.entry || !this.entry.animation) return 1;

        return Math.min(this.entry.trackTime / this.entry.animation.duration, 1);
    }
}

export default AnimationProgressHook;
