import {ResourceType} from "../data/character.mjs";

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

export class EditResourceDialog extends HandlebarsApplicationMixin(ApplicationV2) {
    static DEFAULT_OPTIONS = {
        classes: ['dsa41', 'dialog', 'resource'],
        tag: "form",
        position: {
            width: 480,
            height: 480
        },
        window: {
            resizable: false,
            title: "Resource anpassen"
        },
        form: {
            submitOnChange: false,
            closeOnSubmit: false,
            handler: EditResourceDialog.#onSubmitForm
        },
        actions: {
            setResource: EditResourceDialog.#onSetResource,
        }
    }

    static PARTS = {
        form: {
            template: 'systems/DSA_4-1/templates/dialog/resource-dialog.hbs',
        }
    }

    _actor = null;
    _resourceType = null;
    _min = 0;
    _current = 0;
    _max = 0;

    constructor(actor, resourceType, min, current, max) {
        super();
        this._actor = actor;
        this._resourceType = resourceType;
        this._min = min;
        this._current = current;
        this._max = max;
    }

    static async #onSubmitForm(event, form, formData) {
        event.preventDefault()
        this.close()
    }

    static async #onSetResource(event, target) {
        const {value} = target.dataset;
        switch(this._resourceType) {
            case ResourceType.LE:
                await this._actor.update({"system.lep.aktuell": value});
                break;
            case ResourceType.AU:
                await this._actor.update({"system.aup.aktuell": value});
                break;
            case ResourceType.AE:
                await this._actor.update({"system.asp.aktuell": value});
                break;
            case ResourceType.KE:
                await this._actor.update({"system.kap.aktuell": value});
        }
        this.close();
    }

    async _prepareContext(options) {
        const context = await super._prepareContext(options)

        context.steps = [];

        for (let i = this._min; i <= this._max; ++i ) {
            if (i < this._current) {
                context.steps.push({
                    display: i - this._current,
                    value: i,
                    isReduction: true,
                    isIncreasing: false,
                });
            } else if (i === this._current) {
                context.steps.push({
                    display: i - this._current,
                    value: i,
                    isReduction: false,
                    isIncreasing: false,
                });
            } else {
                context.steps.push({
                    display: "+" + (i - this._current),
                    value: i,
                    isReduction: false,
                    isIncreasing: true,
                });
            }
        }
        return context
    }

    _configureRenderOptions(options) {
        super._configureRenderOptions(options)
        if (options.window) {
            options.window.title = game.i18n.format("RESOURCE_DIALOG." + this._resourceType + ".title")
        }
        return options
    }
}
