import { Player, EquipmentSlot, world } from "@minecraft/server";
import { getScoreUtility, operationScoreUtility, setScoreUtility } from "./scoreboard";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import * as skillList from "./skill_list";
import { giveCommand,clearCommand, clearItemHasTag } from "./function";
import { getHatsuData } from "./hatsu";

/**
 * @param {Player} player
 */
export function status_hatsu(player){
    const hatsu = player.getProperty("hxc:hatsu");
    if(hatsu==0 && player.hasTag("nen_control")){
        player.sendMessage({rawtext:[{translate:"form.status.hatsu.error"}]});
    }
    else if(hatsu==0 && !player.hasTag("nen_control")){
        player.sendMessage({rawtext:[{translate:"form.status.hatsu.error"}]});
        player.sendMessage({rawtext:[{translate:"form.status.nenControl.error"}]});
    }
    else if(hatsu!=0 && !player.hasTag("nen_control")){
        player.sendMessage({rawtext:[{translate:"form.status.nenControl.error"}]});
    }
    else{
        statusHatsu(player,hatsu);
    }
}
/**
 * @param {Player} player
 * @param {Number} hatsu
 */
function statusHatsu(player,hatsu){
    const hatsuName = getHatsuData({num:hatsu}).itemId;
    const skillList = convertToSkillFormDate(player,hatsu).skillList;
    const currentLevel = player.getProperty("hxc:level");
    let form = new ActionFormData().title({rawtext:[{text:"\ue180"},{translate:getHatsuData({num:hatsu}).langId},{text:"\ue180"}]});
    for (let number = 0; number <= skillList.length-1; number++) {
        if(skillList[number].level <= currentLevel){
            const skillName = skillList[number].name;
            form.button({rawtext:[{text:`${hasItemInInventory(player,`hxc:${skillName}`)?"§1":""}`},{translate:skillName}]},`textures/items/${hatsuName}/${skillName}`);
        }
    }
    form.show(player).then(response => {
        if (response.canceled == false){
            const inventoryComp = player.getComponent("minecraft:inventory");
            /**@type {Number} */
            const responseNumber = response.selection;
            const selectItemName = skillList[responseNumber].name;
            if(hasItemInInventory(player,`hxc:${selectItemName}`)){
                clearCommand(player,`hxc:${selectItemName}`);
            }
            else{
                if(inventoryComp.container.emptySlotsCount > 0){
                    giveCommand(player,`hxc:${selectItemName}`,1,true,"inventory");
                }
                else{
                    player.sendMessage({rawtext:[{translate:"rawtext.inventory_full"}]});
                }
            }
        }
    });
}

/**
 * @param {Player} player
 * @param {Number} hatsu
 */
function convertToSkillFormDate(player,hatsu){
    const form = player.getProperty("hxc:form");
    if(hatsu==1 && form=="default"){
        return {
            skillList:skillList.skillGon
        }
    }
    else if(hatsu==1 && form=="gon_adult"){
        return {
            skillList:skillList.skillGon2
        }
    }
    else if(hatsu==2){
        return {
            skillList:skillList.skillKillua
        }
    }
    else if(hatsu==3){
        return {
            skillList:skillList.skillUvogin
        }
    }
    else if(hatsu==4){
        return {
            skillList:skillList.skillMeruem
        }
    }
    else if(hatsu==5 && form=="default"){
        return {
            skillList:skillList.skillFeitan
        }
    }
    else if(hatsu==5 && form=="feitan_angry"){
        return {
            skillList:skillList.skillFeitan2
        }
    }
    else if(hatsu==6){
        return {
            skillList:skillList.skillNetero
        }
    }
}
/**
 * @param {Player} player
 * @param {String} itemName
 */
function hasItemInInventory(player,itemName){
    const inventoryComp = player.getComponent("minecraft:inventory");
    let count = 0;
    for(let slotNumber=0; slotNumber<=35; slotNumber++ ){
        const itemStack = inventoryComp.container.getItem(slotNumber);
        if(itemStack!=undefined){
            if(itemStack.typeId == itemName){
                count = 1;
            }
        }
    }
    const equippableComp = player.getComponent("minecraft:equippable");
    for(const equipmentSlot of Object.values(EquipmentSlot)){
        if(equippableComp.getEquipment(equipmentSlot)!=undefined){
            if(equippableComp.getEquipment(equipmentSlot).typeId == itemName){
                count = 1;
            }
        }
    }
    return count == 1 ? true : false;
}
/**
 * @param {Player} player
 * @param {String} itemName
 */
function hasItemAmountInInventory(player,itemName){
    const inventoryComp = player.getComponent("minecraft:inventory");
    let count = 0;
    for(let slotNumber=0; slotNumber<=35; slotNumber++ ){
        const itemStack = inventoryComp.container.getItem(slotNumber);
        if(itemStack!=undefined){
            if(itemStack.typeId == itemName){
                count = count + itemStack.amount;
            }
        }
    }
    return count;
}