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

/**
 * @class GravityHook
 * @extends Hook
 * @description Hook that updates an entity's position, simulating gravity until it reaches the target Y position.
 */
class GravityHook extends Hook
{
    private entity: Entity;
    private targetY: number;
    private gravity: number;
    private completed: boolean;

    /**
     * @class
     * @param {Entity} entity - The entity object whose position is being updated.
     * @param {number} targetY - The target Y position to reach.
     * @param {number} gravity - The gravitational acceleration in units per second^2.
     */
    constructor(entity: Entity, targetY: number, gravity: number)
    {
        super();
        this.entity = entity;
        this.targetY = targetY;
        this.gravity = gravity;
        this.completed = false;
    }

    /**
     * Updates the entity's position, applying gravity until it reaches the target Y position.
     * @param {number} dt - Delta time in milliseconds since the last update.
     */
    update(dt: number): void
    {
        if (this.completed) return;

        this.entity.y = Math.min(this.entity.y + (this.gravity * (dt * 0.001)), this.targetY);
        if (this.entity.y >= this.targetY)
        {
            this.entity.y = this.targetY;
            this.completed = true;
        }
    }

    /**
     * Checks if the hook has completed its task (i.e., the entity has reached the target Y position).
     * @returns {boolean} - Returns `true` if the hook is complete, otherwise `false`.
     */
    isComplete(): boolean
    {
        return this.completed;
    }
}

export default GravityHook;
