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

export class SpeciesSheet extends HandlebarsApplicationMixin(DocumentSheetV2) {

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

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

    _configureRenderOptions(options) {
        super._configureRenderOptions(options)

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

        return options
    }

    /**
     * Handle form submission
     * @this {SpeciesSheet}
     * @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)
        context.system = context.document.system

        context.name = context.document.name
        context.img = context.document.img
        context.description = context.document.system.description
        context.baseSpeed = context.document.system.baseSpeed
        context.masculineDemonym = context.document.system.masculineDemonym
        context.feminineDemonym = context.document.system.feminineDemonym

        return context
    }
}