KeyBindEvents.modify(event => {
  let MaterialManager = Java.loadClass("net.yiran.extraholopage.api.MaterialManager").INSTANCE
  let $ForgeRegistries = Java.loadClass('net.minecraftforge.registries.ForgeRegistries')


  MaterialManager.registerFilter(m => {
    return true
  }, "按下键盘上的+或-键即可翻页", 998)

  MaterialManager.registerSorter(m => {
    return 0.0
  }, "按下键盘上的+或-键即可翻页", 998)





  //filter 过滤器
  //* 阶段

  // MaterialManager.sorters.removeIf(1)
  MaterialManager.registerFilter(m => {
    let result = false
    m.material.getApplicableItemStacks().forEach(s => {
      if (s.hasTag("dzzl:stage1")) {
        result = true
      }
    })
    return result
  }, "一阶段材料")
  MaterialManager.registerFilter(m => {
    let result = false
    m.material.getApplicableItemStacks().forEach(s => {
      if (s.hasTag("dzzl:stage2")) {
        result = true
      }
    })
    return result
  }, "二阶段材料")

  MaterialManager.registerFilter(m => {
    let result = false
    m.material.getApplicableItemStacks().forEach(s => {
      if (s.hasTag("dzzl:stage3")) {
        result = true
      }
    })
    return result
  }, "三阶段材料")

  //* 带xx属性
  // MaterialManager.registerFilter(m => {
  //   let result = false
  //   if (m.attributes != null && m.attributes.containsKey($ForgeRegistries.ATTRIBUTES.getValue('kubejs:fire_damage'))) {
  //     result = true
  //   }
  //   return result
  // }, "§6带火焰属性的材料")
  // MaterialManager.registerFilter(m => {
  //   let result = false
  //   if (m.attributes != null && m.attributes.containsKey($ForgeRegistries.ATTRIBUTES.getValue('kubejs:pestilence'))) {
  //     result = true
  //   }
  //   return result
  // }, "§a带瘟疫属性的材料")
  MaterialManager.registerFilter(m => {
    let result = false
    if (m.attributes != null && m.attributes.containsKey($ForgeRegistries.ATTRIBUTES.getValue('minecraft:generic.attack_damage'))) {
      result = true
    }
    return result
  }, "带攻击力属性的材料")
  MaterialManager.registerFilter(m => {
    let result = false
    if (m.attributes != null && m.attributes.containsKey($ForgeRegistries.ATTRIBUTES.getValue('minecraft:generic.attack_speed'))) {
      result = true
    }
    return result
  }, "带攻击速度属性的材料")

  // MaterialManager.registerFilter(m => {
  //   return m.effects.getLevel($ItemEffect.get('ocean_heart')) > 0
  // }, "带海洋之心")

  let fire = [
    "soul_form",
    "hellstone_ingot",
    "thunder_fire_overload",
    "nuclear_furnace_component",
    "upgrade_templates/ignitium_ingot",
    "invert",
    "subterranodon",
    "burn",
    "attribute_fire",
    "fire_increase",
    "fire_breath",
    "art_of_forging:flame_protection",
    "art_of_forging:soul_charged",
    "insulation",
    "twilight",
    "upgrade_templates/netherite_ingot",
    "fire_dust",
    "moltengold_ingot",
    "cooked_netherite_monstrosity_meat",
    "ignitium_ingot",
    "lava_eaters_skin",
    "blaze_rod",
    "magma_cream",
    "netherite_block"
  ]

  let lighting = [
    "damp",
    "telecore",
    "south_pole",
    "north_pole",
    "thunder",
    "essence_of_the_storm",
    "thunderous_wrath",
    "call_thunder",
    "lei_ling",
    "lightning_spell"
  ]
  let pestilence = [
    "waste",
    "nuclear_bomb",
    "uranium_spurs",
    "radiation",
    "disorienting_gai",
    "pestilence_attribute",
    "he_huan_san",
    "lime_powder",
    "poison_immunity",
    "hua_gu_san",
    "black_death",
    "wu_bu_she",
    "twilight",
    "poison_spell",
    "poison",
    "eternal_madness",
    "black_death",
  ]
  let radiation = [
    "hellstone_ingot",
    "nuclear_furnace_component",
    "aberration",
    "nuclear_bomb",
    "acidic_radrock",
    "uranium_spurs",
    "fissile",
    "radiation"
  ]
  let criticalStrike = [
    "gambler",
    "gazing_pearl",
    "dong_xi",
    "kubejs_criticalStrike",
    "all_kubejs_criticalStrike",
    "red_corundum_cluster",
    "black_beetle_vocalcord",
    "tui_jin_qi",
    "machine_star"
  ]
  let jab = [
    "jab",
    "fragment_of_eden",
    "euclidean_dagger",
    "orange_corundum_cluster",
    "connected"
  ]
  let wither = [
    "withered_bone",
    "withered_horn",
    "wither_skeleton_skull",
    "witherite_block"
  ]
  MaterialManager.registerFilter(m => {
    let result = false
    for (let i = 0; i < fire.length; i++) {
      if (m.effects.getLevel($ItemEffect.get(fire[i])) > 0) {
        result = true
      }
    }
    if (m.attributes != null && m.attributes.containsKey($ForgeRegistries.ATTRIBUTES.getValue('kubejs:fire_damage'))) {
      result = true
    }
    return result
  }, "§6火系相关材料")

  MaterialManager.registerFilter(m => {
    let result = false
    for (let i = 0; i < pestilence.length; i++) {
      if (m.effects.getLevel($ItemEffect.get(pestilence[i])) > 0) {
        result = true
      }
    }
    for (let i = 0; i < radiation.length; i++) {
      if (m.effects.getLevel($ItemEffect.get(radiation[i])) > 0) {
        result = true
      }
    }
    if (m.attributes != null && m.attributes.containsKey($ForgeRegistries.ATTRIBUTES.getValue('kubejs:pestilence'))) {
      result = true
    }
    return result
  }, "§2毒系相关材料")
  MaterialManager.registerFilter(m => {
    let result = false
    for (let i = 0; i < lighting.length; i++) {
      if (m.effects.getLevel($ItemEffect.get(lighting[i])) > 0) {
        result = true
      }
    }
    return result
  }, "§e雷系相关材料")

  MaterialManager.registerFilter(m => {
    let result = false
    for (let i = 0; i < radiation.length; i++) {
      if (m.effects.getLevel($ItemEffect.get(radiation[i])) > 0) {
        result = true
      }
    }
    return result
  }, "§a辐射相关材料")

  MaterialManager.registerFilter(m => {
    let result = false
    for (let i = 0; i < criticalStrike.length; i++) {
      if (m.effects.getLevel($ItemEffect.get(criticalStrike[i])) > 0) {
        result = true
      }
    }
    return result
  }, "§f暴击相关材料")

  MaterialManager.registerFilter(m => {
    let result = false
    for (let i = 0; i < jab.length; i++) {
      if (m.effects.getLevel($ItemEffect.get(jab[i])) > 0) {
        result = true
      }
    }
    return result
  }, "§f猛刺相关材料")

  MaterialManager.registerFilter(m => {
    let result = false
    for (let i = 0; i < wither.length; i++) {
      if (m.effects.getLevel($ItemEffect.get(wither[i])) > 0) {
        result = true
      }
    }
    return result
  }, "§f凋零相关材料")

  //sorter 排序器

  // MaterialManager.sorters.removeIf(m => { m.name == "ehp.sorter.primary"})
  MaterialManager.removeSorter("ehp.sorter.durability")
  MaterialManager.removeSorter("ehp.sorter.experienceCost")
  MaterialManager.removeSorter("ehp.sorter.requiredTools.hammer")
  MaterialManager.removeSorter("ehp.sorter.requiredTools.cut")
  MaterialManager.removeSorter("ehp.sorter.requiredTools.axe")




  function getAttributeValue(material, attribute_id, operationType) {
    if (material.attributes == null) return 0
    let attribute = $ForgeRegistries.ATTRIBUTES.getValue(attribute_id)
    if (attribute == null || !material.attributes.containsKey(attribute)) {
      return 0
    }
    if (operationType === undefined) {
      operationType = "all"
    }

    let additionTotal = 0
    let multiplyTotal = 0
    let modifiers = material.attributes.get(attribute)

    modifiers.forEach(modifier => {
      if (modifier.getOperation().name() == "ADDITION") {
        additionTotal += modifier.getAmount()
      } else if (modifier.getOperation().name() == "MULTIPLY_BASE" || modifier.getOperation().name() == "MULTIPLY_TOTAL") {
        multiplyTotal += modifier.getAmount()
      }
    })

    if (operationType == "addition") {
      return additionTotal
    } else if (operationType == "multiply") {
      return multiplyTotal
    } else {
      return additionTotal + multiplyTotal
    }
  }

  //* attribute 排序
  MaterialManager.registerSorter(m => {
    let damage = getAttributeValue(m, 'kubejs:fire_damage', "addition")
    return damage
  }, "§6火焰伤害 [加法区] ")

  MaterialManager.registerSorter(m => {
    let damage = getAttributeValue(m, 'kubejs:fire_damage', "multiply")
    return damage
  }, "§6火焰伤害 [乘法区]")

  MaterialManager.registerSorter(m => {
    let damage = getAttributeValue(m, 'kubejs:pestilence', "addition")
    return damage
  }, "§a瘟疫伤害 [加法区]")

  MaterialManager.registerSorter(m => {
    let damage = getAttributeValue(m, 'kubejs:pestilence', "multiply")
    return damage
  }, "§a瘟疫伤害 [乘法区]")

  MaterialManager.registerSorter(m => {
    let damage = getAttributeValue(m, 'minecraft:generic.attack_damage', "addition")
    return damage
  }, "§f攻击力 [加法区]")

  MaterialManager.registerSorter(m => {
    let damage = getAttributeValue(m, 'minecraft:generic.attack_damage', "multiply")
    return damage
  }, "§f攻击力 [乘法区]")

  MaterialManager.registerSorter(m => {
    let damage = getAttributeValue(m, 'minecraft:generic.attack_speed', "addition")
    return damage
  }, "§f攻击速度 [加法区]")

  MaterialManager.registerSorter(m => {
    let damage = getAttributeValue(m, 'minecraft:generic.attack_speed', "multiply")
    return damage
  }, "§f攻击速度 [乘法区]")

  //* effect 排序
  MaterialManager.registerSorter(m => {
    return m.effects.getLevel($ItemEffect.get('ocean_heart'))
  }, "海洋之心")
  MaterialManager.registerSorter(m => {
    return m.effects.getLevel($ItemEffect.get('floating'))
  }, "浮空")
  MaterialManager.registerSorter(m => {
    return m.effects.getLevel($ItemEffect.get('call_thunder'))
  }, "唤雷")
  MaterialManager.registerSorter(m => {
    return m.effects.getLevel($ItemEffect.get('kubejs_criticalStrike'))
  }, "暴击率")
  MaterialManager.registerSorter(m => {
    return m.effects.getEfficiency($ItemEffect.get('kubejs_criticalStrike'))
  }, "暴击伤害")
  MaterialManager.registerSorter(m => {
    return m.effects.getLevel($ItemEffect.get('jab'))
  }, "猛刺倍率")

  // MaterialManager.registerSorter(m => {
  //   return m.integrityGain
  // }, "完整度奖励")

  // MaterialManager.sorters.clear()
})



