import { frontBlockLocation, backBlockLocation, leftBlockLocation, rightBlockLocation, directionState } from '../util/globalVariables';

export class MrCounter {
	constructor() {
		this.onPlace = this.onPlace.bind(this);
		this.onPlayerBreak = this.onPlayerBreak.bind(this);
	}
	updateCounter(block, direction) {
		const frontBlock = block.offset(backBlockLocation[direction]);
		block.setPermutation(block.permutation.withState("mr:f", frontBlock.hasTag("mr:is_counter")));
	}
	updateNearestCounter(block, direction) {
		const backBlock = block.offset(frontBlockLocation[direction]);
		const rightBlock = block.offset(leftBlockLocation[direction]);
		const leftBlock = block.offset(rightBlockLocation[direction]);
		if (backBlock.hasTag("mr:is_counter")) this.updateCounter(backBlock, backBlock.permutation.getState(directionState));
		if (rightBlock.hasTag("mr:is_counter")) this.updateCounter(rightBlock, rightBlock.permutation.getState(directionState));
		if (leftBlock.hasTag("mr:is_counter")) this.updateCounter(leftBlock, leftBlock.permutation.getState(directionState));
	}
	onPlace(e) {
		const direction = e.block.permutation.getState(directionState);
		this.updateCounter(e.block, direction);
		this.updateNearestCounter(e.block, direction);
	}
	onPlayerBreak(e) {
		const direction = e.brokenBlockPermutation.getState(directionState);
		this.updateNearestCounter(e.block, direction);
	}
}