import { ItemStack, Player, Entity, EquipmentSlot, world } from "@minecraft/server";
import { ActionFormData, MessageFormData, ModalFormData } from "@minecraft/server-ui";
import { getScoreUtility, setScoreUtility, operationScoreUtility } from "./scoreboard";
import { clearAllSkill, clearCommand, decrementStack, giveCommand } from "./function";
import { status_nen } from "./status_nen";
import { status_hatsu } from "./status_hatsu";

/**
 * @param {Player} player
 */
export function operationStatus(player) {
	const hatsu = player.getProperty("hxc:hatsu");
	const Exp = player.getProperty("hxc:exp");
	const Level = player.getProperty("hxc:level");
	const MAX_EXP = ((Level * 5) + 20) + (Level * 10);
	const bodyLines = [];
	const buttons = [];
	const actions = [];
	const icons = [];
	buttons.push({translate:"form.status.nen"}); icons.push("textures/form/status/nen"); actions.push(() => status_nen(player));
	buttons.push({rawtext:[{text:hatsu==0 ? "§7" : ""},{translate:"form.status.hatsu"}]}); icons.push("textures/form/status/hatsu"); actions.push(() => status_hatsu(player));
	const statusMenu = new ActionFormData().title({rawtext:[{text:"\ue180"},{text:` ${player.nameTag}\ue180`}]} );
	if(player.getGameMode()=="Creative"){
		buttons.push({rawtext:[{text:"§1§l"},{translate:"form.status.cheatMenu"},{text:"§f"}]}); 
		icons.push(""); 
		actions.push(() => statusButtonCheat(player));
	}
	if (bodyLines.length > 0) {
		statusMenu.body({rawtext: bodyLines});
	}
	for(let i=0; i<buttons.length; i++){
		statusMenu.button(buttons[i], icons[i]);
	}
	statusMenu.show(player).then(response => {
		const idx = response.selection;
		if(idx>=0 && idx<actions.length){
			actions[idx]();
		}
	});
}
/**
 * @param {Player} player 
 */
export function updateStatus(player){
	const Exp = player.getProperty("hxc:exp");
	const Level = player.getProperty("hxc:level");
	const MAX_EXP = ((Level * 5) + 20) + (Level * 10);
	const resetExp = Exp - MAX_EXP;
	const addLevel = Level + 1;
	if(Exp >= MAX_EXP && Level < 200){
		player.setProperty("hxc:exp",resetExp);
		player.setProperty("hxc:level",addLevel);
		player.sendMessage({rawtext:[{text:"§6"},{translate:"form.status.body.0"},{text:`${addLevel}`}]});
		player.runCommand("playsound random.levelup @s ~~1~ 100000 1 100000");
		player.runCommand("particle hxc:level_up ~~-0.1~");
	}
	updateHp(player);
}
/**
 * @param {Player} player 
 */
export function updateHp(player){
	const Exp = player.getProperty("hxc:exp");
	const Level = player.getProperty("hxc:level");
	const MAX_EXP = ((Level * 5) + 20) + (Level * 10);
	const hpComp = player.getComponent("health");
	const defaultHp = hpComp.defaultValue;
	const hpTable = [
		{ min: 0,   max: 4,  hp: 20 },
		{ min: 5,   max: 9,  hp: 30 },
		{ min: 10,  max: 14, hp: 40 },
		{ min: 15,  max: 19, hp: 50 },
		{ min: 20,  max: 24, hp: 60 },
		{ min: 25,  max: 29, hp: 70 },
		{ min: 30,  max: 34, hp: 80 },
		{ min: 35,  max: 39, hp: 90 },
		{ min: 40,  max: 44, hp: 100 },
		{ min: 45,  max: 49, hp: 110 },
		{ min: 50,  max: 54, hp: 120 },
		{ min: 55,  max: 59, hp: 130 },
		{ min: 60,  max: 64, hp: 140 },
		{ min: 65,  max: 69, hp: 150 },
		{ min: 70,  max: 74, hp: 160 },
		{ min: 75,  max: 79, hp: 170 },
		{ min: 80,  max: 84, hp: 180 },
		{ min: 85,  max: 89, hp: 190 },
		{ min: 90,  max: 94, hp: 200 },
		{ min: 95,  max: 99, hp: 300 },
		{ min: 100, max: 149, hp: 400 },
		{ min: 110, max: 119, hp: 500 },
		{ min: 120, max: 129, hp: 600 },
		{ min: 130, max: 139, hp: 700 },
		{ min: 140, max: 149, hp: 800 },
		{ min: 150, max: 199, hp: 900 },
		{ min: 200, max: Infinity, hp: 1000 },
	];
	const entry = hpTable.find(e => Level >= e.min && Level <= e.max);
	if (!entry) return;
	const newHp = entry.hp;
	if (defaultHp !== newHp) {
		player.triggerEvent(`health.${newHp}`);
	}
}
/**
 * @param {Player} player 
 */
function statusButtonCheat(player){
	const Level = player.getProperty("hxc:level");
	const hatsu = player.getProperty("hxc:hatsu");
	const nen_max = getScoreUtility(player,"nen_max");
	const formCheatMenu = new ModalFormData()
	.title("form.status.cheatMenu")
	.slider({rawtext:[{text:"§b"},{translate:"form.cheatMenu.exp"},{text:"§f"}]}, 0, 200, {valueStep:1,defaultValue:Level})
	.slider({translate:"form.cheatMenu.hatsu"}, 0, 10,{valueStep:1,defaultValue:hatsu})
	.slider({translate:"form.cheatMenu.nen_max"}, 0, 5000,{valueStep:10,defaultValue:nen_max})
	formCheatMenu
	.divider()
	.show(player).then(result =>{
		if (result.canceled == false) {
			const form0 = result.formValues[0];
			const form1 = result.formValues[1];
			const form2 = result.formValues[2];
			if(form0 != Level){
				player.runCommand("playsound random.levelup @s ~~1~ 100000 1 100000");
				player.setProperty("hxc:exp",0);
				updateHp(player);
			}
			if(form1 != hatsu){
				clearAllSkill(player);
			}
			player.setProperty("hxc:level",form0);
			player.setProperty("hxc:hatsu",form1);
			setScoreUtility(player,"nen_max",form2);
			if(form1 == 1){
				player.runCommand("function advancement/hatsu/gon");
			}
			else if(form1 == 2){
				player.runCommand("function advancement/hatsu/killua");
			}
			else if(form1 == 3){
				player.runCommand("function advancement/hatsu/uvogin");
			}
			else if(form1 == 4){
				player.runCommand("function advancement/hatsu/meruem");
			}
			else if(form1 == 5){
				player.runCommand("function advancement/hatsu/feitan");
			}
			else if(form1 == 6){
				player.runCommand("function advancement/hatsu/netero");
			}
		}
	});
}