import { assert } from "../../../../core/assert.js";
import { Behavior } from "../Behavior.js";
import { BehaviorStatus } from "../BehaviorStatus.js";

/**
 * Wait for a certain amount of time, then succeed
 * @author Alex Goldring
 * @copyright Company Named Limited (c) 2026
 */
export class DelayBehavior extends Behavior {

    /**
     * Delay value in seconds, how long should we wait?
     * @type {number}
     */
    value = 0;

    /**
     * Time elapsed
     * Must be finite, modified during update and initialization
     * @type {number}
     */
    elapsed = 0;

    fromJSON({ value, elapsed = 0 }) {
        assert.isNumber(value, 'value');
        assert.isNumber(elapsed, 'elapsed');

        this.value = value;
        this.elapsed = elapsed;
    }

    /**
     *
     * @param json
     * @return {DelayBehavior}
     */
    static fromJSON(json) {
        const r = new DelayBehavior();
        r.fromJSON(json);
        return r;
    }

    /**
     *
     * @param {number} seconds in seconds
     * @return {DelayBehavior}
     */
    static from(seconds) {
        assert.isNumber(seconds, 'value');
        assert.notNaN(seconds, 'value');
        assert.isFinite(seconds, 'value');

        const r = new DelayBehavior();

        r.value = seconds;

        return r;
    }

    initialize(ctx) {
        super.initialize(ctx);

        this.elapsed = 0;
    }

    tick(timeDelta) {
        this.elapsed += timeDelta;

        if (this.elapsed >= this.value) {
            return BehaviorStatus.Succeeded;
        }

        return BehaviorStatus.Running;
    }
}
