let choice1 = [
  {
    type: "armour",
    name: "Chausses de Mailles"
  },
  {
    type: "armour",
    name: "Cotte de Mailles"
  },
  {
    type: "armour",
    name: "Coiffe de Mailles"
  },
]
let choice2 = [
  {
    type: "armour",
    name: "Chausses de Mailles"
  },
  {
    type: "armour",
    name: "Cotte de Mailles"
  },
  {
    type: "armour",
    name: "Coiffe de Mailles"
  },
  {
    type: "armour",
    name: "Jambières de Cuir"
  },
  {
    type: "armour",
    name: "Calotte de Cuir"
  },
  {
    type: "armour",
    name: "Veste de Cuir"
  },
]
let choice3 = [
  {
    type: "armour",
    name: "Plastron d'acier"
  },
  {
    type: "armour",
    name: "Brassards"
  },
  {
    type: "armour",
    name: "Heaume"
  },
  {
    type: "armour",
    name: "Jambières d'acier"
  },
]

let choice = await new Promise((resolve, reject) => {
  new Dialog({
    title: "Choix",
    content:
      `<p>
        Selectionnez votre choix
        </p>
        <ol>
        <li>Mailles</li>
        <li>Mailles & Cuir</li>
        <li>Plates</li>
        </ol> 
        `,
    buttons: {
      1: {
        label: "Mailles",
        callback: () => {
          resolve(choice1)
        }
      },
      2: {
        label: "Mailles & Cuir",
        callback: () => {
          resolve(choice2)
        }
      },
      3: {
        label: "Plates",
        callback: () => {
          resolve(choice3)
        }
      }
    }
  }).render(true)
})

let updateObj = this.actor.toObject();
let items = []
for (let c of choice) {
  let existing
  if (c.type == "skill") {
    existing = updateObj.items.find(i => i.name == c.name && i.type == c.type)
    if (existing && c.diff?.system?.advances?.value) {
      existing.system.advances.value += c.diff.system.advances.value
    }
  }

  if (!existing) {
    let item = await game.wfrp4e.utility.find(c.name, c.type)
    if (item) {
      item = item.toObject()
      equip(item);
      items.push(foundry.utils.mergeObject(item, (c.diff || {})))
    }
    else
      ui.notifications.warn(`Impossible de trouver ${c.name}`, { permanent: true })
  }

}
await this.actor.update(updateObj)
this.actor.createEmbeddedDocuments("Item", items);

function equip(item) {
  if (item.type == "armour")
    item.system.worn.value = true
  else if (item.type == "weapon")
    item.system.equipped = true
  else if (item.type == "trapping" && item.system.trappingType.value == "clothingAccessories")
    item.system.worn = true
}