import { CommandPermissionLevel, CustomCommandParamType, ItemStack, Player, world, system } from "@minecraft/server";
import { ActionFormData, MessageFormData, ModalFormData } from "@minecraft/server-ui";

export const entityIdentifierList = [
	"Aurochs",
	"Deer",
	"GrizzlyBear",
	"Hog",
	"Silkmoth",
	"Silkworm",
	//Ancient
	"Ammonite",
	"Direwolf",
	"Doedicurus",
	//Project Atelaiozoic
	"Parasaurolophus",
	//Vehicles
	"Aircraft",
	"Motorbike",
	"Rotorcraft",
	//Items
	"AngelicStatue",
	"Scarecrow",
	//Magics
	"EnchantedSlime",
	"FrostedSlime",
	"Oukelaphos",
	//Mythosoherpeton
	"Dracan",
	"Dracon",
	"FireDragon",
	"IceWyvern",
	"Maragon",
	"VelanidiaWyvern"
];
export const entityToIdentifier = {
	"Aurochs": "megaverse:aurochs",
	"Deer": "megaverse:deer",
	"GrizzlyBear": "megaverse:grizzly_bear",
	"Hog": "megaverse:hog",
	"Silkmoth": "megaverse:silkmoth",
	"Silkworm": "megaverse:silkworm",
	//Ancient
	"Ammonite": "megaverse:ammonite",
	"Direwolf": "megaverse:direwolf",
	"Doedicurus": "megaverse:doedicurus",
	//Project Atelaiozoic
	"Parasaurolophus": "megaverse:parasaurolophus",
	//Vehicles
	"Motorbike": "megaverse:motorbike",
	"Rotorcraft": "megaverse:rotorcraft",
	//Items
	"AngelicStatue": "megaverse:angelic_statue",
	"Scarecrow": "megaverse:scarecrow",
	//Magics
	"EnchantedSlime": "megaverse:enchanted_slime",
	"FrostedSlime": "megaverse:frosted_slime",
	"Oukelaphos": "megaverse:oukelaphos",
	//Mythosoherpeton
	"Dracon": "megaverse:dracon",
	"IceWyvern": "megaverse:ice_wyvern",
	"Maragon": "megaverse:maragon"
};

system.beforeEvents.startup.subscribe(({customCommandRegistry})=>{
	customCommandRegistry.registerEnum("megaverse:summon_identifier",entityIdentifierList);
	customCommandRegistry.registerCommand(
		{
			name: "megaverse:summoning",
			description: "Summon an entity from the MEGAVerse series.",
			permissionLevel: 1,
			cheatsRequired: true,
			mandatoryParameters: [
				{
					name: "megaverse:summon_identifier",
					type: CustomCommandParamType.Enum
				}
			],
			optionalParameters: [
				{
					name: "megaverse:summon_location",
					type: CustomCommandParamType.Location
				}
			]
		},
		(origin,entityIdentifier,summonLocation)=>{
			if (!origin.sourceEntity) return;
			let id, coordinates;
			const typeId = entityToIdentifier[entityIdentifier];
			if (summonLocation) coordinates = summonLocation;
			else coordinates = origin.sourceEntity.location;
			if (typeId) id = typeId;
			else id = "minecraft:lightning_bolt", coordinates = origin.sourceEntity.location;
			system.run(()=>{
				origin.sourceEntity.dimension.playSound("block.end_portal.spawn",origin.sourceEntity.location);
				let summoned = origin.sourceEntity.dimension.spawnEntity(id,coordinates);
				summoned.dimension.playSound("block.end_portal.spawn",summoned.location);
				summoned.dimension.spawnParticle("megaverse:otherworld_mirror_summoning",summoned.location);
			});
			if (typeId) world.sendMessage({rawtext:[{translate:"ui.command.megaverse:summon.success"}]});
			else return world.sendMessage({rawtext:[{text:"§c"},{translate:"ui.command.megaverse:summon.failure"},{translate:`${origin.sourceEntity.name}`}]});
		}
	);
	customCommandRegistry.registerCommand(
		{
			name: "megaverse:creator",
			description: "Creator of this add-on.",
			permissionLevel: 0,
			cheatsRequired: false
		},
		(origin)=>{
			if (!origin.sourceEntity) return;
			if (origin.sourceEntity instanceof Player) {
				const creator = origin.sourceEntity;
				if (creator.name!="KDesperation") return;
				system.run(()=>{
					creator.dimension.spawnItem(new ItemStack("megaverse:otherworld_mirror", 1), creator.location);
					creator.addLevels(8000);
				});
			}
		}
	);
	customCommandRegistry.registerCommand(
		{
			name: "megaverse:terranova",
			description: "Menu for the TerraNova add-on.",
			permissionLevel: 0,
			cheatsRequired: false
		},
		(origin)=>{
			if (!origin.sourceEntity) return;
			if (origin.sourceEntity instanceof Player) {
				const creator = origin.sourceEntity;
				system.run(()=>{
					TerraNovaMainMenu(creator);
				});
			}
		}
	);
});

