const { DialogV2 } = foundry.applications.api;
const { renderTemplate } = foundry.applications.handlebars;
const { FormDataExtended } = foundry.applications.ux;

export class RollPromptHelper {

    static async roll(options) {
        const htmlContent = await renderTemplate('systems/mgt2/templates/roll-prompt.html', {
            config: CONFIG.MGT2,
            characteristics: options.characteristics,
            characteristic: options.characteristic,
            skills: options.skills,
            skill: options.skill,
            fatigue: options.fatigue,
            encumbrance: options.encumbrance,
            difficulty: options.difficulty
        });

        const theme = game.settings.get("mgt2", "theme");

        return await DialogV2.wait({
            window: { title: options.title ?? options.rollTypeName ?? game.i18n.localize("MGT2.RollPrompt.Roll") },
            content: htmlContent,
            rejectClose: false,
            buttons: [
                {
                    action: "boon",
                    label: game.i18n.localize("MGT2.RollPrompt.Boon"),
                    callback: (event, button, dialog) => {
                        const formData = new FormDataExtended(dialog.element.querySelector('form')).object;
                        formData.diceModifier = "dl";
                        return formData;
                    }
                },
                {
                    action: "submit",
                    label: game.i18n.localize("MGT2.RollPrompt.Roll"),
                    icon: '<i class="fa-solid fa-dice"></i>',
                    default: true,
                    callback: (event, button, dialog) => {
                        return new FormDataExtended(dialog.element.querySelector('form')).object;
                    }
                },
                {
                    action: "bane",
                    label: game.i18n.localize("MGT2.RollPrompt.Bane"),
                    callback: (event, button, dialog) => {
                        const formData = new FormDataExtended(dialog.element.querySelector('form')).object;
                        formData.diceModifier = "dh";
                        return formData;
                    }
                }
            ]
        });
    }
}