/**
 *
 * @param {Roll} roll instance of a foundry Die Roll
 * @param {User} user the user rolled the Die
 * @param {Actor} actor
 * @param {Boolean} blind determines if the faces of the die should be obfuscated to user
 * @param {Boolean} secret determines if others can see the die result
 * @param {String} template path to the template that should be displayed in chat
 * @param {[String: *]} templateContext values to be used for rendering the template
 * @returns {Promise<void>}
 */
const displayRoll = async(roll, user=game.user, actor, blind=true, secret=true, template, templateContext) => {

    const rollMode = game.settings.get('core', 'rollMode')


    if (game.dice3d) {

        let blind = false // hidden to user
        let secret = false // hidden to others

        switch (rollMode) {
            case 'selfroll': // secret to others but not blind
                blind = false
                secret = true
                break;
            case 'blindgmroll': // secret to others and to user
                blind = true
                secret = true
                break;
            case 'gmroll': // secret to others but not to user
                blind = false
                secret = true
                break;
            case 'publicroll': // no secret to anyone
            default:
                blind = false
                secret = false
        }

        game.dice3d.showForRoll(roll, user, true, null, blind, null,
            ChatMessage.getSpeaker({actor}), {
                ghost: false,
                secret
            })
    }

    if (template) {
        try {
            await loadTemplates([template])
            const htmlContent = await renderTemplate(template, templateContext)
            const chatData = await (ChatMessage.create({
                user: user._id,
                speaker: {actor},
                content: htmlContent,
            }))
            chatData.applyRollMode("roll")
        } catch (err) {
            console.log(err)
        }

    }
}

export {
    displayRoll
}