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

export class Bagpack extends HandlebarsApplicationMixin(ActorSheetV2) {

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

        }
    }

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

    _actor = null

    constructor(actor) {
        super(actor)
        this._actor = actor
        this.render(true)
    }

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

    static async #newItem(event, target) {
        this._actor?.sheet.options.actions.newItem.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}: Inventar`

        return options
    }

    async _prepareContext(context, options, object) {
        const actorData = this.document
        context.system = actorData.system
        context.flags = actorData.flags

        context.equipments = []
        context.carryingweight = 0
        actorData.itemTypes["Equipment"].sort((a, b) => a.sort - b.sort).forEach((item, index) => {

            // worn items are halved weight

            let effectiveWeight = item.system.weight ?? 0
            if (this.document.isWorn(item._id)) {
                effectiveWeight = item.system.weight ? item.system.weight / 2 : 0
            }

            context.equipments.push({
                index: index,
                id: item._id,
                quantity: item.system.quantity,
                name: item.name,
                icon: item.img ?? "",
                weight: item.system.weight,
                worn: this.document.isWorn(item._id)
            })
            context.carryingweight += item.system.quantity * effectiveWeight;

        })
        context.maxcarryingcapacity = actorData.system.attribute.kk.aktuell
        context.carryingpercentage = Math.min((context.carryingweight / context.maxcarryingcapacity) * 100, 100);

        context.wealth = 0

        actorData.itemTypes["Equipment"].forEach(coin => {
            if (coin.system.category.indexOf("Währung") !== -1) {
                context.wealth += (coin.system.quantity * coin.system.currencyDenominator)
            }
        })

        return context
    }

    _onRender(context, options) {
    }
}