const ICON = 'icons/magic/symbols/elements-air-earth-fire-water.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 dialogOptions = {
        title: "Elemental Manipulation",
        content: `Apply <em>Elemental Manipulation</em> to ${tokenList}`,
        default: "cancel",
        buttons: [
            {label: "Apply", value: "apply"},
            {label: "Apply with raise", value: "raise"},
            {label: "Cancel", value: "cancel"}
        ]
    }
    let choice = await warpgate.buttonDialog(dialogOptions);
    if (choice != "cancel") {
        createEffect(tokens, choice);
    }
}

async function createEffect(tokens, choice) {
    let effectName = 'Elemental Manipulation';
    let effectId = 'elementalmanipulation';
    const effectIcon = ICON;
    let changes = [];
    for (const token of tokens) {
        let effectData = {
            icon: effectIcon,
            id: effectId,
            label: effectName,
            duration: {rounds: 5},
            flags: {
                swade: {
                    expiration: 3,
                    loseTurnOnHold: true
                }
            },
            changes: changes,
        };
        let range = token.actor.data.data.attributes.smarts.die.sides.toString()
        let itemMutate = {
            embedded: {
                Item: {
                    "Elemental Manipulation: Thrown Rocks": {
                        "name": "Elemental Manipulation: Thrown Rock",
                        "type": "weapon",
                        "img": "icons/magic/earth/projectile-stone-ball-orange.webp",
                        "data": {
                            "description": "",
                            "notes": "",
                            "additionalStats": {},
                            "quantity": 1,
                            "weight": 0,
                            "price": 0,
                            "equippable": true,
                            "equipped": true,
                            "isArcaneDevice": false,
                            "isVehicular": false,
                            "mods": 1,
                            "actions": {
                                "skill": "Athletics",
                                "skillMod": "",
                                "dmgMod": "",
                                "additional": {}
                            },
                            "bonusDamageDie": 6,
                            "damage": "2d4",
                            "range": range,
                            "rof": "1",
                            "ap": 0,
                            "parry": 0,
                            "minStr": "",
                            "shots": 0,
                            "currentShots": 0,
                            "ammo": "",
                            "autoReload": false
                        },
                        "effects": [],
                        "folder": null,
                        "sort": 0,
                        "permission": { "default": 0 },
                        "flags": {}
                    },
                    "Elemental Manipulation: Fire Blast": {
                        "name": "Elemental Manipulation: Fire Blast",
                        "type": "weapon",
                        "img": "icons/magic/fire/explosion-fireball-medium-orange.webp",
                        "data": {
                            "description": "",
                            "notes": "",
                            "additionalStats": {},
                            "quantity": 1,
                            "weight": 0,
                            "price": 0,
                            "equippable": true,
                            "equipped": true,
                            "isArcaneDevice": false,
                            "isVehicular": false,
                            "mods": 1,
                            "actions": {
                                "skill": "Athletics",
                                "skillMod": "",
                                "dmgMod": "",
                                "additional": {}
                            },
                            "bonusDamageDie": 6,
                            "damage": "2d4",
                            "range": range,
                            "rof": "1",
                            "ap": 0,
                            "parry": 0,
                            "minStr": "",
                            "shots": 0,
                            "currentShots": 0,
                            "ammo": "",
                            "autoReload": false
                        },
                        "effects": [],
                        "folder": null,
                        "sort": 0,
                        "permission": { "default": 0 },
                        "flags": {}
                    }
                }
            }
        }
        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);
        mutateOptions.permanent = false;
        await warpgate.mutate(token.document, itemMutate, {}, mutateOptions);
    }
}