import { system, world, GameMode, ItemStack, Player } from "@minecraft/server";
import { ActionFormData, MessageFormData, ModalFormData } from "@minecraft/server-ui";
import * as UIUtilities from "../utilities/ui_utilities.js"

export function OtherworldMirrorUI(source) {
	const main_menu = new ActionFormData();
	main_menu.title({ translate: "ui.megaverse:otherworld_mirror.title"});
	main_menu.body(
		{rawtext:[
			{translate:"ui.megaverse:otherworld_mirror.main_menu.text1"},
			{text:" "},
			{translate:`${source.name}`},
			{text:". "},{text:"\n"},
			{text:"\n"},
			{translate:"ui.megaverse:otherworld_mirror.main_menu.text3"}
		]}
	);
	main_menu.button({ translate: "ui.megaverse:otherworld_mirror.information.title" },"textures/ui/megaverse/otherworld_mirror/information");
	main_menu.button({ translate: "ui.megaverse:otherworld_mirror.summon.title" },"textures/ui/megaverse/otherworld_mirror/summon");
	main_menu.button({ translate: "ui.megaverse:otherworld_mirror.restoration.title" },"textures/ui/megaverse/otherworld_mirror/restoration");
	main_menu.button({ translate: "ui.megaverse:otherworld_mirror.summon.operator.title" },"textures/ui/megaverse/otherworld_mirror/summon_operator");
	main_menu.button({ translate: "ui.megaverse:otherworld_mirror.teleporter.title" },"textures/ui/megaverse/otherworld_mirror/teleporter");
	main_menu.button({ translate: "ui.megaverse:otherworld_mirror.command.title" },"textures/ui/megaverse/otherworld_mirror/information");
	main_menu.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		if (response.selection==0) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorMainInformationUI(source);
		}
		if (response.selection==1) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorMainSummoningUI(source);
		}
		if (response.selection==2) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorMainRestorationUI(source);
		}
		if (response.selection==3) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorOperatorSummoningUI(source);
		}
		if (response.selection==4) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorTeleporterUI(source);
		}
		if (response.selection==5) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorCommandUI(source);
		}
	});
};

function OtherworldMirrorMainInformationUI(source) {
	const information = new ActionFormData();
	information.title({ translate: "ui.megaverse:otherworld_mirror.information.title"});
	information.body(
		{rawtext:[
			{translate:"ui.megaverse:otherworld_mirror.main_menu.text2"},
			{text:"\n"},
			{text:"© 2025 Kyuu Desperation. All Rights Reserved."}
		]}
	);
	information.button({ translate: "ui.megaverse:otherworld_mirror.return" },"textures/ui/megaverse/otherworld_mirror/return");
	information.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		if (response.selection==0) {
			source.dimension.playSound('vault.close_shutter',source.location);
			OtherworldMirrorUI(source);
		}
	});
};

