import { loadSprite } from "./loadSprite.js";
import { gameTime } from "./gameTime.js";

export class GameObject {
    constructor(id, options = {}) {
        this.id = id;

        // Position
        this.x = options.x || 0;
        this.y = options.y || 0;

        // Size
        this.width = options.width || null;
        this.height = options.height || null;

        // Direction
        this.direction = options.direction || 0;

        // Sprite
        this.sprite = loadSprite(options.spritePath || '');

        // Animation
        this.animationFrames = null;
        this.animationFrameDuration = options.animationFrameDuration || 360;
        this.animationStartTime = gameTime.now();
        this.animationOffsetMs = 0;
        if (options.animationFolderPath && options.animationFrameCount) {
            this.setAnimation(options.animationFolderPath, options.animationFrameCount);
        }
    }

    setPosition(x, y) {
        this.x = x;
        this.y = y;
    }

    setSize(width, height) {
        this.width = width;
        this.height = height;
    }

    setSprite(spritePath) {
        this.sprite = loadSprite(spritePath);
    }

    setAnimation(folderPath, frameCount) {
        this.animationFrames = [];
        for (let i = 0; i < frameCount; i++) {
            this.animationFrames.push(`${folderPath}/${i}.png`);
        }

        const animationLoopDuration = frameCount * this.animationFrameDuration;
        this.animationOffsetMs = Math.floor(Math.random() * animationLoopDuration);
    }

    draw(ctx, cam) {
        let relativeX = Math.round((this.x + cam.x) * cam.zoom);
        let relativeY = Math.round((this.y + cam.y) * cam.zoom);
        let width = Math.round(this.width * cam.zoom);
        let height = Math.round(this.height * cam.zoom);
        ctx.save();
        ctx.translate(relativeX, relativeY);
        ctx.rotate(this.direction * Math.PI / 180);
        if (this.animationFrames) {
            const elapsedAnimationTime = gameTime.now() - this.animationStartTime + this.animationOffsetMs;
            const frameIndex = Math.floor(elapsedAnimationTime / this.animationFrameDuration) % this.animationFrames.length;
            ctx.drawImage(loadSprite(this.animationFrames[frameIndex]), -width/2, -height/2, width, height);
        } else if (this.sprite) {
            ctx.drawImage(this.sprite, -width/2, -height/2, width, height);
        }
        ctx.restore();
    }

    drawAt(ctx, x, y, scale = 1) {
        if (this.animationFrames) {
            const elapsedAnimationTime = gameTime.now() - this.animationStartTime + this.animationOffsetMs;
            const frameIndex = Math.floor(elapsedAnimationTime / this.animationFrameDuration) % this.animationFrames.length;
            ctx.drawImage(loadSprite(this.animationFrames[frameIndex]), x, y, this.width * scale, this.height * scale);
        } else if (this.sprite) {
            ctx.drawImage(this.sprite, x, y, this.width * scale, this.height * scale);
        }
    }

}