export class Message {
    constructor(options = {}) {
        this.direction = options.direction || "right-to-left";
        this.speed = options.speed || 90;
        this.endLeft = options.endLeft || 0;
        this.endRight = options.endRight || 0;
        this.color = options.color || "#C0C0C0";
        this.currentText = options.initialText || "";
        this.pendingText = null;
        this.position = 0;
        this.textWidth = 0;
        this.lastTimestamp = null;
        this.needsRestart = true;
        this.idleMessages = options.idleMessages || ["To all ants, have a nice day!"];
    }

    setDirection(direction) {
        this.direction = direction === "right-to-left" ? "right-to-left" : "left-to-right";
        this.needsRestart = true;
    }

    setSpeed(speed) {
        if (Number.isFinite(speed) && speed > 0) {
            this.speed = speed;
        }
    }

    setBounds(endLeft, endRight) {
        this.endLeft = endLeft;
        this.endRight = endRight;
        this.needsRestart = true;
    }

    setNextMessage(text) {
        this.pendingText = String(text ?? "");
    }

    getRandomIdleMessage() {
        if (!Array.isArray(this.idleMessages) || this.idleMessages.length === 0) {
            return "";
        }

        const randomIndex = Math.floor(Math.random() * this.idleMessages.length);
        return String(this.idleMessages[randomIndex] ?? "");
    }

    getStartPosition() {
        if (this.direction === "right-to-left") {
            return this.endRight;
        }

        return this.endLeft - this.textWidth;
    }

    getExitPosition() {
        if (this.direction === "right-to-left") {
            return this.endLeft - this.textWidth;
        }

        return this.endRight;
    }

    restartCycle(ctx) {
        const nextText = this.pendingText !== null ? this.pendingText : this.getRandomIdleMessage();

        this.currentText = nextText || "";
        this.pendingText = null;
        this.textWidth = ctx.measureText(this.currentText).width;
        this.position = this.getStartPosition();
        this.needsRestart = false;
    }

    update(ctx, timestamp) {
        if (this.needsRestart) {
            this.restartCycle(ctx);
        }

        if (this.lastTimestamp === null) {
            this.lastTimestamp = timestamp;
            return;
        }

        const deltaSeconds = Math.max(0, (timestamp - this.lastTimestamp) / 1000);
        const directionMultiplier = this.direction === "right-to-left" ? -1 : 1;

        this.position += directionMultiplier * this.speed * deltaSeconds;
        this.lastTimestamp = timestamp;

        if (
            (directionMultiplier > 0 && this.position >= this.getExitPosition())
            || (directionMultiplier < 0 && this.position <= this.getExitPosition())
        ) {
            this.restartCycle(ctx);
        }
    }

    draw(ctx, x, y, width, height, timestamp = performance.now()) {
        this.update(ctx, timestamp);

        ctx.save();
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.rect(x, y, width, height);
        ctx.clip();
        ctx.fillText(this.currentText, x + this.position, y + height / 2);
        ctx.restore();
    }
}

export const sharedMessage = new Message({
    direction: "right-to-left",
    speed: 90,
    endLeft: 0,
    endRight: 878,
    idleMessages: [
        "To all ants, have a nice day!",
        "Remember to take breaks and stretch your legs!",
        "Don't forget to drink water and stay hydrated!",
        "Keep up the great work, ants!",
        "Teamwork makes the dream work!",
        "Every ant counts, including you!",
        "Take a moment to enjoy the sunshine!",
        "Your efforts are appreciated, ants!",
    ]
});