// 配置项
const TOOL_ITEM = 'kubejs:support_indicator'
const COOLDOWN_MS = 1000   // 冷却时间改为1秒

// 加载 TFC 核心类
const Support = Java.loadClass('net.dries007.tfc.util.Support')
const CollapseRecipe = Java.loadClass('net.dries007.tfc.common.recipes.CollapseRecipe')

// 辅助函数：支撑检测
function checkIsSupported(level, pos) {
  return Support.isSupported(level, pos)
}

// 辅助函数
function checkWillTriggerCollapse(level, centerPos) {
  const fromPos = centerPos.offset(-4, -2, -4)
  const toPos = centerPos.offset(4, 2, 4)
  const unsupportedPositions = Support.findUnsupportedPositions(level, fromPos, toPos)
  for (let pos of unsupportedPositions) {
    if (!pos.equals(centerPos) && CollapseRecipe.canStartCollapse(level, pos)) {
      return true
    }
  }
  return false
}

// 岩石-矿物对应表
const ROCK_MINERAL_MAP = [
  {
    rocks: [
      'tfc:rock/raw/shale', 'tfc:rock/cobble/shale', 'tfc:rock/gravel/shale',
      'tfc:rock/mossy_cobble/shale', 'tfc:rock/hardened/shale'
    ],
    desc: '此为页岩岩层，可能生成：紫水晶、烟煤、硼砂、石膏、石盐、褐煤、辉铋矿、褐铁矿、磁铁矿、蛋白石、硝石、钾盐。'
  },
  {
    rocks: [
      'tfc:rock/raw/claystone', 'tfc:rock/cobble/claystone', 'tfc:rock/gravel/claystone',
      'tfc:rock/mossy_cobble/claystone', 'tfc:rock/hardened/claystone'
    ],
    desc: '此为黏土岩岩层，可能生成：紫水晶、烟煤、硼砂、石膏、石盐、褐煤、辉铋矿、褐铁矿、磁铁矿、蛋白石、硝石、钾盐。'
  },
  {
    rocks: [
      'tfc:rock/raw/limestone', 'tfc:rock/cobble/limestone', 'tfc:rock/gravel/limestone',
      'tfc:rock/mossy_cobble/limestone', 'tfc:rock/hardened/limestone'
    ],
    desc: '此为石灰岩岩层，可能生成：紫水晶、烟煤、硼砂、石膏、石盐、青金石、褐煤、辉铋矿、褐铁矿、磁铁矿、孔雀石（铜）、蛋白石、硝石。'
  },
  {
    rocks: [
      'tfc:rock/raw/conglomerate', 'tfc:rock/cobble/conglomerate', 'tfc:rock/gravel/conglomerate',
      'tfc:rock/mossy_cobble/conglomerate', 'tfc:rock/hardened/conglomerate'
    ],
    desc: '此为砾岩岩层，可能生成：紫水晶、烟煤、石膏、石盐、褐煤、辉铋矿、褐铁矿、磁铁矿、蛋白石、硝石。'
  },
  {
    rocks: [
      'tfc:rock/raw/dolomite', 'tfc:rock/cobble/dolomite', 'tfc:rock/gravel/dolomite',
      'tfc:rock/mossy_cobble/dolomite', 'tfc:rock/hardened/dolomite'
    ],
    desc: '此为白云岩岩层，可能生成：紫水晶、烟煤、石膏、石盐、褐煤、辉铋矿、褐铁矿、磁铁矿、孔雀石（铜）、蛋白石、硝石。'
  },
  {
    rocks: [
      'tfc:rock/raw/chert', 'tfc:rock/cobble/chert', 'tfc:rock/gravel/chert',
      'tfc:rock/mossy_cobble/chert', 'tfc:rock/hardened/chert'
    ],
    desc: '此为燧石岩层，可能生成：紫水晶、烟煤、石膏、石盐、褐煤、辉铋矿、褐铁矿、磁铁矿、蛋白石、硝石、钾盐。'
  },
  {
    rocks: [
      'tfc:rock/raw/chalk', 'tfc:rock/cobble/chalk', 'tfc:rock/gravel/chalk',
      'tfc:rock/mossy_cobble/chalk', 'tfc:rock/hardened/chalk'
    ],
    desc: '此为白垩岩岩层，可能生成：紫水晶、烟煤、石膏、石盐、褐煤、辉铋矿、褐铁矿、磁铁矿、孔雀石（铜）、蛋白石、硝石。'
  },
  {
    rocks: [
      'tfc:rock/raw/quartzite', 'tfc:rock/cobble/quartzite', 'tfc:rock/gravel/quartzite',
      'tfc:rock/mossy_cobble/quartzite', 'tfc:rock/hardened/quartzite'
    ],
    desc: '此为石英岩岩层，可能生成：紫水晶、朱砂、石墨、黝铜矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/slate', 'tfc:rock/cobble/slate', 'tfc:rock/gravel/slate',
      'tfc:rock/mossy_cobble/slate', 'tfc:rock/hardened/slate'
    ],
    desc: '此为板岩岩层，可能生成：紫水晶、黝铜矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/phyllite', 'tfc:rock/cobble/phyllite', 'tfc:rock/gravel/phyllite',
      'tfc:rock/mossy_cobble/phyllite', 'tfc:rock/hardened/phyllite'
    ],
    desc: '此为千枚岩岩层，可能生成：紫水晶、朱砂、黝铜矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/schist', 'tfc:rock/cobble/schist', 'tfc:rock/gravel/schist',
      'tfc:rock/mossy_cobble/schist', 'tfc:rock/hardened/schist'
    ],
    desc: '此为片岩岩层，可能生成：紫水晶、朱砂、石墨、原生银、黝铜矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/gneiss', 'tfc:rock/cobble/gneiss', 'tfc:rock/gravel/gneiss',
      'tfc:rock/mossy_cobble/gneiss', 'tfc:rock/hardened/gneiss'
    ],
    desc: '此为片麻岩岩层，可能生成：紫水晶、朱砂、石墨、原生银、黝铜矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/marble', 'tfc:rock/cobble/marble', 'tfc:rock/gravel/marble',
      'tfc:rock/mossy_cobble/marble', 'tfc:rock/hardened/marble'
    ],
    desc: '此为大理岩岩层，可能生成：紫水晶、石墨、青金石、孔雀石（铜）、黝铜矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/granite', 'tfc:rock/cobble/granite', 'tfc:rock/gravel/granite',
      'tfc:rock/mossy_cobble/granite', 'tfc:rock/hardened/granite'
    ],
    desc: '此为花岗岩岩层，可能生成：锡石、冰晶石、绿宝石、辉铋矿、绿镍矿、原生金、原生银、闪锌矿、黄铁矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/diorite', 'tfc:rock/cobble/diorite', 'tfc:rock/gravel/diorite',
      'tfc:rock/mossy_cobble/diorite', 'tfc:rock/hardened/diorite'
    ],
    desc: '此为闪长岩岩层，可能生成：锡石、冰晶石、绿宝石、辉铋矿、绿镍矿、原生金、原生银、闪锌矿、黄铁矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/gabbro', 'tfc:rock/cobble/gabbro', 'tfc:rock/gravel/gabbro',
      'tfc:rock/mossy_cobble/gabbro', 'tfc:rock/hardened/gabbro'
    ],
    desc: '此为辉长岩岩层，可能生成：钻石、锡石、绿宝石、辉铋矿、绿镍矿、原生金、闪锌矿、黄铁矿、硫磺。'
  },
  {
    rocks: [
      'tfc:rock/raw/rhyolite', 'tfc:rock/cobble/rhyolite', 'tfc:rock/gravel/rhyolite',
      'tfc:rock/mossy_cobble/rhyolite', 'tfc:rock/hardened/rhyolite'
    ],
    desc: '此为流纹岩岩层，可能生成：原生铜、赤铁矿、原生金、闪锌矿、蛋白石、黄铁矿。'
  },
  {
    rocks: [
      'tfc:rock/raw/basalt', 'tfc:rock/cobble/basalt', 'tfc:rock/gravel/basalt',
      'tfc:rock/mossy_cobble/basalt', 'tfc:rock/hardened/basalt'
    ],
    desc: '此为玄武岩岩层，可能生成：原生铜、赤铁矿、原生金、闪锌矿、蛋白石、黄铁矿。'
  },
  {
    rocks: [
      'tfc:rock/raw/andesite', 'tfc:rock/cobble/andesite', 'tfc:rock/gravel/andesite',
      'tfc:rock/mossy_cobble/andesite', 'tfc:rock/hardened/andesite'
    ],
    desc: '此为安山岩岩层，可能生成：原生铜、赤铁矿、原生金、闪锌矿、蛋白石、黄铁矿。'
  },
  {
    rocks: [
      'tfc:rock/raw/dacite', 'tfc:rock/cobble/dacite', 'tfc:rock/gravel/dacite',
      'tfc:rock/mossy_cobble/dacite', 'tfc:rock/hardened/dacite'
    ],
    desc: '此为英安岩岩层，可能生成：原生铜、赤铁矿、原生金、闪锌矿、蛋白石、黄铁矿。'
  }
]

