const Tiers = {
  String: 0,
  Andesite: 1,
  Zinc: 2,
  Brass: 3,
};

// BLACKSLIST ITEMS
const exclusionBlacklist = ["minecraft:iron_nugget", "mekanism:shard_copper", "mekanism:shard_iron"];
var gravel = "minecraft:gravel";
var sand = "minecraft:sand";
var grass = "minecraft:grass";
var dirt = "minecraft:dirt";
var shroom = "minecraft:mycelium";
var soul = "minecraft:soul_sand";
var leaves = "minecraft:oak_leaves";

//[['input','output','chance','mintier', true/false waterlogged]]
const siftingRecipes = [
  [dirt, "farmersdelight:cabbage_seeds", 0.1, 1, false],
  [dirt, "farmersdelight:tomato_seeds", 0.1, 1, false],
  [dirt, "minecraft:beetroot_seeds", 0.35, 1, false],
  [dirt, "minecraft:sweet_berries", 0.2, 1, false],
  [sand, "ae2:certus_quartz_dust", 0.05, 2, false],
  [sand, "minecraft:bone", 0.2, 1, false],
  [soul, "minecraft:blaze_powder", 0.1, 2, false],
  [sand, "minecraft:bone_meal", 0.2, 0, false],
  [sand, "minecraft:glowstone_dust", 0.1, 1, false],
  [sand, "minecraft:gunpowder", 0.05, 2, false],
  [sand, "minecraft:redstone", 0.3, 2, false],
  [shroom, "minecraft:ender_pearl", 0.05, 2, false],
  [grass, "minecraft:red_tulip", 0.3, 0, false],
  [grass, "minecraft:oxeye_daisy", 0.3, 0, false],
  [grass, "minecraft:cornflower", 0.3, 0, false],
  [grass, "minecraft:lily_of_the_valley", 0.3, 0, false],
  [grass, "minecraft:allium", 0.3, 0, false],
  [grass, "minecraft:azure_bluet", 0.3, 0, false],
  [grass, "minecraft:blue_orchid", 0.3, 0, false],
  [grass, "minecraft:poppy", 0.3, 0, false],
  [grass, "minecraft:dandelion", 0.3, 0, false],
  [grass, "minecraft:orange_tulip", 0.3, 0, false],
  [grass, "minecraft:white_tulip", 0.3, 0, false],
  [grass, "minecraft:pink_tulip", 0.3, 0, false],
  [gravel, "alltheores:raw_zinc", 0.3, 2, false],
  [gravel, "minecraft:coal", 0.3, 1, false],
  [gravel, "create_new_age:thorium", 0.1, 1, false],
  [gravel, "minecraft:diamond", 0.02, 3, false],
  [gravel, "minecraft:emerald", 0.01, 3, false],
  [gravel, "minecraft:lapis_lazuli", 0.3, 1, false],
  [gravel, "minecraft:raw_copper", 0.4, 2, false],
  [gravel, "minecraft:raw_gold", 0.15, 2, false],
  [gravel, "minecraft:raw_iron", 0.25, 2, false],
  [gravel, "alltheores:raw_lead", 0.2, 2, false],
  [gravel, "alltheores:raw_nickel", 0.2, 2, false],
  [gravel, "alltheores:raw_silver", 0.15, 3, false],
  [gravel, "alltheores:raw_platinum", 0.1, 3, false],
  [gravel, "alltheores:raw_uranium", 0.05, 3, false],
  [gravel, "createnuclear:uranium_powder", 0.1, 3, false],
  [sand, "thermal:apatite", 0.2, 0, false],
  [sand, "thermal:cinnabar", 0.2, 2, false],
  [sand, "thermal:sulfur", 0.2, 2, false],
  [gravel, "alltheores:raw_tin", 0.3, 2, false],
  [gravel, "alltheores:raw_aluminum", 0.3, 2, false],
  [leaves, "minecraft:apple", 0.2, 1, false],
  [leaves, "minecraft:golden_apple", 0.01, 2, false],
  [leaves, "minecraft:dark_oak_sapling", 0.05, 0, false],
  [leaves, "minecraft:oak_sapling", 0.15, 0, false],
  [leaves, "minecraft:spruce_sapling", 0.1, 0, false],
  [leaves, "minecraft:jungle_sapling", 0.1, 0, false],
  [leaves, "minecraft:acacia_sapling", 0.1, 0, false],
  [leaves, "minecraft:birch_sapling", 0.15, 0, false],
  [leaves, "minecraft:cherry_sapling", 0.05, 0, false],
  [leaves, "integrateddynamics:menril_sapling", 0.02, 0, false],
  [leaves, "minecraft:flowering_azalea", 0.15, 1, false],
  [leaves, "minecraft:azalea", 0.15, 1, false],
  [leaves, "minecraft:spore_blossom", 0.15, 1, false],
  [soul, "create:cinder_flour", 0.2, 1, false],
  [soul, "minecraft:netherite_scrap", 0.008, 3, false],
  [sand, "minecraft:cocoa_beans", 0.1, 0, false],
  [sand, "minecraft:bamboo", 0.05, 1, false],
  [sand, "minecraft:cactus", 0.1, 1, false],
  [sand, "minecraft:sugar_cane", 0.3, 0, false],
  [sand, "thermal:quartz_dust", 0.1, 1, false],
  [sand, "minecraft:slime_ball", 0.1, 1, true],
  [sand, "minecraft:clay_ball", 0.5, 0, true],
  [sand, "minecraft:kelp", 0.2, 0, true],
  [sand, "minecraft:prismarine_crystals", 0.02, 2, true],
  [sand, "minecraft:prismarine_shard", 0.1, 2, true],
  [sand, "minecraft:sea_pickle", 0.1, 1, true],
  [sand, "minecraft:seagrass", 0.15, 0, true],
  [shroom, "minecraft:brown_mushroom", 0.4, 1, false],
  [shroom, "minecraft:red_mushroom", 0.4, 1, false],
  [soul, "minecraft:ghast_tear", 0.02, 3, false],
  [soul, "minecraft:nether_wart", 0.1, 1, false],
  [soul, "minecraft:quartz", 0.6, 1, false],
  [soul, "enderitemod:enderite_ingot", 0.004, 3, false],
];

