const SPEEDICON = 'icons/skills/movement/feed-winged-boots-glowing-yellow.webp'
const SLOTHICON = 'icons/magic/control/encase-creature-spider-hold.webp'

let tokens = [];
let targets = Array.from(game.user.targets);
if (targets.length > 0) {
    tokens = targets;
} else if (canvas.tokens.controlled.length > 0) {
    tokens = canvas.tokens.controlled;
}
if (tokens.length > 0) {
    main(tokens);
} else {
    ui.notifications.error("Please select or target a token");
}

async function main(tokens) {
    let tokenList = tokens.map(t => t.name).join(", ");
    let menuOptions = {
        title: 'Sloth/Speed',
        defaultButton: "Cancel",
        options: {}
    };
    let menuData = {
        inputs: [
            { type: 'header', label: 'Sloth/Speed' },
            { type: 'info', label: `Affected Tokens: ${tokenList}` },
            { type: 'info', label: "Speed or Sloth" },
            { type: 'radio', label: 'Speed', options: ['isspeed', true] },
            { type: 'radio', label: 'Sloth', options: ['isspeed', false] },
        ],
        buttons: [
            { label: "Apply", value: "apply" },
            { label: "Apply with raise", value: "raise" },
            { label: "Cancel", value: "cancel" }
        ]
    }
    let { buttons, inputs } = await warpgate.menu(menuData, menuOptions);
    console.log(buttons, inputs)
    if (buttons != "cancel") {
        let direction = inputs[3] || inputs[4];
        createEffect(tokens, direction, buttons);
    }
}

async function createEffect(tokens, trait, direction, buttons) {
    const raise = (buttons == 'raise');
    const effectName = `${raise ? "Major" : "Minor"} ${direction} ${trait.name}`;
    const effectId = `${raise ? "Major" : "Minor"}${direction}${trait.name}`;
    const effectIcon = (direction == "Speed" ? SPEEDICON : SLOTHICON)
    for (const token of tokens) {
        let tokenDoc = token.document;
        let effectData = {
            icon: effectIcon,
            id: effectId,
            label: effectName,
            flags: {
                swade: {
                    expiration: 3,
                    loseTurnOnHold: true
                }
            },
            changes: []
        };
        let mode = foundry.CONST.ACTIVE_EFFECT_MODES.MULTIPLY;
        if (direction == "Speed") {
            effectData.duration = { rounds: 5 };
            effectData.changes.push({
                key: 'data.stats.speed.value',
                mode: mode,
                value: 2
            })
        } else {
            effectData.changes.push({
                key: 'data.stats.speed.value',
                mode: mode,
                value: 0.5
            })
        }
        let mutate = {
            embedded: {
                ActiveEffect: {
                }
            }
        };
        mutate.embedded.ActiveEffect[effectName] = effectData;
        let mutateOptions = {
            comparisonKeys: { 'ActiveEffect': 'label' },
            name: effectName,
            permanent: true,
            description: effectName,
        }
        await warpgate.mutate(token.document, mutate, {}, mutateOptions);
    }
}