import Sprite from '../sprite';
import Input from '../input';

const WeaponKeys = ['knife', 'mace', 'flask'];

class Upgrade {
	private selectedItem = 0;
	private lastKey = '';

	constructor(private sprite: Sprite) {}

	update(input: Input): string | void {
		if (input.left) {
			if (this.lastKey !== 'left' && this.lastKey !== '-') {
				this.selectedItem = (this.selectedItem + 3 - 1) % 3;
				this.lastKey = 'left';
			}
		} else if (input.right) {
			if (this.lastKey !== 'right' && this.lastKey !== '-') {
				this.selectedItem = (this.selectedItem + 1) % 3;
				this.lastKey = 'right';
			}
		} else if (input.enter) {
			if (this.lastKey !== 'enter' && this.lastKey !== '-') {
				this.lastKey = 'enter';
				return WeaponKeys[this.selectedItem];
			}
		} else {
			this.lastKey = '';
		}
	}

	draw(ctx: CanvasRenderingContext2D) {
		ctx.fillStyle = 'white';
		ctx.font = '12px sans-serif';
		ctx.textAlign = 'center';
		ctx.fillText('новый уровень'.toUpperCase(), 64, 50);

		this.sprite.draw(ctx, 20, 80, 'weapon_knife');
		this.sprite.draw(ctx, 60, 72, 'weapon_mace');
		this.sprite.draw(ctx, 97, 75, 'flask_blue');

		ctx.strokeStyle = 'white';
		ctx.strokeRect(9 + this.selectedItem * 40, 70, 30, 30);
	}

	reset() {
		this.selectedItem = 0;
		this.lastKey = '-';
	}
}

export default Upgrade;
