var ExileDB = Java.loadClass("com.robertx22.mine_and_slash.database.registry.ExileDB");
var Load = Java.loadClass("com.robertx22.mine_and_slash.uncommon.datasaving.Load");
var LevelUtils = Java.loadClass("com.robertx22.mine_and_slash.uncommon.utilityclasses.LevelUtils");

ServerEvents.customCommand(`give_gear_chest`, (event) => {
	var unit = Load.Unit(event.player);
	var level = unit.getLevel();
	let rarity = "unique";
	while (rarity == "unique") {
		rarity = ExileDB.GearRarities().random();
	}
	event.player.give(
		Item.of(
			`mmorpg:chest/${rarity.GUID()}_gear`,
			`{mmorpg_loot_chest:'{id:"gear","num":3,"lvl":${level},"rar":"${rarity.GUID()}"}'}`
		)
	);
});

ServerEvents.customCommand(`give_high_rarity_gear_chest`, (event) => {
	var unit = Load.Unit(event.player);
	var level = unit.getLevel();
	var rarities = [
		["epic", 3],
		["legendary", 2],
		["mythic", 1],
	];
	var [rarity, number] = rarities[Math.floor(Math.random() * rarities.length)];
	event.player.give(
		Item.of(
			`mmorpg:chest/${rarity}_gear`,
			`{mmorpg_loot_chest:'{id:"gear","num":${number},"lvl":${level},"rar":"${rarity}"}'}`
		)
	);
});

ServerEvents.customCommand("give_donation_box", (event) => {
	event.player.give(Item.of("ftbquests:lootcrate", `{type:"api_box"}`));
});

ServerEvents.customCommand(`give_unique`, (event) => {
	var unit = Load.Unit(event.player);
	var level = unit.getLevel();
	event.server.runCommand(`mine_and_slash give unique_gear ${event.player.username} random ${level} 1`);
});

ExileDB.GearSlots()
	.getList()
	.forEach((slot) => {
		ServerEvents.customCommand(`give_${slot.GUID()}_soul`, (event) => {
			var unit = Load.Unit(event.player);
			var level = unit.getLevel();
			var tier = LevelUtils.levelToTier(level);
			var rarity = ExileDB.GearRarities().random();
			event.player.give(
				Item.of(
					"mmorpg:stat_soul",
					`{CustomModelData:${
						slot.model_num
					},mmorpg_stat_soul:'{"tier":${tier},"slot":"${slot.GUID()}","rar":"${rarity.GUID()}"}'}`
				)
			);
		});
	});

ServerEvents.customCommand(`give_all_soul`, (event) => {
	ExileDB.GearSlots()
		.getList()
		.forEach((slot) => {
			event.player.give(
				Item.of(
					"mmorpg:stat_soul",
					`{CustomModelData:${slot.model_num},mmorpg_stat_soul:'{"tier":0,"slot":"${slot.GUID()}","rar":"common"}'}`
				)
			);
		});
});

ServerEvents.customCommand("the_faceless_event", (event) => {
	var id = event.player.username;
	var { x, y, z } = event.level.sharedSpawnPos;
	event.server.runCommand(`execute in minecraft:overworld run tp ${id} ${x} ${y} ${z}`);
	event.server.runCommand(`effect give ${id} darkness 5 0 true`);
	event.server.runCommand(
		`title ${id} title [{"translate":"entity.eldritch_end.the_faceless","bold":true,"color":"blue"},{"text":"가 침략해왔습니다.","bold":true,"color":"dark_red"}]`
	);
	setTimeout(
		() => event.server.runCommand(`title ${id} title [{"text":"그를 저지하세요.","bold":true,"color":"dark_red"}]`),
		2000
	);
	setTimeout(
		() => event.server.runCommand(`execute in minecraft:overworld run summon eldritch_end:the_faceless ${x} ${y} ${z}`),
		4000
	);
});
