import * as mc from "@minecraft/server"

mc.world.afterEvents.playerSpawn.subscribe((event) => {
  mc.world.gameRules.commandBlockOutput = false;
});

// /scriptevent cyd_dino:watch_tower_draxar
function greenWatchTowerDraxar(block) {
  block.dimension
    .spawnEntity("cyd_dino:gaiakin", {
      x: block.location.x - 2,
      y: block.location.y,
      z: block.location.z - 2,
    })
    .triggerEvent("spawn_adult_melee");
  block.dimension
    .spawnEntity("cyd_dino:gaiakin", {
      x: block.location.x + 2,
      y: block.location.y,
      z: block.location.z + 2,
    })
    .triggerEvent("spawn_adult_melee");
  block.dimension
    .spawnEntity("cyd_dino:gaiakin", {
      x: block.location.x - 2,
      y: block.location.y,
      z: block.location.z + 2,
    })
    .triggerEvent("spawn_adult_range");
  block.dimension.spawnEntity("cyd_dino:pachy", {
    x: block.location.x + 7,
    y: block.location.y - 15,
    z: block.location.z + 7,
  });
  block?.setType("minecraft:air");
}

function spawnNpc(block, melee, faction) {
  if (melee) {
    block.dimension
      .spawnEntity(`cyd_dino:${faction}`, {
        x: block.location.x,
        y: block.location.y + 2,
        z: block.location.z,
      })
      .triggerEvent("spawn_adult_melee");
  }
  else {
    block.dimension
      .spawnEntity(`cyd_dino:${faction}`, {
        x: block.location.x,
        y: block.location.y + 2,
        z: block.location.z,
      })
      .triggerEvent("spawn_adult_range");
  }
}

function spawnDino(block, dinoType, withArmor) {
  const dino = block.dimension
    .spawnEntity(`cyd_dino:${dinoType}`, {
      x: block.location.x,
      y: block.location.y + 2,
      z: block.location.z,
    })
  if (withArmor) {
    dino.triggerEvent("cyd_dino:armor_upgrade");
  }
}

function spawnMerchant(block, merchantColor) {
  block.dimension
    .spawnEntity(`cyd_dino:${merchantColor}_merchant`, {
      x: block.location.x,
      y: block.location.y + 2,
      z: block.location.z,
    })
}

function spawnBoss(block) {
  const bossList = ["carnankylos", "empy", "mega", "stegospheryx"];
  const bossType = bossList[Math.floor(Math.random() * bossList.length)];
  block.dimension
    .spawnEntity(`cyd_dino:${bossType}`, {
      x: block.location.x + 32,
      y: block.location.y + 2,
      z: block.location.z,
    })
  block?.setType("minecraft:air");
}

mc.system.afterEvents.scriptEventReceive.subscribe(async (event) => {
  try {
    const block = event.sourceBlock;
    const player = block?.dimension.getPlayers({ location: block.location, maxDistance: 64 })[0];
    // green_small 50 chance
    if (event.id === "cyd_dino:green_small") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, false, "gaiakin");
      spawnNpc(block, false, "gaiakin");
      spawnNpc(block, true, "gaiakin");
      if (Math.random() <= 0.5) {
        spawnDino(block, "pachy");
      }
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:red_small") {
      if (!player || !player.isValid()) return;
      block.dimension
        .spawnEntity(`cyd_dino:draxer`, {
          x: block.location.x,
          y: block.location.y - 2,
          z: block.location.z,
        })
        .triggerEvent("spawn_adult_melee");
      block.dimension
        .spawnEntity(`cyd_dino:draxer`, {
          x: block.location.x,
          y: block.location.y - 2,
          z: block.location.z,
        })
        .triggerEvent("spawn_adult_melee");
      block.dimension
        .spawnEntity(`cyd_dino:draxer`, {
          x: block.location.x,
          y: block.location.y - 2,
          z: block.location.z,
        })
        .triggerEvent("spawn_adult_range");
      if (Math.random() <= 0.5) {
        block.dimension
          .spawnEntity(`cyd_dino:velo`, {
            x: block.location.x + 6,
            y: block.location.y - 6,
            z: block.location.z + 6,
          })
      }
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:green_tower") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, false, "gaiakin");
      spawnNpc(block, false, "gaiakin");
      spawnNpc(block, true, "gaiakin");
      spawnDino(block, "pachy");
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:red_tower") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, false, "draxer");
      spawnNpc(block, false, "draxer");
      spawnNpc(block, true, "draxer");
      spawnDino(block, "velo");
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:g_05") {
      if (!player || !player.isValid()) return;
      spawnMerchant(block, "green");
      spawnMerchant(block, "green");
      spawnDino(block, "brachio");
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:g_04") {
      if (!player || !player.isValid()) return;
      spawnMerchant(block, "green");
      spawnMerchant(block, "green");
      spawnDino(block, "brachio");
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:g_03") { // warrior camp
      if (!player || !player.isValid()) return;
      spawnMerchant(block, "green");
      spawnMerchant(block, "green");
      spawnNpc(block, true, "gaiakin");
      spawnDino(block, 'brachio', true)
      spawnDino(block, 'micro', true)
      block?.setType("minecraft:air")
    }
    if (event.id === "cyd_dino:g_02") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, false, "gaiakin");
      spawnNpc(block, false, "gaiakin");
      spawnNpc(block, true, "gaiakin");
      spawnNpc(block, true, "gaiakin");
      spawnNpc(block, true, "gaiakin");
      spawnDino(block, "trice");
      spawnDino(block, "trice");
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:g_01") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, false, "gaiakin");
      spawnNpc(block, false, "gaiakin");
      spawnNpc(block, true, "gaiakin");
      spawnNpc(block, true, "gaiakin");
      spawnNpc(block, true, "gaiakin");
      spawnDino(block, "iguan");
      spawnDino(block, "iguan");
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:r_05_boss") {
    }
    if (event.id === "cyd_dino:r_05") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, false, "draxer");
      spawnNpc(block, false, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnBoss(block);
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:r_04") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnDino(block, "sinotyrannus", true);
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:r_03") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnDino(block, "allosaurus", true);
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:r_02") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, false, "draxer");
      spawnNpc(block, false, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnDino(block, "utah");
      spawnDino(block, "utah", true);
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:r_01") {
      if (!player || !player.isValid()) return;
      spawnNpc(block, false, "draxer");
      spawnNpc(block, false, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnNpc(block, true, "draxer");
      spawnDino(block, "carno");
      spawnDino(block, "carno", true);
      block?.setType("minecraft:air");
    }
    if (event.id === "cyd_dino:spawn_volcano_boss") {
      if (!player || !player.isValid()) return;
      if (Math.random() <= 0.2) {
        block.dimension.spawnEntity(`cyd_dino:carnankylos`, {
          x: block.location.x + 16,
          y: block.location.y + 4,
          z: block.location.z + 16,
        })
      }
      block?.setType("minecraft:air");
    }
  } catch (error) {
    return
  }
});

