// 配置项
const TOOL_ITEM = 'kubejs:support_indicator'
const COOLDOWN_MS = 1000

// 支撑方块列表（请确保ID与游戏中完全一致）
const SMALL_SUPPORT_BLOCKS = [
  'tfc:wood/vertical_support/acacia',
  'tfc:wood/horizontal_support/acacia',
  'tfc:wood/vertical_support/ash',
  'tfc:wood/horizontal_support/ash',
  'tfc:wood/vertical_support/aspen',
  'tfc:wood/horizontal_support/aspen',
  'tfc:wood/vertical_support/birch',
  'tfc:wood/horizontal_support/birch',
  'tfc:wood/vertical_support/blackwood',
  'tfc:wood/horizontal_support/blackwood',
  'tfc:wood/vertical_support/chestnut',
  'tfc:wood/horizontal_support/chestnut',
  'tfc:wood/vertical_support/douglas_fir',
  'tfc:wood/horizontal_support/douglas_fir',
  'tfc:wood/vertical_support/hickory',
  'tfc:wood/horizontal_support/hickory',
  'tfc:wood/vertical_support/kapok',
  'tfc:wood/horizontal_support/kapok',
  'tfc:wood/vertical_support/mangrove',
  'tfc:wood/horizontal_support/mangrove',
  'tfc:wood/vertical_support/maple',
  'tfc:wood/horizontal_support/maple',
  'tfc:wood/vertical_support/oak',
  'tfc:wood/horizontal_support/oak',
  'tfc:wood/vertical_support/palm',
  'tfc:wood/horizontal_support/palm',
  'tfc:wood/vertical_support/pine',
  'tfc:wood/horizontal_support/pine',
  'tfc:wood/vertical_support/rosewood',
  'tfc:wood/horizontal_support/rosewood',
  'tfc:wood/vertical_support/sequoia',
  'tfc:wood/horizontal_support/sequoia',
  'tfc:wood/vertical_support/spruce',
  'tfc:wood/horizontal_support/spruce',
  'tfc:wood/vertical_support/sycamore',
  'tfc:wood/horizontal_support/sycamore',
  'tfc:wood/vertical_support/white_cedar',
  'tfc:wood/horizontal_support/white_cedar',
  'tfc:wood/vertical_support/willow',
  'tfc:wood/horizontal_support/willow'
]

const MEDIUM_SUPPORT_BLOCKS = [
  'immersiveengineering:steel_scaffolding_standard',
  'immersiveengineering:steel_scaffolding_grate_top'
]

const LARGE_SUPPORT_BLOCKS = [
  'kubejs:temporary_geological_anchor',
  'kubejs:infinite_geological_anchor'
]

const SUPPORT_LISTS = [
  { list: SMALL_SUPPORT_BLOCKS, radius: 4, yRadius: 2 },
  { list: MEDIUM_SUPPORT_BLOCKS, radius: 7, yRadius: 3 },
  { list: LARGE_SUPPORT_BLOCKS, radius: 42, yRadius: 15 }
]

// ========== 已自动生成的全岩石变种-矿物对应表（TFC官方译名适配） ==========
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, hand, item } = 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.tell('§c探测仪冷却中...')
    return
  }
  player.persistentData.putLong('lastSupportCheck', now)

  const blockContainer = target.block
  const originId = blockContainer.id
  const cx = blockContainer.pos.x
  const cy = blockContainer.pos.y
  const cz = blockContainer.pos.z

  const level = player.level

  // ========== 岩石-矿物提示逻辑 ==========
  const rockMatch = ROCK_MINERAL_MAP.find(item => item.rocks.includes(originId))
  if (rockMatch) {
    player.tell(`§6${rockMatch.desc}`)
  }

  // 原有支撑检测逻辑
  let found = false
  for (const { list, radius, yRadius } of SUPPORT_LISTS) {
    outer:
    for (let x = -radius; x <= radius; x++) {
      for (let y = -yRadius; y <= yRadius; y++) {
        for (let z = -radius; z <= radius; z++) {
          let checkX = cx + x;
          let checkY = cy + y;
          let checkZ = cz + z;
          let currentId = level.getBlock(checkX, checkY, checkZ).id;

          if (list.includes(currentId)) {
            found = true;
            break outer;
          }
        }
      }
    }
    if (found) break;
  }

  if (found) {
    player.tell('§a✓ 检测到支撑！安全！')
    player.playSound('minecraft:block.note_block.pling', 1.0, 1.0)
  } else {
    player.tell('§c✗ 未检测到任何支撑，可能坍塌！')
    player.playSound('minecraft:entity.villager.no', 1.0, 1.0)
  }
})