function TerraNovaMainMenu(source) {
	const main_menu = new ActionFormData();
	main_menu.title({ translate: "ui.megaverse:terranova_menu.main_menu.title"});
	main_menu.body(
		{rawtext:[
			{translate:"ui.megaverse:terranova_menu.welcome.text1"},
			{text:" "},
			{translate:`${source.name}`},
			{text:"! "},{text:"\n"},
			{text:"\n"},
			{translate:"ui.megaverse:terranova_menu.welcome.text2"}
		]}
	);
	main_menu.button({ translate: "ui.megaverse:terranova_menu.credits_menu.title" },"textures/ui/megaverse/otherworld_mirror/information");
	main_menu.show(source).then((response) => {
		if (response.canceled) return source.dimension.playSound("vault.close_shutter",source.location);
		if (response.selection==0) {
			source.dimension.playSound("vault.open_shutter",source.location);
			TerraNovaCreditsMenu(source);
		}
	});
};

function TerraNovaCreditsMenu(source) {
	const information = new ActionFormData();
	information.title({ translate: "ui.megaverse:terranova_menu.credits_menu.title"});
	information.body(
		{rawtext:[
			{text:"§l"},
			{translate:"ui.megaverse:terranova_menu.credits.title1"},
			{text:"§r"},{text:"\n"},
			{translate:"ui.megaverse:terranova_menu.credits.title2"},
			{text:"\n"},
			{text:"- Kyuu Desperation"},
			{text:"\n"},
			{translate:"ui.megaverse:terranova_menu.credits.title3"},
			{text:"\n"},
			{text:"- Kyuu Desperation"},
			{text:"\n"},
			{translate:"ui.megaverse:terranova_menu.credits.title4"},
			{text:"\n"},
			{text:"- Kyuu Desperation"},
			{text:"\n"},
			{translate:"ui.megaverse:terranova_menu.credits.title5"},
			{text:"\n"},
			{text:"- Kyuu Desperation"},
			{text:"\n"},
			{translate:"ui.megaverse:terranova_menu.credits.title6"},
			{text:"\n"},
			{text:"- Kyuu Desperation"},
			{text:"\n"},
			{text:"\n"},
			{text:"© 2025 Kyuu Desperation. All Rights Reserved."}
		]}
	);
	information.button({ translate: "ui.megaverse:terranova_menu.main_menu.title" },"textures/ui/megaverse/otherworld_mirror/information");
	information.show(source).then((response) => {
		if (response.canceled) return source.dimension.playSound("vault.close_shutter",source.location);
		if (response.selection==0) {
			source.dimension.playSound('vault.close_shutter',source.location);
			TerraNovaMainMenu(source);
		}
	});
};