ServerEvents.recipes((event) => {
  let mesh;
  // var xxxRecipes = [[String], [Andesite], [Zinc], [Brass]]
  var dirtRecipes = [[], [], [], []];
  var dustRecipes = [[], [], [], []];
  var endRecipes = [[], [], [], []];
  var gravelRecipes = [[], [], [], []];
  var grassRecipes = [[], [], [], []];
  var leavesRecipes = [[], [], [], []];
  var netherRecipes = [[], [], [], []];
  var sandRecipes = [[], [], [], []];
  var shroomRecipes = [[], [], [], []];
  var soulRecipes = [[], [], [], []];
  var waterDirtRecipes = [[], [], [], []];
  var waterEndRecipes = [[], [], [], []];
  var waterGravelRecipes = [[], [], [], []];
  var waterGrassRecipes = [[], [], [], []];
  var waterLeavesRecipes = [[], [], [], []];
  var waterSandRecipes = [[], [], [], []];
  var waterShroomRecipes = [[], [], [], []];
  var waterSoulRecipes = [[], [], [], []];
  siftingRecipes.forEach((recipeMap) => {
    let input = recipeMap[0];
    let output = recipeMap[1];
    let chance = recipeMap[2];
    let tier = recipeMap[3];
    let waterlogged = recipeMap[4];

    let json = Item.of(output).withChance(chance);
    switch (input) {
      case dirt: {
        if (waterlogged) waterDirtRecipes[`${tier}`].push(json);
        else dirtRecipes[`${tier}`].push(json);
        break;
      }
      case gravel: {
        if (waterlogged) waterGravelRecipes[`${tier}`].push(json);
        else gravelRecipes[`${tier}`].push(json);
        break;
      }
      case grass: {
        if (waterlogged) waterGrassRecipes[`${tier}`].push(json);
        else grassRecipes[`${tier}`].push(json);
        break;
      }
      case leaves: {
        if (waterlogged) waterLeavesRecipes[`${tier}`].push(json);
        else leavesRecipes[`${tier}`].push(json);
        break;
      }
      case sand: {
        if (waterlogged) waterSandRecipes[`${tier}`].push(json);
        else sandRecipes[`${tier}`].push(json);
        break;
      }
      case shroom: {
        if (waterlogged) waterShroomRecipes[`${tier}`].push(json);
        else shroomRecipes[`${tier}`].push(json);
        break;
      }
      case soul: {
        if (waterlogged) waterSoulRecipes[`${tier}`].push(json);
        else soulRecipes[`${tier}`].push(json);
        break;
      }
    }
  });

  function getRecipes(material, recipesArray, waterlogged) {
    for (let i = 0; i < recipesArray.length; i++) {
      let speed = 0;
      switch (i) {
        case 0:
          mesh = "createsifter:string_mesh";
          break;
        case 1: {
          mesh = "createsifter:andesite_mesh";
          speed = 0;
          recipesArray[i - 1].forEach((recipe) => {
            if (!checkExclusion(recipe)) recipesArray[i].push(recipe);
          });
          break;
        }
        case 2: {
          mesh = "createsifter:zinc_mesh";
          speed = 0;
          recipesArray[i - 1].forEach((recipe) => {
            if (!checkExclusion(recipe)) recipesArray[i].push(recipe);
          });
          break;
        }
        case 3: {
          mesh = "createsifter:brass_mesh";
          speed = 0;
          recipesArray[i - 1].forEach((recipe) => {
            if (!checkExclusion(recipe)) recipesArray[i].push(recipe);
          });
          break;
        }

      }

      //console.log(`Mesh: ${mesh.split(':')[1]} | Material: ${material.split(':')[1]} | Waterlogged: ${waterlogged} | recipes: ${recipesArray[i]} `)
      if (recipesArray[i].length !== 0) {
        if (waterlogged)
          event.recipes
            .createsifterSifting(recipesArray[i], [material, mesh])
            .minimumSpeed(speed)
            .waterlogged()
            .id(`ftbskies:${material.split(":")[1]}_${mesh.split(":")[1]}_waterlogged`);
        else
          event.recipes
            .createsifterSifting(recipesArray[i], [material, mesh])
            .minimumSpeed(speed)
            .id(`ftbskies:${material.split(":")[1]}_${mesh.split(":")[1]}`);
      }
    }
  }

  getRecipes(dirt, dirtRecipes, false);
  getRecipes(grass, grassRecipes, false);
  getRecipes(gravel, gravelRecipes, false);
  getRecipes(leaves, leavesRecipes, false);
  getRecipes(sand, sandRecipes, false);
  getRecipes(shroom, shroomRecipes, false);
  getRecipes(soul, soulRecipes, false);
  getRecipes(dirt, waterDirtRecipes, true);
  getRecipes(grass, waterGrassRecipes, true);
  getRecipes(gravel, waterGravelRecipes, true);
  getRecipes(leaves, waterLeavesRecipes, true);
  getRecipes(sand, waterSandRecipes, true);
  getRecipes(shroom, waterShroomRecipes, true);
  getRecipes(soul, waterSoulRecipes, true);
});

function checkExclusion(recipe) {
  if (exclusionBlacklist.includes(recipe.item.id)) {
    return true;
  }
  return false;
}
