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

export class LiturgySheet extends HandlebarsApplicationMixin(DocumentSheetV2) {

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


    static TABS = {
        sheet: {
            tabs: [
                {id: 'meta', group: 'sheet', label: 'Meta'},
                {id: 'commonality', group: 'sheet', label: 'Verbreitung'},
            ],
            initial: 'meta'
        }
    }

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

    _configureRenderOptions(options) {
        super._configureRenderOptions(options)

        if (options.window) {
            options.window.title = this.document.name
        }

        return options
    }

    /**
     * Handle form submission
     * @this {EquipmentSheet}
     * @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 liturgyData = context.document;

        context.system = liturgyData.system
        context.flags = liturgyData.flags
        context.name = liturgyData.name
        context.json = JSON.stringify(liturgyData)
        return context;
    }

}
