import { world, system } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";

world.afterEvents.itemUse.subscribe(event => {
	if (event.itemStack.typeId !== "myth:device") return;

	form_guidebook(event.source);
});

//scriptevent mythicus:settings
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
    if (id !== "myth:device")
        return;

    form_guidebook(sourceEntity);
});

async function form_guidebook(player) {
	const form = new ActionFormData();
	form.title({ rawtext: [{ translate: "generator.mythicus:ui.form.guidebook" }] });

	form.body({
		rawtext: [
			{ translate: "generator.mythicus:ui.guidebook.1", "with": ["\n"] },
			{ "text": "\n\n" },
			{ translate: "generator.mythicus:ui.guidebook.2", "with": ["\n"] },
			{ "text": "\n\n" },
			{ translate: "generator.mythicus:ui.guidebook.3", "with": ["\n"] },
			{ "text": "\n\n" },
			{ translate: "generator.mythicus:ui.guidebook.4", "with": ["\n"] },
			{ "text": "\n\n" }
			
		]
	});

	form.button({ rawtext: [{ translate: "generator.mythicus:ui.guidebook.phone" }] }, "textures/ui/myth/device/phone");
	form.button({ rawtext: [{ translate: "generator.mythicus:ui.guidebook.console" }] }, "textures/ui/myth/device/console");
	form.button({ rawtext: [{ translate: "generator.mythicus:ui.guidebook.computer" }] }, "textures/ui/myth/device/keyboard");


	player.playSound("item.book.page_turn", { volume: 1.0, pitch: 1.0 });
	form.show(player).then((response) => {
		player.playSound("item.book.page_turn", { volume: 1.0, pitch: 1.0 });

		switch (response?.selection) {
			case 1:
				player.runCommand('scoreboard players set "#generator_frequency" gvar 1');
				player.sendMessage({ translate: "generator.mythicus:guidebook.response.console" });
				return;

			case 2:
				player.runCommand('scoreboard players set "#generator_frequency" gvar 0');
				player.sendMessage({ translate: "generator.mythicus:guidebook.response.computer" });
				return;

			default:
				player.runCommand('scoreboard players set "#generator_frequency" gvar 2');
				player.sendMessage({ translate: "generator.mythicus:guidebook.response.phone" });
		}
	});
}