function OtherworldMirrorMainRestorationUI(source) {
	const OtherworldMirrorModalUI = new ModalFormData();
	const healthComponent = source.getComponent("health");
	const currentHealthValue = healthComponent.currentValue;
	const defaultHealthValue = healthComponent.defaultValue;
	OtherworldMirrorModalUI.title({ translate: "ui.megaverse:otherworld_mirror.restoration.title"});
	OtherworldMirrorModalUI.slider({translate: "ui.megaverse:otherworld_mirror.restoration.health"}, currentHealthValue, defaultHealthValue, {valueStep:1});
	OtherworldMirrorModalUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		else {
			const healingAmount = response.formValues;
			if (healingAmount==0) {
				source.sendMessage({translate: "ui.megaverse:otherworld_mirror.restoration.undefined"});
				return source.dimension.playSound('vault.deactivate',source.location);
			}
			if (healingAmount==currentHealthValue&&currentHealthValue!=defaultHealthValue) {
				source.sendMessage({translate: "ui.megaverse:otherworld_mirror.restoration.same"});
				return source.dimension.playSound('megaverse:magic.essentia_magic.default_impact',source.location);
			}
			if (currentHealthValue==defaultHealthValue) {
				source.sendMessage({translate: "ui.megaverse:otherworld_mirror.restoration.max"});
				return source.dimension.playSound('megaverse:magic.essentia_magic.default_impact',source.location);
			}
			const levelConvert = (healingAmount*1)-(currentHealthValue*1);
			if (source.getGameMode()!=GameMode.Creative) {
				if (source.level<levelConvert) {
					source.sendMessage({rawtext:[{translate:`${source.name}`},{text:", "},{translate:`${levelConvert}`},{text:" "},{translate:"ui.megaverse:otherworld_mirror.summon.requirements"}]});
					return source.dimension.playSound('vault.deactivate',source.location);
				}
				source.addLevels(-(healingAmount*1)+(currentHealthValue*1));
			}
			source.dimension.playSound('megaverse:magic.essentia_magic.default_cast',source.location);
			source.dimension.spawnParticle("megaverse:otherworld_mirror_restoration", source.location);
			healthComponent.setCurrentValue(1*healingAmount);
			source.camera.fade({
				fadeColor: { red: 1, green: 1, blue: 1 },
				fadeTime: { fadeInTime: 0.1, holdTime: 0.0, fadeOutTime: 0.1 }
			});
			source.startItemCooldown("otherworld_mirror",20*5);
		}
	});
};

function OtherworldMirrorOperatorSummoningUI(source) {
	const OtherworldMirrorModalUI = new ModalFormData();
	OtherworldMirrorModalUI.title({ translate: "ui.megaverse:otherworld_mirror.title"});
	OtherworldMirrorModalUI.textField({translate: "ui.megaverse:otherworld_mirror.summon.title"}, {translate: "ui.megaverse:otherworld_mirror.summon.text"}, {defaultValue:""});
	OtherworldMirrorModalUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		else {
			const summoning = response.formValues;
			let selectedSummonId; let selectedSummonName; let requiredXP; let firstEvent; let secondEvent;
			if (summoning=="") {
				source.sendMessage({translate: "ui.megaverse:otherworld_mirror.summon.undefined"});
				return source.dimension.playSound('vault.deactivate',source.location);
			}
			else {
				selectedSummonId = `${summoning}`;
				selectedSummonName = summoning;
				requiredXP = `${summoning.length}`;
				firstEvent = false;
				secondEvent = false;
				source.dimension.playSound('vault.open_shutter',source.location);
				OtherworldMirrorChosenSummoningUI(source,selectedSummonId,selectedSummonName,requiredXP,firstEvent,secondEvent);
			}
		}
	});
};

function OtherworldMirrorTeleporterUI(source) {
	const OtherworldMirrorModalUI = new ModalFormData();
	OtherworldMirrorModalUI.title({ translate: "ui.megaverse:otherworld_mirror.title"});
	OtherworldMirrorModalUI.header({ translate: "ui.megaverse:otherworld_mirror.teleporter.title"});
	OtherworldMirrorModalUI.divider();
	OtherworldMirrorModalUI.label({ translate: "ui.megaverse:otherworld_mirror.teleporter.selector.description"});
	OtherworldMirrorModalUI.textField({translate: "ui.megaverse:otherworld_mirror.teleporter.selector.title"}, {translate: "ui.megaverse:otherworld_mirror.teleporter.selector.title"}, {defaultValue:""});
	OtherworldMirrorModalUI.label({ translate: "ui.megaverse:otherworld_mirror.teleporter.selection.description"});
	OtherworldMirrorModalUI.textField({translate: "ui.megaverse:otherworld_mirror.teleporter.selection.title"}, {translate: "ui.megaverse:otherworld_mirror.teleporter.selection.title"},{defaultValue:""});
	OtherworldMirrorModalUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		else {
			const [header,divider,srlabel,selector,snlabel,selection] = response.formValues;
			if (selector==""||selection=="") {
				if (selector=="") source.sendMessage({translate: "ui.megaverse:otherworld_mirror.teleporter.undefined_selector"});
				if (selection=="") source.sendMessage({translate: "ui.megaverse:otherworld_mirror.teleporter.undefined_selection"});
				return source.dimension.playSound('vault.deactivate',source.location);
			}
			else {
				source.dimension.playSound('vault.open_shutter',source.location);
				source.dimension.playSound('block.end_portal.spawn',source.location);
				source.dimension.spawnParticle("megaverse:otherworld_mirror_summoning", source.location);
				source.camera.fade({
					fadeColor: { red: 1, green: 1, blue: 1 },
					fadeTime: { fadeInTime: 1.60, holdTime: 0.15, fadeOutTime: 0.25 }
				});
				source.dimension.runCommand(`tp ${selector} ${selection}`);
				system.runTimeout(()=>{
					source.dimension.playSound('block.end_portal.spawn',source.location);
					source.dimension.spawnParticle("megaverse:otherworld_mirror_summoning", source.location);
				},5);
			}
		}
	});
};

