export default {
    _prepareContext: async (context, options, object) => {

        const actorData = context.document
        context.system = actorData.system
        context.flags = actorData.flags
        context.derived = context.document.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, thisObject) => {
        new foundry.applications.ux.DragDrop.implementation({
            dropSelector: ".advantages, .special-abilities",
            permissions: {
                drop: thisObject._canDragDrop.bind(thisObject)
            },
            callbacks: {
                drop: thisObject._onDrop.bind(thisObject),
            }
        }).bind(thisObject.element);
    },
    _getTabConfig: (group) => {
        group.tabs.push({id: "advsf", group: "sheet", label: "Vorteile"})
    },
    template: `systems/DSA_4-1/templates/actor/character/tab-advsf.hbs`
}