const MagicShardManager = {

  MAX: 10,

  SHARDS: {

    "kubejs:mana_crystal": {
      types: ["mana", "arsmana"],
      value: 25,
      attrs: [
        { id: "irons_spellbooks:max_mana", base: 100 },
        { id: "ars_nouveau:ars_nouveau.perk.max_mana", base: 0 }
      ],
      color: "§b",
      text: "Mana"
    },

    "kubejs:cooldown_reduction_crystal": {
      types: ["cooldown"],
      value: 0.03,
      attrs: [
        { id: "irons_spellbooks:cooldown_reduction", base: 1 }
      ],
      color: "§d",
      text: "Cooldown Reduction"
    },

    "kubejs:spell_power_crystal": {
      types: ["spellPower"],
      value: 0.5,
      attrs: [
        { id: "irons_spellbooks:spell_power", base: 6 }
      ],
      color: "§c",
      text: "Spell Power"
    },

    "kubejs:mana_regeneration_crystal": {
      types: ["regenera"],
      value: 0.1,
      attrs: [
        { id: "irons_spellbooks:mana_regen", base: 1 }
      ],
      color: "§d",
      text: "Regeneration"
    }
  },

  getData(server, player) {
    if (!server.persistentData.magicShardBonuses)
      server.persistentData.magicShardBonuses = {}

    const uuid = player.uuid.toString()

    if (!server.persistentData.magicShardBonuses[uuid]) {
      server.persistentData.magicShardBonuses[uuid] = {
        mana: 0,
        arsmana: 0,
        cooldown: 0,
        spellPower: 0,
        regenera: 0,

        mana_count: 0,
        cooldown_count: 0,
        spellPower_count: 0,
        regenera_count: 0
      }
    }

    const data = server.persistentData.magicShardBonuses[uuid]

    // Save-Migration (fix für alte Spieler)
    if (data.mana_count === undefined) data.mana_count = 0
    if (data.cooldown_count === undefined) data.cooldown_count = 0
    if (data.spellPower_count === undefined) data.spellPower_count = 0
    if (data.regenera_count === undefined) data.regenera_count = 0

    return data
  },

  apply(player, server) {
    const data = this.getData(server, player)

    Object.values(this.SHARDS).forEach(shard => {
      shard.attrs.forEach(attr => {

        let bonus = 0

        if (attr.id === "irons_spellbooks:max_mana")
          bonus = data.mana

        if (attr.id === "ars_nouveau:ars_nouveau.perk.max_mana")
          bonus = data.arsmana

        if (attr.id === "irons_spellbooks:cooldown_reduction")
          bonus = data.cooldown

        if (attr.id === "irons_spellbooks:spell_power")
          bonus = data.spellPower

        if (attr.id === "irons_spellbooks:mana_regen")
          bonus = data.regenera

        server.runCommandSilent(
          `attribute ${player.username} ${attr.id} base set ${attr.base + bonus}`
        )
      })
    })
  },

  consume(player, server, item) {
    const shard = this.SHARDS[item.id]
    if (!shard) return

    const data = this.getData(server, player)

    const mainType = shard.types[0]
    const countKey = mainType + "_count"

    // Hard Cap: 10 Stück im ganzen Leben
    if (data[countKey] >= this.MAX) {
      player.tell("§cYou can never absorb more of this crystal.")
      return
    }

    // Bonus geben
    shard.types.forEach(type => {
      data[type] += shard.value
    })

    // Counter erhöhen
    data[countKey]++

    // Item -1
    item.count--

    // Blindness
    server.runCommandSilent(
      `effect give ${player.username} minecraft:blindness 2 0 true`
    )

    // Sound
    server.runCommandSilent(
      `playsound goety:apostle_death master ${player.username}`
    )

    // Partikel Ritual
    server.runCommandSilent(
      `particle minecraft:soul_fire_flame ${player.x} ${player.y + 1} ${player.z} 0.4 0.6 0.4 0.02 40`
    )
    server.runCommandSilent(
      `particle minecraft:enchant ${player.x} ${player.y + 1} ${player.z} 0.5 0.8 0.5 0.1 60`
    )

    this.apply(player, server)

    player.tell(
      `${shard.color}Crystal ${shard.text} absorbed! §7(${data[countKey]}/${this.MAX})`
    )
  }
}


// Rechtsklick
ItemEvents.rightClicked(event => {
  MagicShardManager.consume(event.player, event.server, event.item)
})

// Permanent Reapply
PlayerEvents.loggedIn(event => {
  MagicShardManager.apply(event.player, event.server)
})

PlayerEvents.respawned(event => {
  MagicShardManager.apply(event.player, event.server)
})
