import { system } from "@minecraft/server";

const VALID_ELEMENTS = ["pyro", "hydro", "geo", "anemo", "electro", "necro", "ender"];
const VALID_ABILITIES = ["passive", "primary", "skill1", "skill2", "skill3"];

// /scriptevent oct_eg:upgradeability set <elementKey> <abilityKey> <level>
// Set the upgrade level of an ability to a specific level

// /scriptevent oct_eg:upgradeability setall <elementKey> <level>
// Set the upgrade level of all abilities of an element to a specific level

// /scriptevent oct_eg:upgradeability reset <elementKey> <abilityKey>
// Reset the upgrade level of an ability to 0

// /scriptevent oct_eg:upgradeability resetall <elementKey>
// Reset the upgrade level of all abilities of an element to 0


system.afterEvents.scriptEventReceive.subscribe((event) => {
    const { id, message, sourceEntity: sender } = event;

    if (id !== "oct_eg:upgradeability") return;

    const parts = message.trim().split(/\s+/);
    const subcommand = parts[0];
    const args = parts.slice(1);

    try {
        let success = false;

        switch (subcommand) {
            case "set": {
                const [elementKey, abilityKey, levelStr] = args;
                success = upgradeAbilitySet(sender, elementKey, abilityKey, levelStr);
                break;
            }
            case "setall": {
                const [elementKey, levelStr] = args;
                success = upgradeAbilitySetAll(sender, elementKey, levelStr);
                break;
            }
            case "reset": {
                const [elementKey, abilityKey] = args;
                success = upgradeAbilitySet(sender, elementKey, abilityKey, "0");
                break;
            }
            case "resetall": {
                const [elementKey] = args;
                success = upgradeAbilitySetAll(sender, elementKey, "0");
                break;
            }
            default:
                sender.sendMessage("§cUnknown subcommand for upgradeability.");
                return;
        }

        if (!success) {
            sender.sendMessage("§cFailed to execute command.");
        }
    } catch (err) {
        sender.sendMessage(`§cCommand failed — internal error.\n${err}`);
    }
});

function upgradeAbilitySet(sender, elementKey, abilityKey, levelStr) {
    if (!elementKey || !abilityKey || !levelStr) {
        sender.sendMessage("§cUsage: !oct_eg upgradeability set <elementKey> <abilityKey> <level>");
        return false;
    }

    if (!VALID_ELEMENTS.includes(elementKey)) {
        sender.sendMessage(`§cInvalid element key: ${elementKey}\nValid keys: ${VALID_ELEMENTS.join(", ")}`);
        return false;
    }

    if (!VALID_ABILITIES.includes(abilityKey)) {
        sender.sendMessage(`§cInvalid ability key: ${abilityKey}\nValid keys: ${VALID_ABILITIES.join(", ")}`);
        return false;
    }

    const level = parseInt(levelStr);
    if (isNaN(level) || level < 0 || level > 3) {
        sender.sendMessage(`§cInvalid level: ${levelStr} (must be 0-3)`);
        return false;
    }

    // Attempt to set the value
    try {
        const gauntlet = sender.gauntletManager?.gauntlets?.get(`oct_eg:${elementKey}_gauntlet`);
        if (!gauntlet) {
            sender.sendMessage(`§cGauntlet not found for element: ${elementKey}`);
            return false;
        }

        gauntlet.upgradeStages[abilityKey] = level;
        sender.setDynamicProperty(`oct_eg:ul.${elementKey}.${abilityKey}`, level);

        sender.sendMessage(`§gSet §d${elementKey} - ${abilityKey}§g to level §q${level}§r`);

    } catch (e) {
        return false;
    };

    return true;
}

function upgradeAbilitySetAll(sender, elementKey, levelStr) {
    if (!elementKey || !levelStr) {
        sender.sendMessage("§cUsage: !oct_eg upgradeability setall <elementKey> <level>");
        return false;
    }

    if (!VALID_ELEMENTS.includes(elementKey)) {
        sender.sendMessage(`§cInvalid element key: ${elementKey}\nValid keys: ${VALID_ELEMENTS.join(", ")}`);
        return false;
    }

    const level = parseInt(levelStr);
    if (isNaN(level) || level < 0 || level > 3) {
        sender.sendMessage(`§cInvalid level: ${levelStr} (must be 0-3)`);
        return false;
    }

    try {
        const gauntlet = sender.gauntletManager?.gauntlets?.get(`oct_eg:${elementKey}_gauntlet`);
        if (!gauntlet) return false;

        for (const abilityKey of VALID_ABILITIES) {
            gauntlet.upgradeStages[abilityKey] = level;
            sender.setDynamicProperty(`oct_eg:ul.${elementKey}.${abilityKey}`, level);
        }

        sender.sendMessage(`§gSet all abilities of §d${elementKey}§g to level §q${level}§r`);
    } catch (err) {
        return false;
    }

    return true;
}