const {HandlebarsApplicationMixin, DocumentSheetV2} = foundry.applications.api
const {ActorSheetV2} = foundry.applications.sheets

export class StandaloneSpells extends HandlebarsApplicationMixin(ActorSheetV2) {

    /** @inheritDoc */
    static DEFAULT_OPTIONS = {
        position: {width: 520, height: 480},
        classes: ['dsa41', 'sheet', 'actor', 'character', 'standalone', 'spells'],
        tag: 'form',
        actions: {
            openEmbeddedDocument: StandaloneSpells.#openEmbeddedDocument,
            castSpell: StandaloneSpells.castSpell,
        }
    }

    /** @inheritDoc */
    static PARTS = {
        form: {
            template: `systems/DSA_4-1/templates/actor/character/standalone/spells.hbs`
        }
    }

    _actor = null

    constructor(actor) {
        super(actor)
        this._actor = actor
        this.render(true)
    }

    static async #openEmbeddedDocument(event, target) {
        this._actor?.sheet.options.actions.openEmbeddedDocument.bind(this)(event, target)
    }

    static async castSpell(event, target) {
        this._actor?.sheet.options.actions.castSpell.bind(this)(event, target)
    }

    _configureRenderOptions(options) {
        super._configureRenderOptions(options)

        options.window.title = `${this.document.name}: Zauber und Rituale`

        return options
    }

    async _prepareContext(context, options, object) {

        const actorData = this.document
        context.spells = []
        context.system = actorData.system
        context.flags = actorData.flags
        context.derived = this.document.system
        context.originalName = actorData.name
        context.name = context.derived.name ?? actorData.name
        context.effects = actorData.effects ?? []

        const cleanUpMerkmal = (merkmale) => {
            return merkmale.split(",").map((merkmal) => merkmal.trim())
        }

        const prepareEigenschaftRoll = (actorData, name) => {
            if (name && name !== "*") {
                return actorData.system.attribute[name.toLowerCase()].aktuell
            } else {
                return 0
            }
        }

        actorData.itemTypes["Spell"].forEach((item, index) => {

            const eigenschaften = item.system.probe;
            const werte = [
                {name: eigenschaften[0], value: prepareEigenschaftRoll(actorData, eigenschaften[0])},
                {name: eigenschaften[1], value: prepareEigenschaftRoll(actorData, eigenschaften[1])},
                {name: eigenschaften[2], value: prepareEigenschaftRoll(actorData, eigenschaften[2])}
            ]
            context.spells.push({
                id: item._id,
                name: item.name,
                zfw: item.system.zfw,
                hauszauber: item.system.hauszauber,
                merkmal: cleanUpMerkmal(item.system.merkmal),
                rollEigenschaft1: werte[0].value,
                rollEigenschaft2: werte[1].value,
                rollEigenschaft3: werte[2].value,
                eigenschaft1: werte[0].name,
                eigenschaft2: werte[1].name,
                eigenschaft3: werte[2].name,
                fav: item.getFlag("DSA_4-1", "favourite")
            })

        })
        context.hasSpells = context.spells.length > 0

        return context
    }

    _onRender(context, options) {
    }
}