export default abstract class Entity {
    public hovered = false;

    constructor(public position: [number, number], public size: [number, number]) { }

    public render(ctx: CanvasRenderingContext2D) {
        ctx.save();

        ctx.translate(...this.position);
        ctx.scale(...this.size);

        this.draw(ctx);

        ctx.restore();
    }

    public isPointInBounds(x: number, y: number) {
        return (
            this.left <= x && x <= this.right &&
            this.top <= y && y <= this.bottom
        );
    }

    get centerX() { return this.position[0] + this.size[0] / 2; }
    get centerY() { return this.position[1] + this.size[0] / 2; }

    get left() { return this.position[0]; }
    get top() { return this.position[1]; }

    get right() { return this.position[0] + this.size[0]; }
    get bottom() { return this.position[1] + this.size[1]; }

    get width() { return this.size[0]; }
    get height() { return this.size[1]; }

    public handleClick(x: number, y: number) {
        if (this.isPointInBounds(x, y)) {
            this.onClick(x - this.left, y - this.right);
        }
    }

    public handleMouseMove(x: number, y: number) {
        this.hovered = this.isPointInBounds(x, y);
    }

    protected abstract draw(ctx: CanvasRenderingContext2D): void;
    protected onClick(_x: number, _y: number) { }
    public update(_dt: number) { };
}