import { Player } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";

/**
 * @param {Player} player
 */
export function status_nen(player){
    const form = player.getProperty("hxc:form");
    if(form=="default" && player.hasTag("nen_control")){
        statusNen(player);
    }
    else if(form!="default" && !player.hasTag("nen_control")){
        player.sendMessage({rawtext:[{translate:"form.status.timeUse.error"}]});
    }
    else if(form!="default" && player.hasTag("nen_control")){
        player.sendMessage({rawtext:[{translate:"form.status.timeUse.error"}]});
    }
    else if(form=="default" && !player.hasTag("nen_control")){
        player.sendMessage({rawtext:[{translate:"form.status.nenControl.error"}]});
    }
}
/**
 * @param {Player} player
 */
export function statusNen(player){
    const Level = player.getProperty("hxc:level");
    const acquiredLevel10 = Level >= 10;
    const acquiredLevel20 = Level >= 20;
    const acquiredLevel30 = Level >= 30;
    const acquiredLevel40 = Level >= 40;
    const familyTypes = player.getComponent("type_family");
    const activeNen = () => {
        const isTen = familyTypes.hasTypeFamily("ten");
        const isRen = familyTypes.hasTypeFamily("ren");
        const isGyo = familyTypes.hasTypeFamily("gyo");
        const isEn = familyTypes.hasTypeFamily("en");
        const isShu = familyTypes.hasTypeFamily("shu");
        const isKo = familyTypes.hasTypeFamily("ko");
        const isKen = familyTypes.hasTypeFamily("ken");
        const isRyu = familyTypes.hasTypeFamily("ryu");
        if (isTen) return "ten";
        if (isRen) return "ren";
        if (isGyo) return "gyo";
        if (isEn) return "en";
        if (isShu) return "shu";
        if (isKo) return "ko";
        if (isKen) return "ken";
        if (isRyu) return "ryu";
        return "";
    };
    const currentActive = activeNen();
    const button0_acquired = acquiredLevel10;
    const button1_acquired = acquiredLevel10;
    const button2_acquired = acquiredLevel10;
    const button3_acquired = acquiredLevel30;
    const button4_acquired = acquiredLevel20;
    const button5_acquired = acquiredLevel30;
    const button6_acquired = acquiredLevel40;
    const button7_acquired = acquiredLevel40;
    const button0_active = currentActive === "ten";
    const button1_active = currentActive === "ren";
    const button2_active = currentActive === "gyo";
    const button3_active = currentActive === "en";
    const button4_active = currentActive === "shu";
    const button5_active = currentActive === "ko";
    const button6_active = currentActive === "ken";
    const button7_active = currentActive === "ryu";
    const data = [
        {acquired:button0_acquired,active:button0_active,type:"ten"},
        {acquired:button1_acquired,active:button1_active,type:"ren"},
        {acquired:button2_acquired,active:button2_active,type:"gyo"},
        {acquired:button3_acquired,active:button3_active,type:"en"},
        {acquired:button4_acquired,active:button4_active,type:"shu"},
        {acquired:button5_acquired,active:button5_active,type:"ko"},
        {acquired:button6_acquired,active:button6_active,type:"ken"},
        {acquired:button7_acquired,active:button7_active,type:"ryu"},
    ]
    const actionForm = new ActionFormData().title("form.status.nen.title");
    for(let num=0;num<data.length;num++){
        if(!data[num].acquired){
            actionForm.button(
                {rawtext:[{text:"§7?"}]},
                "textures/entity/transparent"
            );
        }
        else{
            actionForm.button(
                {rawtext:[{text:data[num].active ? "§1" : ""},{translate:`form.status.nen.changeType.button.${num}`}]},
                `textures/form/nen/type/${data[num].type}_${data[num].active}`
            );
        }
    }
    actionForm.show(player).then(res => {
        if(res.canceled) return;
        const selection = res.selection;
        if(!data[selection].acquired){
            showSubtitle(player,"form.status.nen.changeType.error",15);
        }
        else{
            if(data[selection].active){
                player.triggerEvent("no_nen");
            }
            else {
                if(res.selection === 7){
                    ryuSelect(player,currentActive);
                }else {
                    player.triggerEvent(`${data[selection].type}`);
                }
            }
        }
    });
}
/**
 * @param {Player} player
 */
function ryuSelect(player){
    const ryuType = () => {
        const ryuBody = player.getProperty("hxc:ryu")==0;
        const ryuArm = player.getProperty("hxc:ryu")==1;
        const ryuLeg = player.getProperty("hxc:ryu")==2;
        const ryuArmLeg = player.getProperty("hxc:ryu")==3;
        const ryuArmBody = player.getProperty("hxc:ryu")==4;
        const ryuLegBody = player.getProperty("hxc:ryu")==5;
        if (ryuBody) return "body";
        if (ryuArm) return "arm";
        if (ryuLeg) return "leg";
        if (ryuArmLeg) return "arm_leg";
        if (ryuArmBody) return "arm_body";
        if (ryuLegBody) return "leg_body";
        return "";
    };
    const currentRyu = ryuType();
    const Ryu = [
        {type:"body"},
        {type:"arm"},
        {type:"leg"},
        {type:"arm_leg"},
        {type:"arm_body"},
        {type:"leg_body"},
    ]
    const actionForm = new ActionFormData().title("form.status.ryu.title");
    for(let num=0;num<Ryu.length;num++){
        actionForm.button(
            {rawtext:[{translate:`form.status.ryu.percent.button.${num}`}]},
            `textures/form/nen/type/ryu/${num}`
        );
    }
    actionForm.show(player).then(res => {
        const selection = res.selection;
        if(res.canceled){
            player.playSound("note.bass");
            return;
        }
        else{
            player.triggerEvent("ryu");
            player.setProperty("hxc:ryu",selection);
        }
    });
}