import {world, system} from '@minecraft/server';
import './general.js';
import './fases.js';
import './comandos.js';
import './componentes.js';












export const timer = {
  s: 20,
  m: 1200
}

export const list = [
  "minecraft:bat",// 
  "minecraft:cow",// 
  "minecraft:creeper",// 
  "minecraft:dolphin",// 
  "minecraft:ender_dragon",// 
  "minecraft:enderman",// 
  "minecraft:fox",// 
  "minecraft:horse",// 
  "minecraft:husk",// 
  "minecraft:llama",// 
  "minecraft:mooshroom",// 
  "minecraft:pig",// 
  "minecraft:player",// 
  "minecraft:polar_bear",// 
  "minecraft:sheep",// 
  "minecraft:villager",// 
  "minecraft:villager_v2",// 
  "minecraft:wolf",// 
  "minecraft:zombie_horse",// 
  "minecraft:zombie",// 
  "minecraft:zombie_villager",// 
  "minecraft:zombie_pigman"// 
];

export function qkaj(normal, remplazo) {
  const variant = normal.getComponent("variant")?.value ?? 0;
  const variants = ["pale", "ashen", "black", "chestnut", "rusty", "snowy", "spotted", "striped", "woods"];
  if (variant >= 0 && variant < variants.length) {
    remplazo.triggerEvent(`minecraft:spawn_wild_${variants[variant]}`);
  }
}

export function v1(normal, remplazo) {
  const variant = normal.getProperty("minecraft:climate_variant");
  remplazo.setProperty("minecraft:climate_variant", `${variant}`);
}

export function dispararP(source, proyectil, num) {
  const view = source.getViewDirection();
  const sp = source.dimension.spawnEntity(proyectil, source.getHeadLocation());
  const component = sp.getComponent('minecraft:projectile');
  component.owner = source;
  component.shoot({x: view.x*num, y: view.y*num, z: view.z*num});
}

export function spawnEnt(entidad, wo) {
  if (!list.includes(entidad.typeId)) return;
  const rot = entidad.getRotation();
  const loc = { x: entidad.location.x, y: entidad.location.y, z: entidad.location.z };
  entidad.dimension.spawnParticle('parasitesap:parasite', loc);
  const zkdj = world.getDimension(entidad.dimension.id).id;
  world.scoreboard.getObjective("parasitephasegs").addScore(`parasitesaphase_${zkdj}`, 6);
  if (entidad.typeId == 'minecraft:bat') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_bat`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_bat`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:cow') {
    if (wo === undefined) {
      let oo = entidad.dimension.spawnEntity(`parasitesap:infected_cow`, loc);
      oo.setRotation({ x: rot.x, y: rot.y });
      v1(entidad, oo);
    }
    if (wo !== undefined) {
      let oo = entidad.dimension.spawnEntity(`parasitesap:mut_inf_cow`, loc);
      oo.setRotation({ x: rot.x, y: rot.y });
      v1(entidad, oo);
    }
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:creeper') {
    entidad.dimension.spawnEntity(`parasitesap:infected_creeper`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:dolphin') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_dolphin`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_dolphin`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:ender_dragon') {
    entidad.dimension.spawnEntity(`parasitesap:infected_ender_dragon`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:enderman') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_enderman`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_enderman`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:fox') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_fox`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_fox`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:horse' || entidad.typeId == 'minecraft:zombie_horse') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_horse`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_horse`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:husk') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_husk`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_husk`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:llama') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_llama`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_llama`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:mooshroom') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_mooshroom`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_mooshroom`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:pig') {
    if (wo === undefined) {
      let oo = entidad.dimension.spawnEntity(`parasitesap:infected_pig`, loc);
      oo.setRotation({ x: rot.x, y: rot.y });
      v1(entidad, oo);
    }
    if (wo !== undefined) {
      let oo = entidad.dimension.spawnEntity(`parasitesap:mut_inf_pig`, loc);
      oo.setRotation({ x: rot.x, y: rot.y });
      v1(entidad, oo);
    }
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:player') {
    if (wo === undefined) {
      const pla = entidad.dimension.spawnEntity(`parasitesap:infected_human`, loc);
    }
    if (wo !== undefined) {
      const pla = entidad.dimension.spawnEntity(`parasitesap:mut_inf_human`, loc);
    }
    pla.nameTag = `§r${entidad.nameTag}`
    pla.setRotation({ x: rot.x, y: rot.y });
    entidad.removeTag('parasite');
  }
  if (entidad.typeId == 'minecraft:polar_bear') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_bear`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_bear`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:sheep') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_sheep`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_sheep`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:villager' || entidad.typeId == 'minecraft:villager_v2' || entidad.typeId == 'minecraft:zombie_villager') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_villager`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_villager`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:wolf') {
    if (wo === undefined) {
      let oo = entidad.dimension.spawnEntity(`parasitesap:infected_wolf`, loc);
      oo.setRotation({ x: rot.x, y: rot.y });
      qkaj(entidad, oo);
    }
    if (wo !== undefined) {
      let oo = entidad.dimension.spawnEntity(`parasitesap:mut_inf_wolf`, loc);
      oo.setRotation({ x: rot.x, y: rot.y });
      qkaj(entidad, oo);
    }
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:zombie') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_zombie`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_zombie`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
  if (entidad.typeId == 'minecraft:zombie_pigman') {
    if (wo === undefined) entidad.dimension.spawnEntity(`parasitesap:infected_zombified_piglin`, loc).setRotation({ x: rot.x, y: rot.y });
    if (wo !== undefined) entidad.dimension.spawnEntity(`parasitesap:mut_inf_zombified_piglin`, loc).setRotation({ x: rot.x, y: rot.y });
    system.run(()=>{
      entidad.remove();
    });
  }
}

