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

export class SpellSheet extends HandlebarsApplicationMixin(DocumentSheetV2) {


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

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

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

    _configureRenderOptions(options) {
        super._configureRenderOptions(options)

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

        return options
    }

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

        context.system = spellData.system;
        context.flags = spellData.flags;
        context.name = spellData.name;

        return context;
    }

}