import { equal, Vector } from './vector';
import { Character } from './characters/character';

type ConstructorProps = {
	x: number;
	y: number;
	w: number;
	h: number;
	speed: number;
	health: number;
	power: number;
	body: Character;
};

class Unit {
	private pos: [number, number];
	public readonly width;
	public readonly height;
	private _speed = 0.1;
	private _power = 1;
	private _health = 3;
	private body: Character;

	private initialData: ConstructorProps;

	constructor(props: ConstructorProps) {
		this.pos = [props.x, props.y];
		this._speed = props.speed;
		this.body = props.body;
		this.width = props.w;
		this.height = props.h;
		this._health = props.health;
		this._power = props.power;

		this.initialData = { ...props };
	}

	get x() {
		return this.pos[0];
	}

	get y() {
		return this.pos[1];
	}

	get position() {
		return this.pos;
	}

	get speed() {
		return this._speed;
	}

	get power() {
		return this._power;
	}

	get health() {
		return this._health;
	}

	move(pos: Vector) {
		this.pos = pos;
	}

	hit(power: number) {
		this._health = Math.max(0, this._health - power);
	}

	draw(ctx: CanvasRenderingContext2D) {
		ctx.save();
		ctx.translate(Math.round(this.pos[0]), Math.round(this.pos[1]));
		this.body.draw(ctx);

		// ctx.strokeStyle = 'red';
		// ctx.rect(-this.width / 2, -this.height, this.width, this.height);
		// ctx.stroke();

		ctx.restore();
	}

	reset() {
		this.pos = [this.initialData.x, this.initialData.y];
		this._speed = this.initialData.speed;
		this.body = this.initialData.body;
		this._health = this.initialData.health;
		this._power = this.initialData.power;
	}
}

export default Unit;
