import { SimCommand } from '../../sim/commands/SimCommand';
import { Hex } from '../../util/Hex';
import { VisMain } from '../VisMain';
import { InputHandler } from './InputHandler';

export class HexInteractionInputHandler implements InputHandler {
	private hexCallback: (hex: Hex) => SimCommand;
	private command: SimCommand | null = null;
	visMain: VisMain;

	constructor(visMain: VisMain, hexCallback: (hex: Hex) => SimCommand) {
		this.visMain = visMain;
		this.hexCallback = hexCallback;
	}

	onKeyDown(event: KeyboardEvent): boolean {
		return false;
	}

	onMouseDown(event: MouseEvent, hex: Hex): boolean {
		if (!this.command) {
			return false;
		}
		
		this.visMain.simMain.addCommand(this.command);
		return true;
	}

	onMouseMove(event: MouseEvent, hex: Hex): boolean {
		this.command = this.hexCallback(hex);
		if (this.command && this.command.check(this.visMain.simMain)) {
			this.visMain.visLevel.setHoveredHex(hex);
		}
		else {
			this.visMain.visLevel.setHoveredHex(null);
		}
		return false;
	}
}