// 右键事件：使用动作栏显示
ItemEvents.rightClicked(TOOL_ITEM, event => {
  const { player, target } = event
  if (!target || target.type !== 'block') return
  if (!player.isShiftKeyDown()) return

  // 冷却检查
  const lastCheck = player.persistentData.getLong('lastSupportCheck')
  const now = Date.now()
  if (now - lastCheck < COOLDOWN_MS) {
    player.statusMessage = Text.of("§c探测仪冷却中...")
    return
  }
  player.persistentData.putLong('lastSupportCheck', now)

  const block = target.block
  const originId = block.id
  const pos = block.pos
  const level = player.level

  // 岩石矿物匹配
  const rockMatch = ROCK_MINERAL_MAP.find(entry => entry.rocks.includes(originId))

  // TFC 支撑检测与塌方风险评估
  const isSupported = checkIsSupported(level, pos)
  const willTriggerCollapse = checkWillTriggerCollapse(level, pos)

  // 构建动作栏消息
  let message = ""
  if (rockMatch) {
    message += `§6${rockMatch.desc}\n`
  }
  message += isSupported ? "§a✓ 自身有支撑" : "§c✗ 自身无支撑"
  message += `\n`
  message += willTriggerCollapse ? "§c⚠ 破坏后可能引发周围塌方" : "§a✓ 破坏后不会引发塌方"

  player.statusMessage = Text.of(message)

  // 播放音效反馈
  if (isSupported && !willTriggerCollapse) {
    player.playSound('minecraft:block.note_block.pling', 1.0, 1.0)  // 完全安全
  } else if (!isSupported && willTriggerCollapse) {
    player.playSound('minecraft:entity.villager.no', 1.0, 1.0)      // 极度危险
  } else {
    player.playSound('minecraft:block.note_block.bit', 1.0, 0.8)     // 中间状态
  }
})