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

export class StandaloneADVSF extends HandlebarsApplicationMixin(ActorSheetV2) {

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

        }
    }

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

    _actor = null

    constructor(actor) {
        super(actor)
        this._actor = actor
        this.render(true)
        this.options.window.title = `${this.document.name} Vor und Nachteile`
    }

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

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

    _configureRenderOptions(options) {
        super._configureRenderOptions(options)

        options.window.title = `${this.document.name}: Vor und Nachteile`

        return options
    }

    async _prepareContext(context, options, object) {
        if (this._actor) {
            const actorData = this.document
            context.system = actorData.system
            context.flags = actorData.flags
            context.derived = actorData.system
            context.originalName = actorData.name
            context.name = context.derived.name ?? actorData.name
            context.effects = actorData.effects ?? []
            context.advantages = []
            context.flaws = []

            actorData.itemTypes.Advantage.forEach((item) => {
                    if (!item.system.schlechteEigenschaft) {
                        context.advantages.push({
                            id: item._id,
                            name: item.name,
                            value: item.system.value,
                            options: item.system.auswahl,
                            description: item.system.description,
                            isAdvantage: !item.system.nachteil,
                            isDisadvantage: item.system.nachteil,
                            isBadAttribute: item.system.schlechteEigenschaft,
                            fav: item.getFlag("DSA_4-1", "favourite")
                        })
                    } else {
                        context.flaws.push({
                            id: item._id,
                            name: item.name,
                            value: item.system.value,
                            options: item.system.auswahl,
                            description: item.system.description,
                            isAdvantage: !item.system.nachteil,
                            isDisadvantage: item.system.nachteil,
                            isBadAttribute: item.system.schlechteEigenschaft,
                            fav: item.getFlag("DSA_4-1", "favourite")
                        })
                    }
                }
            )

            context.specialAbilities = []
            actorData.itemTypes.SpecialAbility.forEach((item) => {
                    context.specialAbilities.push({
                        id: item._id,
                        name: item.system.value ? item.system.value : item.name,
                        fav: item.getFlag("DSA_4-1", "favourite")
                    });
                }
            );

            return context
        }
    }

    _onRender(context, options) {
        if (this._actor) {
            new foundry.applications.ux.DragDrop.implementation({
                dropSelector: ".advantages, .special-abilities",
                permissions: {
                    drop: this._actor.sheet._canDragDrop.bind(this._actor.sheet)
                },
                callbacks: {
                    drop: this._actor.sheet._onDrop.bind(this._actor.sheet),
                }
            }).bind(this.element);
        }
    }
}