// priority:950
// Written by EnigmaQuip as a post almost unified recipe generation script for missing recipes

ServerEvents.recipes(event => {
  if (global.devLogging) {
    console.log('Finishing Unifying on Dusts')
  }

  let dustCount = {
    occult: 0,
    create: 0,
    enderio: 0
  }

  global.auTags.dusts.forEach(material => {
    let dust = global.itemFromTag('dusts', material)
    if (dust.isEmpty()) {
      console.log(`${material} does not have a dust tag entry`)
      return
    }

    let ingotTag = Ingredient.of(`#forge:ingots/${material}`)
    let oreTag = Ingredient.of(`#forge:ores/${material}`)
    let rawTag = Ingredient.of(`#forge:raw_materials/${material}`)
    let gemTag = Ingredient.of(`#forge:gems/${material}`)

    // Occultism Crusher
    if (global.loaded.Occult_Loaded) {
      let crush = {
        ingot: false,
        ore: false,
        raw: false,
        gem: false
      }
      event.forEachRecipe({ type: "occultism:crushing" }, recipe => {
        let recipeJson = recipe.json
        if (dust.equalsIgnoringCount(Item.of(recipeJson.get('result')))) {
          let input = recipeJson.get('ingredient')
          if (ingotTag.test(Ingredient.of(input))) {
            crush.ingot = true
          } else if (oreTag.test(Ingredient.of(input))) {
            crush.ore = true
          } else if (rawTag.test(Ingredient.of(input))) {
            crush.raw = true
          } else if (gemTag.test(Ingredient.of(input))) {
            crush.gem = true
          }
        }
      })
      let recipe = {
        type: "occultism:crushing",
        ingredient: {},
        result: {},
        crushing_time: 200,
        ignore_crushing_multiplier: true
      }
      if (!ingotTag.getFirst().isEmpty() && !crush.ingot) {
        let ingotRecipe = recipe
        ingotRecipe.ingredient = ingotTag.toJson()
        ingotRecipe.result = dust.withCount(1).toJson()
        event.custom(ingotRecipe).id(`kubejs:occultism/crushing/${material}_dust_from_ingot`)
        dustCount.occult++
      }
      if (!rawTag.getFirst().isEmpty() && !crush.raw) {
        let rawRecipe = recipe
        rawRecipe.ingredient = rawTag.toJson()
        rawRecipe.result = dust.withCount(1).toJson()
        rawRecipe.ignore_crushing_multiplier = false
        event.custom(rawRecipe).id(`kubejs:occultism/crushing/${material}_dust_from_raw_material`)
        dustCount.occult++
      }
      if (!oreTag.getFirst().isEmpty() && !crush.ore) {
        let oreRecipe = recipe
        oreRecipe.ingredient = oreTag.toJson()
        oreRecipe.result = dust.withCount(2).toJson()
        oreRecipe.crushing_time = 300
        oreRecipe.ignore_crushing_multiplier = false
        event.custom(oreRecipe).id(`kubejs:occultism/crushing/${material}_dust`)
        dustCount.occult++
      }
      if (!gemTag.getFirst().isEmpty() && !crush.gem) {
        let gemRecipe = recipe
        gemRecipe.ingredient = gemTag.toJson()
        gemRecipe.result = dust.withCount(1).toJson()
        gemRecipe.ignore_crushing_multiplier = false
        event.custom(gemRecipe).id(`kubejs:occultism/crushing/${material}_dust_from_gem`)
        dustCount.occult++
      }
    }

    // Create Crusher
    if (global.loaded.Create_Loaded) {
      let crush = {
        ingot: false,
        ore: false,
        raw: false,
        gem: false
      }
      event.forEachRecipe({ type: "create:crushing" }, recipe => {
        let recipeJson = recipe.json
        if (dust.equalsIgnoringCount(Item.of(recipeJson.get('results')))) {
          let input = recipeJson.get('ingredients')
          if (ingotTag.test(Ingredient.of(input))) {
            crush.ingot = true
          } else if (oreTag.test(Ingredient.of(input))) {
            crush.ore = true
          } else if (rawTag.test(Ingredient.of(input))) {
            crush.raw = true
          } else if (gemTag.test(Ingredient.of(input))) {
            crush.gem = true
          }
        }
      })
      let recipe = {
        type: "create:crushing",
        ingredients: {},
        results: {},
        processingTime: 150
      }
      if (!ingotTag.getFirst().isEmpty() && !crush.ingot) {
        let ingotRecipe = recipe
        ingotRecipe.ingredients = ingotTag.toJson()
        ingotRecipe.results = [{ tag: `forge:dusts/${material}`, count: 1 }],
        event.custom(ingotRecipe).id(`kubejs:create/crushing/${material}_dust_from_ingot`)
        dustCount.create++
      }
      if (!rawTag.getFirst().isEmpty() && !crush.raw) {
        let rawRecipe = recipe
        rawRecipe.ingredients = rawTag.toJson()
        rawRecipe.results = [{ tag: `forge:dusts/${material}`, count: 1 }],
        event.custom(rawRecipe).id(`kubejs:create/crushing/${material}_dust_from_raw_material`)
        dustCount.create++
      }
      if (!oreTag.getFirst().isEmpty() && !crush.ore) {
        let oreRecipe = recipe
        oreRecipe.ingredients = oreTag.toJson()
        oreRecipe.results = [{ tag: `forge:dusts/${material}`, count: 2 }],
        oreRecipe.processingTime = 300
        event.custom(oreRecipe).id(`kubejs:create/crushing/${material}_dust`)
        dustCount.create++
      }
      if (!gemTag.getFirst().isEmpty() && !crush.gem) {
        let gemRecipe = recipe
        gemRecipe.ingredients = gemTag.toJson()
        gemRecipe.results = [{ tag: `forge:dusts/${material}`, count: 1 }],
        event.custom(gemRecipe).id(`kubejs:create/crushing/${material}_dust_from_gem`)
        dustCount.create++
      }
    }

  //Sag Mill
  if (global.loaded.EnderIO_Loaded) {
    let crush = {
      ingot: false,
      ore: false,
      raw: false,
      gem: false
    }
    event.forEachRecipe({ type: "enderio:sag_milling" }, recipe => {
      let recipeJson = recipe.json
      if (dust.equalsIgnoringCount(Item.of(recipeJson.get('outputs')))) {
        let input = recipeJson.get('input')
        if (ingotTag.test(Ingredient.of(input))) {
          crush.ingot = true
        } else if (oreTag.test(Ingredient.of(input))) {
          crush.ore = true
        } else if (rawTag.test(Ingredient.of(input))) {
          crush.raw = true
        } else if (gemTag.test(Ingredient.of(input))) {
          crush.gem = true
        }
      }
    })
    let recipe = {
      type: "enderio:sag_milling",
      energy: 2500,
      input: {},
      outputs: {}
    }
    if (!ingotTag.getFirst().isEmpty() && !crush.ingot) {
      let ingotRecipe = recipe
      ingotRecipe.input = ingotTag.toJson()
      ingotRecipe.outputs = [{ tag: `forge:dusts/${material}`, count: 1 }],
      event.custom(ingotRecipe).id(`kubejs:enderio/sag_milling/${material}_dust_from_ingot`)
      dustCount.enderio++
    }
    if (!rawTag.getFirst().isEmpty() && !crush.raw) {
      let rawRecipe = recipe
      rawRecipe.input = rawTag.toJson()
      rawRecipe.outputs = [{ tag: `forge:dusts/${material}`, count: 1 }],
      event.custom(rawRecipe).id(`kubejs:enderio/sag_milling/${material}_dust_from_raw_material`)
      dustCount.enderio++
    }
    if (!oreTag.getFirst().isEmpty() && !crush.ore) {
      let oreRecipe = recipe
      oreRecipe.input = oreTag.toJson()
      oreRecipe.outputs = [{ tag: `forge:dusts/${material}`, count: 2 }],
      oreRecipe.processingTime = 300
      event.custom(oreRecipe).id(`kubejs:enderio/sag_milling/${material}_dust`)
      dustCount.enderio++
    }
    if (!gemTag.getFirst().isEmpty() && !crush.gem) {
      let gemRecipe = recipe
      gemRecipe.input = gemTag.toJson()
      gemRecipe.outputs = [{ tag: `forge:dusts/${material}`, count: 1 }],
      event.custom(gemRecipe).id(`kubejs:enderio/sag_milling/${material}_dust_from_gem`)
      dustCount.enderio++
    }
  }
})

  if (global.devLogging) {
    console.log(`Added Dust Recipes - Create: ${dustCount.create}, Occultism: ${dustCount.occult}, EnderIO: ${dustCount.enderio}`)
    // Added Dust Recipes - FTBIC: 60, Occultism: 5
  }
})