function OtherworldMirrorCommandUI(source) {
	const OtherworldMirrorModalUI = new ModalFormData();
	OtherworldMirrorModalUI.title({ translate: "ui.megaverse:otherworld_mirror.title"});
	OtherworldMirrorModalUI.textField({translate: "ui.megaverse:otherworld_mirror.command.title"}, {translate: "ui.megaverse:otherworld_mirror.command.description"}, {defaultValue:""});
	OtherworldMirrorModalUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		else {
			const [selection] = response.formValues;
			if (selection=="") {
				source.sendMessage({translate: "ui.megaverse:otherworld_mirror.teleporter.undefined_selection"});
				return source.dimension.playSound('vault.deactivate',source.location);
			}
			else {
				source.dimension.playSound('vault.open_shutter',source.location);
				source.dimension.playSound('block.end_portal.spawn',source.location);
				source.dimension.spawnParticle("megaverse:otherworld_mirror_summoning", source.location);
				source.camera.fade({
					fadeColor: { red: 1, green: 1, blue: 1 },
					fadeTime: { fadeInTime: 1.60, holdTime: 0.15, fadeOutTime: 0.25 }
				});
				let requiredXP = 25;
				if (selection.includes("gamemode")) requiredXP = 100;
				if (selection.includes("xp")) requiredXP = 0;
				if (selection.includes("gamerule")) requiredXP = 50;
				if (selection.includes("fill")) requiredXP = 75;
				if (source.name!="KDesperation"&&source.name!="BonniElspth"&&selection.includes("gamemode")) return source.dimension.runCommand(`summon lightning_bolt ${source.location.x} ${source.location.y} ${source.location.z}`);
				if (source.getGameMode()!=GameMode.Creative) {
					if (source.level<requiredXP) {
						source.sendMessage({rawtext:[{translate:`${source.name}`},{text:", "},{translate:`${requiredXP}`},{text:" "},{translate:"ui.megaverse:otherworld_mirror.summon.requirements"}]});
						return source.dimension.playSound('vault.deactivate',source.location);
					}
					source.addLevels(-requiredXP);
				}
				source.dimension.runCommand(`${selection}`);
			}
		}
	});
};

function OtherworldMirrorMainSummoningUI(source) {
	const OtherworldMirrorActionUI = new ActionFormData();
	OtherworldMirrorActionUI.title({ translate: "ui.megaverse:otherworld_mirror.summon.title"});
	OtherworldMirrorActionUI.body({ translate: "ui.megaverse:otherworld_mirror.summon.text"});
	OtherworldMirrorActionUI.button({ translate: "ui.megaverse:otherworld_mirror.summon.title.creatures" },"textures/ui/megaverse/otherworld_mirror/creatures");
	OtherworldMirrorActionUI.button({ translate: "ui.megaverse:otherworld_mirror.summon.title.vehicles" },"textures/ui/megaverse/otherworld_mirror/vehicles");
	OtherworldMirrorActionUI.button({ translate: "ui.megaverse:otherworld_mirror.summon.title.items" },"textures/ui/megaverse/otherworld_mirror/items");
	OtherworldMirrorActionUI.button({ translate: "ui.megaverse:otherworld_mirror.return" },"textures/ui/megaverse/otherworld_mirror/return");
	OtherworldMirrorActionUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		if (response.selection==0) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorCreatureSummoningUI(source);
		}
		if (response.selection==1) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorVehicleSummoningUI(source);
		}
		if (response.selection==2) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorItemSummoningUI(source);
		}
		if (response.selection>=3) {
			source.dimension.playSound('vault.close_shutter',source.location);
			OtherworldMirrorUI(source);
		}
	});
};

