let characteristics = {
    "ws" : 10,
    "bs" : 20,
    "s" : 0,
    "t" : 10,
    "i" : 15,
    "ag" : 0,
    "dex" : 0,
    "int" : 40,
    "wp" : 45,
    "fel" : 10
}
let skills = ["Kanalisieren", "Besonnenheit", "Einschüchtern", "Sprache (Magick)", "Sprache (Nehekharan)", "Wissen (Magick)", "Wahrnehmung"]
let skillAdvancements = [10, 20, 15, 15, 10, 10, 10]
let talents = ["Arkane Magie (Domäne)", "Robustheit", "Bedrohlich", "Niedere Magie", "Lesen & Schreiben", "Zweites Gesicht"]
let trappings = ["Waffe"]
let specialItems = [
    {name: "Mouldering Robes", type: "trapping", trappingType: "clothingAccessories" },
    {name: "Pouches containing ritual components", type: "trapping", trappingType: "clothingAccessories" },
    {name: "Staff", type: "weapon", damage: "SB+2"},
    {name: "Dunkle Magie (Nekromantie)", type: "talent"},
]
let items = [];

let updateObj = this.actor.toObject();

for (let ch in characteristics)
{
    updateObj.system.characteristics[ch].modifier += characteristics[ch];
}

for (let item of specialItems) {
    let newItem
    if (item.type == "weapon") {
        newItem = new ItemWfrp4e({ name: item.name, type: item.type, system: { equipped: true, damage: {value: item.damage}}  })
    } else if (item.type == "trapping") {
        newItem = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: item.name, type: item.type, system: { worn: true, trappingType: { value: item.trappingType}  } } )
    } else {
        newItem = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: item.name, type: item.type  })
    }
    items.push(newItem.toObject())
}

for (let index = 0; index < skills.length; index++)
{
    let skill = skills[index]
    let skillItem;
    skillItem = updateObj.items.find(i => i.name == skill && i.type == "skill")
    if (skillItem)
        skillItem.system.advances.value += skillAdvancements[index]
    else 
    {
        skillItem = await game.wfrp4e.utility.findSkill(skill)
        skillItem = skillItem.toObject();
        skillItem.system.advances.value = skillAdvancements[index];
        items.push(skillItem);
    }
}

for (let talent of talents)
{
    let talentItem = await game.wfrp4e.utility.findTalent(talent)
    if (talentItem)
    {
        items.push(talentItem.toObject());
    }
    else 
    {
        ui.notifications.warn(`${talent} nicht gefunden`, {permanent : true})
    }
}

for (let trapping of trappings) 
{
    let trappingItem = await game.wfrp4e.utility.findItem(trapping)
    if (trappingItem)
    {
        trappingItem = trappingItem.toObject()

        trappingItem.system.equipped.value = true;

        items.push(trappingItem);
    }
    else 
    {
        ui.notifications.warn(`${trapping} nicht gefunden`, {permanent : true})
    }
}

let filters = [
    {
        property : "type",
        value : "spell"
    },
    {
        property : "system.lore.value",
        value : "petty"
    }
]

let petty = (await ItemDialog.createFromFilters(filters, 3, {text: "Wähle 3 Zaubertricks der Niederen Magie"})).map(i => i.toObject());


filters = [
    {
        property : "type",
        value : "spell"
    },
    {
        property : "system.lore.value",
        value : ""
    }
]

let arcane = (await ItemDialog.createFromFilters(filters, 2, {text : "Wähle 2 Arkane Zauber"})).map(i => i.toObject());

filters = [
    {
        property : "type",
        value : "spell"
    },
    {
        property : "name",
        value: /^((?!\().)*$/gm, // Remove tout spells with parentheses (tout arcane spells spells)
        regex: true
    },
    {
        property : "system.lore.value",
        value : "necromancy"
    }
]

let necromancy = (await ItemDialog.createFromFilters(filters, 1, {text : "Wähle 1 Zauber der Nekromantie"})).map(i => i.toObject());

arcane.forEach(i => {
    i.img = "modules/wfrp4e-core/icons/spells/necromancy.png";
    i.system.lore.value = "necromancy";
})
let spells = [...petty, ...necromancy, ...arcane];

updateObj.name = updateObj.name += " " + this.effect.name
await this.actor.update(updateObj)
this.actor.createEmbeddedDocuments("Item", items.concat(spells));
