import { world, system, ItemStack, EnchantmentType, GameMode } from "@minecraft/server";

const death = [
  {
    name: "yokai:bandit",
    property: "banditDaily",
    xp: 1,
    moedas: 2
  },
  {
    name: "yokai:boar",
    property: "boarDaily",
    xp: 1,
    moedas: 0
  },
  {
    name: "yokai:revchi",
    history: 1,
    xp: 10,
    moedas: 30,
    stars: 1
  },
  {
    name: "yokai:fluss",
    xp: 0,
    moedas: 0
  },
  {
    name: "yokai:fluss2",
    history: 2,
    xp: 20,
    moedas: 50,
    stars: 1
  },
  {
    name: "yokai:heath",
    history: 3,
    xp: 40,
    moedas: 70,
    stars: 1
  }
  ]


world.afterEvents.entityHurt.subscribe((data) => {
  const mob = death.findIndex(obj => obj.name == data.hurtEntity.typeId)
  if (mob != -1) {
    if (death[mob].history === undefined) {
      const life = data.hurtEntity.getComponent("health").currentValue
      if (life <= 1 && !data.hurtEntity.hasTag("death")) {
        data.hurtEntity.addTag("death")
        data.hurtEntity.triggerEvent("death")
        let player = data.damageSource.damagingEntity
        if (!player) {
          player = data.hurtEntity.dimension.getPlayers({
            location: data.hurtEntity.location,
            maxDistance: 70,
          })[0]
        }
        const xpValue = player.getDynamicProperty("xp") ?? 0
        world.scoreboard.getObjective("moedas").addScore(player, death[mob].moedas)
          player.setDynamicProperty("xp", xpValue + death[mob].xp)
        const mobDaily = player.getDynamicProperty("mobDaily") ?? 0
        player.setDynamicProperty("mobDaily", mobDaily+1)
        if (death[mob].property) {
          const daily = player.getDynamicProperty(death[mob].property) ?? 0
          player.setDynamicProperty(death[mob].property, daily + 1)
        }
        system.runTimeout(() => {
          data.hurtEntity.triggerEvent("sumir")
        }, 18)
      }
    } else {
      const life = data.hurtEntity.getComponent("health").currentValue
      if (life <= 1 && !data.hurtEntity.hasTag("death")) {
        let tag = data.hurtEntity.getTags()[0]
        if (tag) {
          tag = tag.split("h")[1]
        }
        data.hurtEntity.addTag("death")
        data.hurtEntity.triggerEvent("death")
        const players = data.hurtEntity.dimension.getPlayers()
        const player = players.find(p => p.id == tag)
        
        if (player) {
          const xpValue = player.getDynamicProperty("xp") ?? 0
          
          const stars = player.getDynamicProperty("stars") ?? 0
          
          player.setDynamicProperty("stars", stars + death[mob].stars)
          
          const history = death[mob].history
          
          player.setDynamicProperty(`history${history}`, "end")
          player.setDynamicProperty(`history${history+1}`, "open")
          
          world.scoreboard.getObjective("moedas").addScore(player, death[mob].moedas)
          
            player.setDynamicProperty("xp", xpValue + death[mob].xp)
        }
        system.runTimeout(() => {
          data.hurtEntity.runCommand(`event entity @e[tag=h${player.id}] sumir`)
        }, 18)
      }
    }
  }
})

world.afterEvents.entityDie.subscribe((data) => {
  if (!data.damageSource) return
  let player = data.damageSource.damagingEntity
  if (!data.deadEntity.isValid) return
  if (!player) {
    player = data.deadEntity.dimension.getPlayers({
      location: data.deadEntity.location,
      maxDistance: 40,
    })[0]
  }
  
  if (data.deadEntity.typeId != "minecraft:player") {
    if (!player) return
    if (player.typeId == "minecraft:player") {
      const mobDaily = player.getDynamicProperty("mobDaily") ?? 0
      player.setDynamicProperty("mobDaily", mobDaily+1)
    }
  } else {
    data.deadEntity.runCommand(`event entity @e[tag=h${data.deadEntity.id}] sumir`)
  }
  
})


//Revchi Chains

  system.afterEvents.scriptEventReceive.subscribe((data) => {
    if (data.id == "yokai:chains_atk") {
      const mob = data.sourceEntity
      if (!mob) return
      const players = mob.dimension.getPlayers({
        location: mob.location,
        maxDistance: 10,
        gameMode: GameMode.Survival,
      }) 
      if (players.length > 0) {
        mob.triggerEvent("chains")
        system.runTimeout(() => {
          if (!mob.isValid) return
          mob.triggerEvent("atk_none")
          mob.triggerEvent("chains_wait")
        }, 20)
      }
      for (let player of players) {
        player.triggerEvent("revchi_chains")
        player.triggerEvent("no_knockback")
        player.addTag("movement")
        player.addTag("no_skill")
        player.addTag("camera")
        system.runTimeout(() => {
        player.triggerEvent("knockback")
        player.removeTag("movement")
        player.removeTag("no_skill")
        player.removeTag("camera")
          player.triggerEvent("atk_none")
        }, 150)
      }
    }
  })
  
  
  
  
// fluss
system.afterEvents.scriptEventReceive.subscribe((data) => {
  if (data.id == "yokai:fluss_water") {
    const players = data.sourceEntity.dimension.getPlayers({
      location: data.sourceEntity.location,
      maxDistance: 20,
      gameMode: GameMode.Survival,
    }) 
    if (players.length <= 0) return
    data.sourceEntity.setProperty("yokai:ataques", 1)
    data.sourceEntity.runCommand("tp @s ~~~ facing @p")
    data.sourceEntity.dimension.playSound("shoot_water", data.sourceEntity.location)
    data.sourceEntity.triggerEvent("water_atk")
    system.runTimeout(() => {
      if (!data.sourceEntity.isValid) return
      data.sourceEntity.setProperty("yokai:ataques", 0)
    }, 10)
  }
})


// Heath
system.afterEvents.scriptEventReceive.subscribe((data) => {
  if (data.id == "yokai:heath_ice") {
    const players = data.sourceEntity.dimension.getPlayers({
      location: data.sourceEntity.location,
      maxDistance: 20,
      gameMode: GameMode.Survival,
    }) 
    if (players.length <= 0) return
    data.sourceEntity.setProperty("yokai:ataques", 1)
    data.sourceEntity.runCommand("tp @s ~~~ facing @p")
    data.sourceEntity.dimension.playSound("shoot_water", data.sourceEntity.location)
    data.sourceEntity.triggerEvent("heath_ice")
    system.runTimeout(() => {
      if (!data.sourceEntity.isValid) return
      data.sourceEntity.setProperty("yokai:ataques", 0)
    }, 10)
  }
})