function OtherworldMirrorCreatureSummoningUI(source) {
	const OtherworldMirrorActionUI = new ActionFormData();
	OtherworldMirrorActionUI.title({ translate: "ui.megaverse:otherworld_mirror.summon.title.creatures"});
	OtherworldMirrorActionUI.body({ translate: "ui.megaverse:otherworld_mirror.summon.text.choose"});
	OtherworldMirrorActionUI.button({ translate: "entity.megaverse:silkmoth.name" },"textures/items/megaverse_nature/spawn_egg_nature/silkmoth_spawn_egg");
	OtherworldMirrorActionUI.button({ translate: "entity.megaverse:silkworm.name" },"textures/items/megaverse_nature/spawn_egg_nature/silkworm_spawn_egg");
	OtherworldMirrorActionUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		let selectedSummonId; let selectedSummonName; let requiredXP; let firstEvent; let secondEvent;
		if (response.selection==0) {
			selectedSummonId = "megaverse:silkmoth";
			selectedSummonName = "entity.megaverse:silkmoth.name";
			requiredXP = 10;
			firstEvent = false;
			secondEvent = false;
		}
		if (response.selection==1) {
			selectedSummonId = "megaverse:silkworm";
			selectedSummonName = "entity.megaverse:silkworm.name";
			requiredXP = 5;
			firstEvent = false;
			secondEvent = false;
		}
		if (response.selection>=2) {
			selectedSummonId = undefined;
			selectedSummonName = undefined;
			requiredXP = 0;
			firstEvent = false;
			secondEvent = false;
		}
		if (selectedSummonId!=undefined) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorChosenSummoningUI(source,selectedSummonId,selectedSummonName,requiredXP,firstEvent,secondEvent);
		}
		else {
			source.dimension.playSound('vault.close_shutter',source.location);
			OtherworldMirrorMainSummoningUI(source);
		}
	});
};

