import { world, system} from "@minecraft/server";
import {RandomTeleport} from "../utils/teleport.js"
import { EatArtifacts } from "./artifacts/artifacts.js";

// This is a Set containing a collection of valid effect types.
const validEffectTypes = new Set([
  "absorption", "bad_omen", "blindness", "clear", "conduit_power", "darkness",
  "fatal_poison", "fire_resistance", "haste", "health_boost", "hunger", 
  "instant_damage", "instant_health", "invisibility", "jump_boost", 
  "levitation", "mining_fatigue", "nausea", "night_vision", "poison", 
  "regeneration", "resistance", "saturation", "slow_falling", "slowness", 
  "speed", "strength", "village_hero", "water_breathing", "weakness", "wither"
]);

/**
 * Object array defining the items, along with the effect(s) and modifiers assigned to them.
 * 
 * itemTypeId: string; - The typeId of the item.
 * effects: {
 *      effectType: string; - The type of effect applied by the item.
 *      duration: number; - The duration of the effect applied by the item in seconds.
 *      amplifier: number; - The amplifier of the effect applied by the item.
 *      showParticles: boolean; - Indicates whether particles are shown for the effect.
 * }[];
 * 
 */
const itemEffects = [
  {
    itemTypeId: "edx:deceitweb",
    effects: [
      { effectType: "hunger", duration: 15, amplifier: 1, showParticles: true },
      { effectType: "fatal_poison", duration: 15, amplifier: 0, showParticles: true },
      { effectType: "blindness", duration: 5, amplifier: 0, showParticles: true },
      { effectType: "nausea", duration: 15, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:crimsonspire_cap",
    effects: [
      { effectType: "nausea", duration: 15, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:crimsonspire_slice",
    effects: [
      { effectType: "nausea", duration: 5, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:blue_energy_stew",
    effects: [
      { effectType: "speed", duration: 90, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:eye_globe",
    effects: [
      { effectType: "night_vision", duration: 20, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:fire_agave_leaf",
    effects: [
      { effectType: "fire_resistance", duration: 60, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:fire_agave_leaf",
    effects: [
      { effectType: "strength", duration: 12, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:blindness_potion",
    effects: [
      { effectType: "blindness", duration: 33, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:long_levitation_potion",
    effects: [
      { effectType: "levitation", duration: 240, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:strength_potion",
    effects: [
      { effectType: "strength", duration: 240, amplifier: 1, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:long_blindness_potion",
    effects: [
      { effectType: "blindness", duration: 120, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:levitation_potion",
    effects: [
      { effectType: "levitation", duration: 90, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:levitation_potion",
    effects: [
      { effectType: "levitation", duration: 90, amplifier: 0, showParticles: true }
    ]
  },
  {
    itemTypeId: "edx:yellow_energy_stew",
    effects: [
      { effectType: "haste", duration: 90, amplifier: 0, showParticles: true }
    ]
  }
];

world.afterEvents.itemCompleteUse.subscribe(evd => {
  const { source: entity, itemStack: itemUsed } = evd;
  const player = evd.source;
  var item = evd.itemStack
  EatArtifacts(player,item)
  if (item.typeId === "edx:deceitweb")
    {
      world.gameRules.showDeathMessages = false
        player.applyDamage(8,{cause: "magic",damagingEntity: player})
        system.runTimeout(() => {
          if(player.getComponent("health").currentValue ==0)
          {
       var Deceit1Message= {rawtext :[
      {text: player.name},{text:" "},{ translate: "message.Deceit1Message"}
]};
       var Deceit2Message= {rawtext :[
      {text: player.name},{text:" "},{ translate: "message.Deceit2Message"}
]};
       var Deceit3Message= {rawtext :[
      {text: player.name},{text:" "},{ translate: "message.Deceit3Message"}
]};
       var Deceit4Message= {rawtext :[
      {text: player.name},{text:" "},{ translate: "message.Deceit4Message"}
]};
       var Deceit5Message= {rawtext :[
      {text: player.name},{text:" "},{ translate: "message.Deceit5Message"}
]};
       var Deceit6Message= {rawtext :[
      {text: player.name},{text:" "},{ translate: "message.Deceit6Message"}
]};
            const random = Math.random()
            if(random<0.2)
              world.sendMessage(Deceit1Message)
            else if(random<0.4)
              world.sendMessage(Deceit2Message)
            else if(random<0.6)
              world.sendMessage(Deceit3Message)
            else if(random<0.8)
              world.sendMessage(Deceit4Message)
            else if(random<0.8)
              world.sendMessage(Deceit5Message)
            else
              world.sendMessage(Deceit6Message)
          }
          world.gameRules.showDeathMessages = true
      },2);
    }
  if (item.typeId === "edx:blue_energy_stew")
  {
      player.runCommand(`give @s bowl`)
  }
  if (item.typeId === "edx:yellow_energy_stew")
  {
     player.runCommand(`give @s bowl`)
  }
  if (item.typeId === "edx:chorus_stew")
  {
    player.runCommand(`give @s bowl`)
  }
  if (item.typeId === "edx:blindness_potion")
  {
    player.runCommand(`give @s glass_bottle`)
  }
  if (item.typeId === "edx:levitation_potion")
  {
    player.runCommand(`give @s glass_bottle`)
  }
  if (item.typeId === "edx:long_blindness_potion")
  {
    player.runCommand(`give @s glass_bottle`)
  }
  if (item.typeId === "edx:long_levitation_potion")
  {
    player.runCommand(`give @s glass_bottle`)
  }
  if (item.typeId === "edx:strength_potion")
  {
    player.runCommand(`give @s glass_bottle`)
  }
  if (item.typeId === "edx:muscle_fiber")
  {
    player.runCommand(`effect @s strength 15 0`)
  }
  if (item.typeId === "edx:oozeshroom_juice")
  {
    let health = player.getComponent("health")
    if(player.getEffect("wither")!=undefined)
    {
      health.setCurrentValue(health.currentValue+4)
      player.runCommand(`give @s glass_bottle`)
    }
    player.removeEffect("wither")
  }
  if (item.typeId === "edx:snow_cone")
  {
    player.runCommand(`give @s paper`)
  }
  if (item.typeId === "edx:suspicious_pie")
  {
    player.runCommand(`event entity @s suspicious_pie`)
  }

  if (item.typeId === "edx:fire_potion")
  {
    player.runCommand(`scoreboard players set @s Fire_breath 220`)
    player.runCommand(`give @s glass_bottle`)
  }
  if (item.typeId === "edx:teleportation_potion_1")
  {
      RandomTeleport(player,3000,1000,true)
  }
  if (item.typeId === "edx:teleportation_potion_2")
  {
      player.runCommand(`give @s glass_bottle`)
      RandomTeleport(player,6000,3000,true)
  }
    // This returns if the entity is not a player, or itemUsed is undefined.
    if (entity.typeId !== "minecraft:player" || !itemUsed) return;

    // This searches for a item in the itemEffects array matching the itemUsed.
    const foundItem = itemEffects.find(item => item.itemTypeId === itemUsed.typeId);

    // If the item is within the itemEffects array, then it will apply the effect(s) and modifiers assigned to the itemUsed.
    if (foundItem) {
      foundItem.effects.forEach(effectInfo => {
        const { effectType, duration, amplifier, showParticles } = effectInfo;
        const durationInTicks = 20 * duration;
        entity.addEffect(effectType, durationInTicks, { amplifier, showParticles });
      });
    }
});