//priority:400
// 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 Gears')
  }
  let gearCount = {
    ie: 0,
    thermal: 0
  }
  global.auTags.gears.forEach(material => {
    let gear = global.itemFromTag('gears', material)
    if (gear.isEmpty()) {
      console.log(`${material} does not have a gear tag entry`)
      return
    }
    
    let ingotTag = Ingredient.of(`#forge:ingots/${material}`)
    let gemTag = Ingredient.of(`#forge:gems/${material}`)
    if (!gemTag.getFirst().isEmpty()) {

      if (global.loaded.Thermal_Loaded) {
        // Check if thermal multiservo press recipe exists and add it if not
        let count = event.recipeStream({ type: 'thermal:press' }).mapToInt(recipe => {
          let hasMatch = false
          recipe.json.get('result').forEach(item => {
            if (gear.specialEquals(Item.of(item), true)) {
              hasMatch = true
            }
          })
          if (hasMatch) {
             return 1 
          }
          return 0
        }).sum()

        if (count == 0) {
          event.custom({
            type: 'thermal:press',
            ingredients: [{ tag: `forge:gems/${material}`, count: 4 }, { item: 'thermal:press_gear_die' }],
            result: [gear.toJson()],
            energy: 2400,
          }).id(`kubejs:thermal/machines/press/press_${material}_ingot_to_gear`)
          gearCount.thermal++
        }

      }
    }

    if (!ingotTag.getFirst().isEmpty()) {

      if (global.loaded.Thermal_Loaded) {
        // Check if thermal multiservo press recipe exists and add it if not
        let count = event.recipeStream({ type: 'thermal:press' }).mapToInt(recipe => {
          let hasMatch = false
          recipe.json.get('result').forEach(item => {
            if (gear.specialEquals(Item.of(item), true)) {
              hasMatch = true
            }
          })
          if (hasMatch) {
             return 1 
          }
          return 0
        }).sum()

        if (count == 0) {
          event.custom({
            type: 'thermal:press',
            ingredients: [{ tag: `forge:ingots/${material}`, count: 4 }, { item: 'thermal:press_gear_die' }],
            result: [gear.toJson()],
            energy: 2400,
          }).id(`kubejs:thermal/machines/press/press_${material}_ingot_to_gear`)
          gearCount.thermal++
        }

      }
    }
  })

  if (global.devLogging) {
    console.log(`Added Gear Recipes - FTBIC: ${gearCount.ftbic}, IE: ${gearCount.ie}, Thermal: ${gearCount.thermal}`)
    // Added Gear Recipes - FTBIC: 22, IE: 17, Thermal: 15
  }
})