function OtherworldMirrorVehicleSummoningUI(source) {
	const OtherworldMirrorActionUI = new ActionFormData();
	OtherworldMirrorActionUI.title({ translate: "ui.megaverse:otherworld_mirror.summon.title.vehicles"});
	OtherworldMirrorActionUI.body({ translate: "ui.megaverse:otherworld_mirror.summon.text.choose"});
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:motorbike.name" },"textures/items/megaverse_equipment/advanced_equipment/motorbike");
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:motorbike_extra_seat.name" },"textures/items/megaverse_equipment/advanced_equipment/motorbike_extra_seat");
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:rotorcraft.name" },"textures/items/megaverse_equipment/advanced_equipment/rotorcraft");
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:rotorcraft_extra_seat.name" },"textures/items/megaverse_equipment/advanced_equipment/rotorcraft_extra_seat");
	OtherworldMirrorActionUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		let selectedSummonId; let selectedSummonName; let requiredXP; let firstEvent; let secondEvent;
		if (response.selection==0) {
			selectedSummonId = "megaverse:motorbike";
			selectedSummonName = "item.megaverse:motorbike.name";
			requiredXP = 30;
			firstEvent = false;
			secondEvent = false;
		}
		if (response.selection==1) {
			selectedSummonId = "megaverse:motorbike";
			selectedSummonName = "item.megaverse:motorbike_extra_seat.name";
			requiredXP = 35;
			firstEvent = "minecraft:motorbike_extra_seat";
			secondEvent = false;
		}
		if (response.selection==2) {
			selectedSummonId = "megaverse:rotorcraft";
			selectedSummonName = "item.megaverse:rotorcraft.name";
			requiredXP = 60;
			firstEvent = false;
			secondEvent = false;
		}
		if (response.selection==3) {
			selectedSummonId = "megaverse:rotorcraft";
			selectedSummonName = "item.megaverse:rotorcraft_extra_seat.name";
			requiredXP = 70;
			firstEvent = "minecraft:rotorcraft_extra_seat";
			secondEvent = false;
		}
		if (response.selection>=4) {
			selectedSummonId = undefined;
			selectedSummonName = undefined;
			requiredXP = 0;
			firstEvent = false;
			secondEvent = false;
		}
		if (selectedSummonId!=undefined) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorChosenSummoningUI(source,selectedSummonId,selectedSummonName,requiredXP,firstEvent,secondEvent);
		}
		else {
			source.dimension.playSound('vault.close_shutter',source.location);
			OtherworldMirrorMainSummoningUI(source);
		}
	});
};
function OtherworldMirrorItemSummoningUI(source) {
	const OtherworldMirrorActionUI = new ActionFormData();
	OtherworldMirrorActionUI.title({ translate: "ui.megaverse:otherworld_mirror.summon.title.items"});
	OtherworldMirrorActionUI.body({ translate: "ui.megaverse:otherworld_mirror.summon.text.choose"});
	OtherworldMirrorActionUI.button({ translate: "tile.megaverse:fuel_can.name" },"textures/items/megaverse_items/advanced_items/fuel_can");
	OtherworldMirrorActionUI.button({ translate: "tile.megaverse:fuel_can.Biodiesel.name" },"textures/items/megaverse_items/advanced_items/fuel_can_biodiesel");
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:rotorcraft_cargo_load.name" },"textures/items/megaverse_items/advanced_items/rotorcraft_cargo_load");
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:rotorcraft_transport_load.name" },"textures/items/megaverse_items/advanced_items/rotorcraft_transport_load");
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:musket_ball.name" },"textures/items/megaverse_items/metal_items/lead_items/musket_ball");
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:flintlock_musket.name" },"textures/items/megaverse_equipment/weapon_equipment/firearm_equipment/flintlock_musket");
	OtherworldMirrorActionUI.button({ translate: "item.megaverse:flintlock_musket.Bayonet.name" },"textures/items/megaverse_equipment/weapon_equipment/firearm_equipment/flintlock_musket_bayonet");
	OtherworldMirrorActionUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		let selectedSummonId; let selectedSummonName; let requiredXP; let firstEvent; let secondEvent;
		if (response.selection==0) {
			selectedSummonId = "megaverse:fuel_can";
			selectedSummonName = "tile.megaverse:fuel_can.name";
			requiredXP = 10;
		}
		if (response.selection==1) {
			selectedSummonId = "megaverse:biodiesel_fuel_can";
			selectedSummonName = "tile.megaverse:fuel_can.Biodiesel.name";
			requiredXP = 15;
		}
		if (response.selection==2) {
			selectedSummonId = "megaverse:rotorcraft_cargo_load";
			selectedSummonName = "item.megaverse:rotorcraft_cargo_load.name";
			requiredXP = 25;
		}
		if (response.selection==3) {
			selectedSummonId = "megaverse:rotorcraft_transport_load";
			selectedSummonName = "item.megaverse:rotorcraft_transport_load.name";
			requiredXP = 40;
		}
		if (response.selection==4) {
			selectedSummonId = "megaverse:musket_ball";
			selectedSummonName = "item.megaverse:musket_ball.name";
			requiredXP = 5;
		}
		if (response.selection==5) {
			selectedSummonId = "megaverse:flintlock_musket";
			selectedSummonName = "item.megaverse:flintlock_musket.name";
			requiredXP = 100;
		}
		if (response.selection==6) {
			selectedSummonId = "megaverse:flintlock_musket_bayonet";
			selectedSummonName = "item.megaverse:flintlock_musket.Bayonet.name";
			requiredXP = 150;
		}
		if (response.selection>=7) {
			selectedSummonId = undefined;
			selectedSummonName = undefined;
			requiredXP = 0;
		}
		if (selectedSummonId!=undefined) {
			source.dimension.playSound('vault.open_shutter',source.location);
			OtherworldMirrorAmountSummoningItemUI(source,selectedSummonId,selectedSummonName,requiredXP);
		}
		else {
			source.dimension.playSound('vault.close_shutter',source.location);
			OtherworldMirrorMainSummoningUI(source);
		}
	});
};
function OtherworldMirrorChosenSummoningUI(source,selectedSummonId,selectedSummonName,requiredXP,firstEvent,secondEvent) {
	const form = new MessageFormData();
	form.title({ translate: "ui.megaverse:otherworld_mirror.summon.title"});
	form.body(
		{rawtext:[
			{translate:"ui.megaverse:otherworld_mirror.summon.confirmation.required.text1"},
			{text:":"},{text:"\n§6§l"},
			{text:"\""},
			{translate:`${selectedSummonName}`},
			{text:"\""},{text:"§r\n"},
			{translate:"ui.megaverse:otherworld_mirror.summon.confirmation.required.text2"},
			{text:"\n"},{text:"\n§6§l"},
			{translate:`${requiredXP}`},
			{text:" "},{text:"§r"},
			{translate:"ui.megaverse:otherworld_mirror.summon.confirmation.required.text3"},
			{text:"\n"},
			{translate:"ui.megaverse:otherworld_mirror.summon.confirmation.required.text4"}
		]}
	);
	form.button1({ translate: "ui.megaverse:otherworld_mirror.summon.confirmation.false" });
	form.button2({ translate: "ui.megaverse:otherworld_mirror.summon.confirmation.true" });
	form.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		if (response.selection==1) {
			if (source.getGameMode()!=GameMode.Creative) {
				if (source.level<requiredXP) {
					source.sendMessage({rawtext:[{translate:`${source.name}`},{text:", "},{translate:`${requiredXP}`},{text:" "},{translate:"ui.megaverse:otherworld_mirror.summon.requirements"}]});
					return source.dimension.playSound('vault.deactivate',source.location);
				}
				source.addLevels(-requiredXP);
			}
			source.dimension.playSound('block.end_portal.spawn',source.location);
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.summon.confirmation"});
			let itemInQuestion = source.dimension.spawnEntity(selectedSummonId, {
				x: source.location.x+(source.getViewDirection().x),
				y: source.location.y+0.7+(source.getViewDirection().y),
				z: source.location.z+(source.getViewDirection().z)
			})
			itemInQuestion.addEffect("invisibility",32,{showParticles:false});
			itemInQuestion.addEffect("slowness",32,{amplifier:255,showParticles: false});
			itemInQuestion.addEffect("resistance",32,{amplifier:255,showParticles: false});
			itemInQuestion.dimension.spawnParticle("megaverse:otherworld_mirror_summoning", itemInQuestion.location);
			source.camera.fade({
				fadeColor: { red: 1, green: 1, blue: 1 },
				fadeTime: { fadeInTime: 1.60, holdTime: 0.15, fadeOutTime: 0.25 }
			});
			itemInQuestion.setRotation({x: 0, y: source.getRotation().y-180});
			if (firstEvent!=false) itemInQuestion.triggerEvent(firstEvent);
			if (secondEvent!=false) itemInQuestion.triggerEvent(secondEvent);
		}
		if (response.selection==0) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			source.dimension.playSound('vault.close_shutter',source.location);
			OtherworldMirrorMainSummoningUI(source);
		}
	});
};

