//Priority: 1
/**
 * @file        mob_drops.js
 * @description Custom drops for bosses and special mobs.
 * @modpack     Monumental Experience
 * @author      KnightDexx
 * @version     2.0 - EntityEvents.death Edition 
 * @copyright   KnightDexx's Modded Minecraft World
 * @license     All Rights Reserved
 */

var ForgeRegistries = Java.loadClass('net.minecraftforge.registries.ForgeRegistries')

// ═══════════════════════════════════════════════════════════
// CUSTOM MOB DROPS SYSTEM
// ═══════════════════════════════════════════════════════════

console.info('═══════════════════════════════════════════════════════')
console.info('[AOF] Custom Mob Drops System v2.0')
console.info('[AOF] Adding unique drops to bosses & special mobs...')
console.info('═══════════════════════════════════════════════════════')

// ═══════════════════════════════════════════════════════════
// CUSTOM MOB DROPS CONFIGURATION
// ═══════════════════════════════════════════════════════════

var MOB_DROPS = {
  // ───────────────────────────────────────────────────────────
  // BOSS DROPS (100% GUARANTEED)
  // ───────────────────────────────────────────────────────────
  boss_drops: {
    'minecraft:ender_dragon': [
      { item: 'minecraft:elytra', count: 1 }
    ],
    'minecraft:warden': [
      { item: 'enigmaticlegacy:abyssal_heart', count: 1 },
    ],
    'minecraft:witch': [
      { item: 'reliquary:witch_hat', count: 1 }
    ],
    'minecraft:wither': [
      { item: 'enigmaticlegacy:earth_heart', count: 1 }
    ]
  },

  // ───────────────────────────────────────────────────────────
  // RARE DROPS (10% CHANCE)
  // ───────────────────────────────────────────────────────────
  rare_drops: {
    'alexsmobs:soul_vulture': [
      { item: 'endrem:undead_soul', count: 1, chance: 0.10 }
    ],
    'aquamirae:tortured_soul': [
      { item: 'endrem:undead_soul', count: 1, chance: 0.10 }
    ],
    'minecraft:bat': [
      { item: 'minecraft:phantom_membrane', count: 1, chance: 0.10 }
    ],
    'minecraft:creeper': [
      { item: 'minecraft:tnt', count: 1, chance: 0.10 }
    ],
    'minecraft:vex': [
      { item: 'enigmaticlegacy:earth_heart', count: 1, chance: 0.10 }
    ]
  },

  // ───────────────────────────────────────────────────────────
  // VERY RARE DROPS (5% CHANCE)
  // ───────────────────────────────────────────────────────────
  very_rare_drops: {
    'minecraft:wither_skeleton': [
      { item: 'enigmaticlegacy:earth_heart', count: 1, chance: 0.05 }
    ]
  },

  // ───────────────────────────────────────────────────────────
  // SPECIAL DROPS (Custom rules)
  // ───────────────────────────────────────────────────────────
  special_drops: {
    'minecraft:shulker': [
      { item: 'minecraft:shulker_shell', count: 2, chance: 1.0 }
    ]
  }
}

// ═══════════════════════════════════════════════════════════
// BUILD UNIFIED MOB -> DROPS MAPPING
// ═══════════════════════════════════════════════════════════

if (!global.AOF_MOB_DROPS_MAP) {
  global.AOF_MOB_DROPS_MAP = {}
  var dropCount = 0

  // Boss drops (100%)
  Object.keys(MOB_DROPS.boss_drops).forEach(mobId => {
    global.AOF_MOB_DROPS_MAP[mobId] = MOB_DROPS.boss_drops[mobId]
    dropCount++
  })

  // Rare drops (10%)
  Object.keys(MOB_DROPS.rare_drops).forEach(mobId => {
    if (!global.AOF_MOB_DROPS_MAP[mobId]) {
      global.AOF_MOB_DROPS_MAP[mobId] = []
    }
    MOB_DROPS.rare_drops[mobId].forEach(drop => {
      global.AOF_MOB_DROPS_MAP[mobId].push(drop)
    })
    dropCount++
  })

  // Very rare drops (5%)
  Object.keys(MOB_DROPS.very_rare_drops).forEach(mobId => {
    if (!global.AOF_MOB_DROPS_MAP[mobId]) {
      global.AOF_MOB_DROPS_MAP[mobId] = []
    }
    MOB_DROPS.very_rare_drops[mobId].forEach(drop => {
      global.AOF_MOB_DROPS_MAP[mobId].push(drop)
    })
    dropCount++
  })

  // Special drops
  Object.keys(MOB_DROPS.special_drops).forEach(mobId => {
    if (!global.AOF_MOB_DROPS_MAP[mobId]) {
      global.AOF_MOB_DROPS_MAP[mobId] = []
    }
    MOB_DROPS.special_drops[mobId].forEach(drop => {
      global.AOF_MOB_DROPS_MAP[mobId].push(drop)
    })
    dropCount++
  })

  console.info('═══════════════════════════════════════════════════════')
  console.info('[AOF] Custom Mob Drops v2.0 - EntityEvents.death')
  console.info('═══════════════════════════════════════════════════════')
  console.info(`[AOF] ✓ Built drop mapping for ${dropCount} mob types`)
  console.info('[AOF]')
  console.info('[AOF] ✓ 13 Boss drops (100% guaranteed)')
  console.info('[AOF] ✓ 5 Rare drops (10% chance)')
  console.info('[AOF] ✓ 1 Very rare drop (5% chance)')
  console.info('[AOF] ✓ 1 Special drop (Shulker: 2x shells)')
  console.info('[AOF]')
  console.info('[AOF] 🏆 Hunt bosses for unique rewards!')
  console.info('[AOF] ✨ Every boss drops exclusive loot!')
  console.info('[AOF] 💎 Warden drops 2 items (Abyssal Heart + Jewel)!')
  console.info('[AOF] 🎯 Ready for action!')
  console.info('═══════════════════════════════════════════════════════')
}

// ═══════════════════════════════════════════════════════════
// MAIN EVENT HANDLER - EntityEvents.death
// ═══════════════════════════════════════════════════════════

EntityEvents.death((event) => {
  // Check if feature is enabled
  if (!global.AOF_LOOT || !global.AOF_LOOT.FEATURES.BOSS_REWARDS) {
    return
  }

  var entity = event.entity
  var mobType = String(ForgeRegistries.ENTITY_TYPES.getKey(entity.type))
  var dropsMap = global.AOF_MOB_DROPS_MAP

  // Check if this mob has custom drops
  var drops = dropsMap[mobType]

  if (!drops) {
    return
  }

  // Process each drop
  drops.forEach(drop => {
    var chance = drop.chance || 1.0  // Default to 100%

    if (Math.random() < chance) {
      // Drop the item(s)
      var count = drop.count || 1

      for (var i = 0; i < count; i++) {
        entity.block.popItem(drop.item)
      }

      // Debug log
      var chanceStr = chance < 1.0 ? ` (${(chance * 100).toFixed(0)}%)` : ''
      console.info(`[AOF] ${mobType} dropped ${count}x ${drop.item}${chanceStr}`)
    }
  })
})