export default {
    _prepareContext: (context) => {

        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 prepareEigenschaftRoll = (actorData, name) => {
            if (name && name !== "*") {
                return actorData.system.attribute[name.toLowerCase()].aktuell
            } else {
                return 0
            }
        }


        context.skills = {};
        context.flatSkills = [];

        actorData.itemTypes.Skill.forEach((item, index) => {

                    const talentGruppe = item.system.gruppe;
                    const eigenschaften = Object.values(item.system.probe);
                    const werte = [
                        {name: eigenschaften[0], value: prepareEigenschaftRoll(actorData, eigenschaften[0])},
                        {name: eigenschaften[1], value: prepareEigenschaftRoll(actorData, eigenschaften[1])},
                        {name: eigenschaften[2], value: prepareEigenschaftRoll(actorData, eigenschaften[2])}
                    ]
                    if (context.skills[talentGruppe] == null) {
                        context.skills[talentGruppe] = [];
                    }
                    const obj = {
                        type: "talent",
                        gruppe: talentGruppe,
                        name: item.name.replace(/Sprachen kennen/g, "Sprache:").replace(/Lesen\/Schreiben/g, "Schrift: "),
                        taw: "" + item.system.taw,
                        tawPath: `system.items.${index}.taw`,
                        werte,
                        rollEigenschaft1: werte[0].value,
                        rollEigenschaft2: werte[1].value,
                        rollEigenschaft3: werte[2].value,
                        eigenschaft1: werte[0].name,
                        eigenschaft2: werte[1].name,
                        eigenschaft3: werte[2].name,
                        probe: `(${eigenschaften.join("/")})`,
                        id: item._id,
                        at: item.system.at,
                        pa: item.system.pa,
                        komplexität: item.system.komplexität,
                        fav: item.getFlag("DSA_4-1", "favourite")
                    };

                    if (talentGruppe === "Kampf") {

                        if (item.system.pa != null) { // has no parry value so it must be ranged talent (TODO: but it isnt as there can be combatstatistics which has no pa value assigned to)
                            obj.at = item.system.at + context.derived.at.aktuell
                            obj.pa = item.system.pa + context.derived.pa.aktuell
                        } else {
                            obj.at = item.system.at + context.derived.fk.aktuell
                        }
                    }

                    context.skills[talentGruppe].push(obj);
                    context.flatSkills.push(obj);
                }

        )

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

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