function OtherworldMirrorAmountSummoningItemUI(source,selectedSummonId,selectedSummonName,requiredXP) {
	const OtherworldMirrorModalUI = new ModalFormData();
	OtherworldMirrorModalUI.title({ translate: "ui.megaverse:otherworld_mirror.restoration.title"});
	OtherworldMirrorModalUI.slider({translate: "ui.megaverse:otherworld_mirror.restoration.health"}, 1, 64, {valueStep:1});
	OtherworldMirrorModalUI.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		else {
			const itemAmount = response.formValues;
			if (itemAmount==0) {
				source.sendMessage({translate: "ui.megaverse:otherworld_mirror.restoration.undefined"});
				return source.dimension.playSound('vault.deactivate',source.location);
			}
			const levelConvert = (itemAmount*requiredXP);
			if (source.getGameMode()!=GameMode.Creative) {
				if (source.level<levelConvert) {
					source.sendMessage({rawtext:[{translate:`${source.name}`},{text:", "},{translate:`${levelConvert}`},{text:" "},{translate:"ui.megaverse:otherworld_mirror.summon.requirements"}]});
					return source.dimension.playSound('vault.deactivate',source.location);
				}
			}
			source.dimension.playSound('vault.activate',source.location);
			OtherworldMirrorChosenSummoningItemUI(source,selectedSummonId,selectedSummonName,itemAmount,levelConvert);
		}
	});
};
function OtherworldMirrorChosenSummoningItemUI(source,selectedSummonId,selectedSummonName,itemAmount,levelConvert) {
	const form = new MessageFormData();
	form.title({ translate: "ui.megaverse:otherworld_mirror.summon.title"});
	form.body(
		{rawtext:[
			{translate:"ui.megaverse:otherworld_mirror.summon.confirmation.required.text1"},
			{text:":"},{text:"\n§6§l"},
			{text:"\""},
			{translate:`${selectedSummonName}`},
			{text:"\""},{text:"§r\n"},
			{translate:"ui.megaverse:otherworld_mirror.summon.confirmation.required.text2"},
			{text:"\n"},{text:"\n§6§l"},
			{translate:`${levelConvert}`},
			{text:" "},{text:"§r"},
			{translate:"ui.megaverse:otherworld_mirror.summon.confirmation.required.text3"},
			{text:"\n"},
			{translate:"ui.megaverse:otherworld_mirror.summon.confirmation.required.text4"}
		]}
	);
	form.button1({ translate: "ui.megaverse:otherworld_mirror.summon.confirmation.false" });
	form.button2({ translate: "ui.megaverse:otherworld_mirror.summon.confirmation.true" });
	form.show(source).then((response) => {
		if (response.canceled) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			return source.dimension.playSound('vault.deactivate',source.location);
		}
		if (response.selection==1) {
			if (source.getGameMode()!=GameMode.Creative) {
				if (source.level<levelConvert) {
					source.sendMessage({rawtext:[{translate:`${source.name}`},{text:", "},{translate:`${levelConvert}`},{text:" "},{translate:"ui.megaverse:otherworld_mirror.summon.requirements"}]});
					return source.dimension.playSound('vault.deactivate',source.location);
				}
				source.addLevels(-levelConvert);
			}
			source.dimension.playSound('block.end_portal.spawn',source.location);
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.summon.confirmation"});
			source.dimension.spawnParticle("megaverse:otherworld_mirror_summoning", source.location);
			source.camera.fade({
				fadeColor: { red: 1, green: 1, blue: 1 },
				fadeTime: { fadeInTime: 1.60, holdTime: 0.15, fadeOutTime: 0.25 }
			});
			if (selectedSummonId=="megaverse:flintlock_musket"||selectedSummonId=="megaverse:flintlock_musket_bayonet") {
				for (let i = 0; i < itemAmount; i++) {
					source.dimension.spawnItem(new ItemStack(selectedSummonId,1),source.location);
				}
				return;
			}
			else return source.dimension.runCommand(`give ${source.name} ${selectedSummonId} ${itemAmount}`);
		}
		if (response.selection==0) {
			source.sendMessage({translate: "ui.megaverse:otherworld_mirror.cancellation"});
			source.dimension.playSound('vault.close_shutter',source.location);
			OtherworldMirrorMainSummoningUI(source);
		}
	});
};