import * as SERVER from '@minecraft/server';
import * as UI from '@minecraft/server-ui';
import { playNote } from "./note_block.js";

// Subscribe to events to run code when specific in game actions occur
SERVER.system.afterEvents.scriptEventReceive.subscribe((data) => {
	if (data.id != 'vc:checkRedstone') return;
	let block = data.sourceBlock;
	if (data.sourceBlock == undefined) block = data.sourceEntity;
	if (data.sourceBlock == undefined) {
		console.warn(("test failed (no sourceEntity or sourceBlock)"));
		return;
	};

	let wasSucessful = false
	if (block.north(1) != undefined) if (block.north(1).getRedstonePower() > 0 || block.north(1).permutation.getState("vc:lit_bit")) wasSucessful = true;
	if (block.east(1) != undefined) if (block.east(1).getRedstonePower() > 0 || block.east(1).permutation.getState("vc:lit_bit")) wasSucessful = true;
	if (block.south(1) != undefined) if (block.south(1).getRedstonePower() > 0 || block.south(1).permutation.getState("vc:lit_bit")) wasSucessful = true;
	if (block.west(1) != undefined) if (block.west(1).getRedstonePower() > 0 || block.west(1).permutation.getState("vc:lit_bit")) wasSucessful = true;
	if (block.above(1) != undefined) if (block.above(1).getRedstonePower() > 0 || block.above(1).permutation.getState("vc:lit_bit")) wasSucessful = true;
	if (block.below(1) != undefined) if (block.below(1).getRedstonePower() > 0 || block.below(1).permutation.getState("vc:lit_bit")) wasSucessful = true;
	//console.log(wasSucessful)
	//console.warn(wasSucessful);
	//SERVER.world.sendMessage((wasSucessful ? "true" : "false"));
	const loc = block.location;
	if (data.sourceBlock == undefined) {
		console.warn("no block to test");
		return;
	}
	else if (data.message.split(' ')[0] == 'once') {
		let split = data.message.split(' ')
		if (block.permutation.getState(split[1]) != wasSucessful) {
			setPermutation(block, split[2], !block.permutation.getState(split[2]))
			setPermutation(block, split[1], wasSucessful)
		}
	}
	else if (block.typeId == 'vc:cut_gloricalium_block' || block.typeId == 'vc:gloricalium_block') {
		if (wasSucessful) {
			block.dimension.runCommand(`setblock ${loc.x} ${loc.y} ${loc.z} vc:${block.typeId.startsWith('vc:cut_') ? 'cut_' : ''}discharged_gloricalium_block`);
		}
	}
	else if (block.typeId == 'vc:custom_note_block') {
		if (wasSucessful && block.permutation.getState(data.message) == false) {

			playNote(block, false)
		}
	}
	else if (block.typeId == 'vc:rotator') {
		if (wasSucessful) {
			if (block.permutation.getState("vc:direction") == "clockwise") block.dimension.runCommand(`setblock ${loc.x} ${loc.y} ${loc.z} vc:rotator_powered[\"vc:direction\"=\"clockwise\"]`);
			if (block.permutation.getState("vc:direction") == "counterclockwise") block.dimension.runCommand(`setblock ${loc.x} ${loc.y} ${loc.z} vc:rotator_powered[\"vc:direction\"=\"counterclockwise\"]`);
		}
	}
	else if (block.typeId == 'vc:rotator_powered') {
		if (!wasSucessful) {
			if (block.permutation.getState("vc:direction") == "clockwise") block.dimension.runCommand(`setblock ${loc.x} ${loc.y} ${loc.z} vc:rotator[\"vc:direction\"=\"clockwise\"]`);
			if (block.permutation.getState("vc:direction") == "counterclockwise") block.dimension.runCommand(`setblock ${loc.x} ${loc.y} ${loc.z} vc:rotator[\"vc:direction\"=\"counterclockwise\"]`);
		}
	}
	else {
		if (data.message == "" && data.message == undefined) return;
		if (data.message.startsWith("on.runCommand:")) if (wasSucessful) block.dimension.runCommand(`${data.message.replace("on.runCommand:", "")}`);
		else if (data.message.startsWith("off.runCommand:")) if (!wasSucessful) block.dimension.runCommand(`${data.message.replace("off.runCommand:", "")}`);
	}
	if (wasSucessful)
		setPermutation(block, data.message, true)
	else
		setPermutation(block, data.message, false)

});

function setPermutation(block, stateAdd, stateValue) { //stole this function from Ramcor14_Player thanks to him
	try {
		const result = block.permutation.getAllStates();
		result[stateAdd] = stateValue;
		block.setPermutation(SERVER.BlockPermutation.resolve(block.typeId, result));

	}
	catch (error) { }
}
// See more at https://wiki.bedrock.dev/scripting/script-server.html
