import {ActionManager} from "../actions/action-manager.mjs";

export default {
    _prepareContext: async (context, 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 ?? []

        const findEquipmentOnSlot = (slot, setNumber, object) => {
            return object.items.get(object.system.heldenausruestung[setNumber]?.[slot])
        }

        const am = new ActionManager(actorData)
        context.actions = am.evaluate()


        context.inidice = actorData.system.ini.wuerfel
        context.inivalue = actorData.system.ini.aktuell
        context.inimod = actorData.system.ini.mod

        context.aupper = Math.min((actorData.system.aup.aktuell / actorData.system.aup.max) * 100, 100)
        context.lepper = Math.min((actorData.system.lep.aktuell / actorData.system.lep.max) * 100, 100)
        context.keper = Math.min((actorData.system.kap.aktuell / actorData.system.kap.max) * 100, 100)
        context.aspper = Math.min((actorData.system.asp.aktuell / actorData.system.asp.max) * 100, 100)
        context.lepcurrent = actorData.system.lep.aktuell ?? 0

        context.aupcurrent = actorData.system.aup.aktuell ?? 0

        const fernkampf = findEquipmentOnSlot("fernkampf", actorData.system.setEquipped, actorData)
        const links = findEquipmentOnSlot("links", actorData.system.setEquipped, actorData)
        const rechts = findEquipmentOnSlot("rechts", actorData.system.setEquipped, actorData)
        context.attacks = [];

        if (fernkampf) {
            const fkitems = fernkampf.system.rangedSkills.map((skillInQuestion) => actorData.items.find(p => p.name === skillInQuestion))
            fkitems.forEach(async skill => {
                const obj = await skill
                context.attacks.push({
                    name: obj.name,
                    using: fernkampf.name,
                    atroll: `1d20cs<${object.system.fk.aktuell + obj.system.at}`,
                    at: `${object.system.fk.aktuell + obj.system.at}`,
                    tproll: `${fernkampf.system.rangedAttackDamage}`, // TODO consider adding TP/KK mod and Range mod
                    tp: `${fernkampf.system.rangedAttackDamage}`,
                    iniroll: `(${context.inidice})d6 + ${context.inivalue + fernkampf.system.iniModifier ?? 0}`,
                    ini: `${context.inidice}w6 + ${context.inivalue + fernkampf.system.iniModifier ?? 0}`,
                })
            })
        }
        if (links) {
            const meitems = []
            links.system.meleeSkills.forEach((skillInQuestion) => {
                const item = actorData.items.find(p => p.name === skillInQuestion)
                if (item) {
                    meitems.push(item)
                }
            })
            meitems.forEach(skill => {
                const obj = skill
                context.attacks.push({
                    name: obj.name,
                    using: links.name,
                    atroll: `1d20cs<${object.system.at.links.aktuell + obj.system.at + links.system.attackModifier}`, // TODO consider adding W/M
                    at: `${object.system.at.links.aktuell + obj.system.at + links.system.attackModifier}`,
                    paroll: `1d20cs<${object.system.pa.links.aktuell + obj.system.pa + links.system.parryModifier}`, // TODO consider adding W/M
                    pa: `${object.system.pa.links.aktuell + obj.system.pa + links.system.parryModifier}`,
                    tproll: `${links.system.meleeAttackDamage}`, // TODO consider adding TP/KK mod
                    tp: `${links.system.meleeAttackDamage}`,
                    iniroll: `(${context.inidice})d6 + ${context.inivalue + links.system.iniModifier ?? 0}`,
                    ini: `${context.inidice}w6 + ${context.inivalue + links.system.iniModifier ?? 0}`,
                })
            })
        }
        if (rechts) {
            const meitems = []
            rechts.system.meleeSkills.forEach((skillInQuestion) => {
                const item = actorData.items.find(p => p.name === skillInQuestion)
                if (item) {
                    meitems.push(item)
                }
            })
            meitems.forEach(skill => {
                const obj = skill
                context.attacks.push({
                    name: obj.name,
                    using: rechts.name,
                    atroll: `1d20cs<${object.system.at.rechts.aktuell + obj.system.at + rechts.system.attackModifier}`,  // TODO consider adding W/M
                    at: `${object.system.at.rechts.aktuell + obj.system.at + rechts.system.attackModifier}`,
                    paroll: `1d20cs<${object.system.pa.rechts.aktuell + obj.system.pa + rechts.system.parryModifier}`,  // TODO consider adding W/M
                    pa: `${object.system.pa.rechts.aktuell + obj.system.pa + rechts.system.parryModifier}`,
                    tproll: `${rechts.system.meleeAttackDamage}`, // TODO consider adding TP/KK mod
                    tp: `${rechts.system.meleeAttackDamage}`,
                    iniroll: `(${context.inidice})d6 + ${context.inivalue + rechts.system.iniModifier ?? 0}`,
                    ini: `${context.inidice}w6 + ${context.inivalue + rechts.system.iniModifier ?? 0}`,
                })
            })
        }

        return context

    },
    _onRender: (context, options) => {

    },
    _getTabConfig: (group) => {
        group.tabs.push({id: "combat", group: "sheet", label: "Kampf"})
    },
    template: `systems/DSA_4-1/templates/actor/character/tab-combat.hbs`
}