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

export class AdvantageSheet extends HandlebarsApplicationMixin(DocumentSheetV2) {

    /** @inheritDoc */
    static DEFAULT_OPTIONS = {
        position: {width: 520, height: 480},
        classes: ['dsa41', 'sheet', 'item', 'advantage'],
        tag: 'form',
        form: {
            submitOnChange: true,
            closeOnSubmit: false,
            handler: AdvantageSheet.#onSubmitForm
        }
    }

    static TABS = {
        sheet: {
            tabs: [
                {id: 'advantage', group: 'sheet', label: 'Vorteil'},
            ],
            initial: 'advantage'
        }
    }

    /** @inheritDoc */
    static PARTS = {
        form: {
            template: `systems/DSA_4-1/templates/item/advantage/main-sheet.hbs`
        },
        advantage: {
            template: `systems/DSA_4-1/templates/item/advantage/tab-advantage.hbs`
        }
    }

    /**
     * Handle form submission
     * @this {AdvantageSheet}
     * @param {SubmitEvent} event
     * @param {HTMLFormElement} form
     * @param {FormDataExtended} formData
     */
    static async #onSubmitForm(event, form, formData) {
        event.preventDefault()

        await this.document.update(formData.object) // Note: formData.object
    }

    /** @override */
    async _prepareContext(options) {
        const context = await super._prepareContext(options);
        const advantageData = context.document;

        context.system = advantageData.system;
        context.flags = context.system.flags;
        context.hasChoices = context.system.auswahl.length > 0;
        context.choices = {}
        context.system.auswahl.forEach(a => {
            context.choices[a] = a
        })
        context.hasModality = context.system.value != null

        return context;
    }
}