export function lc(om) {
  if (list.includes(om.typeId)) return;
  const rot = om.getRotation();
  om.dimension.spawnEntity("parasitesap:lc", {
    x: om.location.x,
    y: om.location.y,
    z: om.location.z
  }).setRotation({ x: rot.x, y: rot.y });
  om.dimension.spawnParticle("parasitesap:parasite", {
    x: om.location.x,
    y: om.location.y,
    z: om.location.z
  });
  system.run(()=>{
    om.remove();
  })
}

export function durabilidad(jugador, objeto, valor) {
  const item = objeto?.getComponent("durability");
  const lvl = objeto?.getComponent('enchantable').getEnchantment('unbreaking')?.level ?? 0;
  const breakingP = 100 / (lvl + 1);
  const breakingR = (Math.random() * 100);
  if (breakingP < breakingR) return;
  item.damage += valor;
  const ar = jugador.getComponent('inventory').container
  if (item.damage < item.maxDurability) {
    ar.setItem(jugador.selectedSlotIndex, objeto);
  }
  else {
    jugador.playSound('random.break', jugador.location);
    ar.setItem(jugador.selectedSlotIndex, new ItemStack('minecraft:air'));
  }
}

function getEntFromVi(source, distancia) {
  const sijs = source.getEntitiesFromViewDirection({maxDistance: distancia});
  for (const ao of sijs) {
    const b = ao.entity;
    return b;
  }
}

/*system.runInterval(()=>{
  for (const p of world.getAllPlayers()) {
    const inv = p.getComponent('inventory').container;
    for (let i = 0; i < inv.size; i ++) {
      const itemD = inv.getItem(i);
      const durability = itemD?.getComponent('durability') ?? null;
      if (durability !== null) {
        itemD?.setLore([`§7${durability.maxDurability - durability.damage}/${durability.maxDurability}`]);
        if (durability.damage < durability.maxDurability) {
          durability.damage ++;
          inv.setItem(i,itemD);
        }
        else return inv.setItem(i, new ItemStack('minecraft:air'));
      }
    }
  }
});*/