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

world.beforeEvents.itemUse.subscribe((data) => {
	let player = data.source;
	if (data.itemStack.typeId == "manalabs_opw:guide_book")
		system.run(() => manalabs_opw_main(player));

	//MAIN PAGE
	function manalabs_opw_main() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.main.title" })
			.body({ translate: "manalabs_opw.guide.main.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.about.button" },
				"textures/manalabs/opw/guide/about",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapons.button" },
				"textures/manalabs/opw/guide/weapons",
			)
			.button(
				{ translate: "manalabs_opw.guide.exit.button" },
				"textures/manalabs/opw/guide/exit",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) about(player);
			if (r.selection == 1) weapons(player);
		});
	}
	//ABOUT PAGE
	function about() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.about.title" })
			.body({ translate: "manalabs_opw.guide.about.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) manalabs_opw_main(player);
		});
	}
	//WEAPONS PAGE
	function weapons() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.weapons.title" })
			.body({ translate: "manalabs_opw.guide.weapons.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_celestial.button" },
				"textures/manalabs/opw/items/celestial_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_cosmic.button" },
				"textures/manalabs/opw/items/cosmic_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_hand.button" },
				"textures/manalabs/opw/items/hand_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_ice.button" },
				"textures/manalabs/opw/items/ice_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_kyro.button" },
				"textures/manalabs/opw/items/kyro_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_laser.button" },
				"textures/manalabs/opw/items/laser_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_mystic.button" },
				"textures/manalabs/opw/items/mystic_axe",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_poison.button" },
				"textures/manalabs/opw/items/poison_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_pyro.button" },
				"textures/manalabs/opw/items/pyro_bow",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_skull.button" },
				"textures/manalabs/opw/items/skull_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_vampire.button" },
				"textures/manalabs/opw/items/vampire_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_bubble.button" },
				"textures/manalabs/opw/items/cosmic_rings",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_shadow.button" },
				"textures/manalabs/opw/items/shadow_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_crusher.button" },
				"textures/manalabs/opw/items/skull_crusher",
			)
			.button(
				{ translate: "manalabs_opw.guide.weapon_titan.button" },
				"textures/manalabs/opw/items/titan_sword",
			)
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) manalabs_opw_main(player);
			if (r.selection == 1) celestial(player);
			if (r.selection == 2) cosmic(player);
			if (r.selection == 3) hand(player);
			if (r.selection == 4) ice(player);
			if (r.selection == 5) kyro(player);
			if (r.selection == 6) laser(player);
			if (r.selection == 7) mystic(player);
			if (r.selection == 8) poison(player);
			if (r.selection == 9) pyro(player);
			if (r.selection == 10) skull(player);
			if (r.selection == 11) vampire(player);
			if (r.selection == 12) bubble(player);
			if (r.selection == 13) shadow(player);
			if (r.selection == 14) crusher(player);
			if (r.selection == 15) titan(player);
			if (r.selection == 16) manalabs_opw_main(player);
		});
	}
	//WEAPONS - CELESTIAL
	function celestial() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.celestial.title" })
			.body({ translate: "manalabs_opw.guide.celestial.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - COSMIC
	function cosmic() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.cosmic.title" })
			.body({ translate: "manalabs_opw.guide.cosmic.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - HAND
	function hand() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.hand.title" })
			.body({ translate: "manalabs_opw.guide.hand.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - ICE
	function ice() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.ice.title" })
			.body({ translate: "manalabs_opw.guide.ice.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - KYRO
	function kyro() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.kyro.title" })
			.body({ translate: "manalabs_opw.guide.kyro.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - LASER
	function laser() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.laser.title" })
			.body({ translate: "manalabs_opw.guide.laser.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - MYSTIC
	function mystic() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.mystic.title" })
			.body({ translate: "manalabs_opw.guide.mystic.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - POISON
	function poison() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.poison.title" })
			.body({ translate: "manalabs_opw.guide.poison.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - PYRO
	function pyro() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.pyro.title" })
			.body({ translate: "manalabs_opw.guide.pyro.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - SKULL
	function skull() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.skull.title" })
			.body({ translate: "manalabs_opw.guide.skull.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - VAMPIRE
	function vampire() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.vampire.title" })
			.body({ translate: "manalabs_opw.guide.vampire.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - BUBBLE
	function bubble() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.bubble.title" })
			.body({ translate: "manalabs_opw.guide.bubble.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - SHADOW
	function shadow() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.shadow.title" })
			.body({ translate: "manalabs_opw.guide.shadow.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - CRUSHER
	function crusher() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.crusher.title" })
			.body({ translate: "manalabs_opw.guide.crusher.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
	//WEAPONS - TITAN
	function titan() {
		player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
		const form = new ActionFormData()
			.title({ translate: "manalabs_opw.guide.titan.title" })
			.body({ translate: "manalabs_opw.guide.titan.body", with: ["\n"] })
			.button(
				{ translate: "manalabs_opw.guide.back.button" },
				"textures/manalabs/opw/guide/back",
			);
		form.show(player).then((r) => {
			if (r.selection == 0) weapons(player);
		});
	}
});
