import type { EnemyKind } from './actionBoss/actionCombatSimulation'

export type ActionModelAssetCategory = 'action-bosses' | 'claudecraft-players' | 'claudecraft-mobs' | 'downloaded-dungeon-props' | 'downloaded-monsters' | 'downloaded-previews' | 'kaykit-skeletons' | 'opengameart-mobs'

type ActionModelFileFormat = 'dae' | 'gltf' | 'obj'

export type ActionFileModelAsset = {
  category: ActionModelAssetCategory
  defaultAnimation?: string
  format: ActionModelFileFormat
  id: string
  label: string
  materialVariant?: 'black' | 'emerald' | 'frost' | 'red'
  mtlUrl?: string
  source: string
  type: 'file'
  url: string
}

export type ActionProceduralModelAsset = {
  category: ActionModelAssetCategory
  enemyKind: EnemyKind
  id: string
  label: string
  source: string
  type: 'procedural'
}

export type ActionModelAsset = ActionFileModelAsset | ActionProceduralModelAsset

export type ActionWeaponAsset = {
  id: string
  label: string
  url: string
}

const CLAUDECRAFT_ROOT = '/action-assets/models/claudecraft'
const DOWNLOADED_ROOT = '/action-assets/models/downloaded'
const KAYKIT_DUNGEON_ROOT = `${DOWNLOADED_ROOT}/kaykit-dungeon-remastered`
const KAYKIT_ROOT = '/action-assets/models/kaykit-skeletons/characters'
const OPENGAMEART_ROOT = '/action-assets/models/opengameart'
const YUGIOH_ROOT = `${DOWNLOADED_ROOT}/yugioh`

export const ACTION_MODEL_CATEGORIES: Array<{ id: ActionModelAssetCategory; label: string }> = [
  { id: 'claudecraft-players', label: 'ClaudeCraft Players' },
  { id: 'claudecraft-mobs', label: 'ClaudeCraft Mobs' },
  { id: 'opengameart-mobs', label: 'OpenGameArt Mobs' },
  { id: 'downloaded-monsters', label: 'Downloaded Monsters' },
  { id: 'downloaded-dungeon-props', label: 'Downloaded Dungeon Props' },
  { id: 'downloaded-previews', label: 'Downloaded Previews' },
  { id: 'action-bosses', label: 'Current Bosses' },
  { id: 'kaykit-skeletons', label: 'KayKit Skeletons' },
]

export const ACTION_MODEL_ASSETS: ActionModelAsset[] = [
  model('claudecraft-players', 'woc-knight', 'Knight', `${CLAUDECRAFT_ROOT}/chars/players/knight.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-paladin', 'Paladin', `${CLAUDECRAFT_ROOT}/chars/players/paladin.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-ranger', 'Ranger', `${CLAUDECRAFT_ROOT}/chars/players/ranger.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-rogue', 'Rogue', `${CLAUDECRAFT_ROOT}/chars/players/rogue.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-rogue-hooded', 'Rogue Hooded', `${CLAUDECRAFT_ROOT}/chars/players/rogue_hooded.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-mage', 'Mage', `${CLAUDECRAFT_ROOT}/chars/players/mage.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-mage-classic', 'Mage Classic', `${CLAUDECRAFT_ROOT}/chars/players/mage_classic.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-druid', 'Druid', `${CLAUDECRAFT_ROOT}/chars/players/druid.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-barbarian', 'Barbarian', `${CLAUDECRAFT_ROOT}/chars/players/barbarian.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-players', 'woc-combat-mech', 'Combat Mech', `${CLAUDECRAFT_ROOT}/chars/players/CombatMech.glb`, 'World of ClaudeCraft', 'Idle'),

  model('claudecraft-mobs', 'woc-wild-boar', 'Wild Boar', `${CLAUDECRAFT_ROOT}/creatures/wild_boar.glb`, 'World of ClaudeCraft', 'Idle1'),
  model('claudecraft-mobs', 'woc-wolf', 'Wolf', `${CLAUDECRAFT_ROOT}/creatures/wolf.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-mobs', 'woc-spider', 'Spider', `${CLAUDECRAFT_ROOT}/creatures/spider.glb`, 'World of ClaudeCraft', 'Spider_Idle'),
  model('claudecraft-mobs', 'woc-velociraptor', 'Velociraptor', `${CLAUDECRAFT_ROOT}/creatures/velociraptor.glb`, 'World of ClaudeCraft', 'Velociraptor_Idle'),
  model('claudecraft-mobs', 'woc-dragon-evolved', 'Dragon Evolved', `${CLAUDECRAFT_ROOT}/creatures/dragonevolved.glb`, 'World of ClaudeCraft', 'Flying_Idle'),
  model('claudecraft-mobs', 'woc-golem-evolved', 'Goleling Evolved', `${CLAUDECRAFT_ROOT}/creatures/golelingevolved.glb`, 'World of ClaudeCraft', 'Flying_Idle'),
  model('claudecraft-mobs', 'woc-goblin', 'Goblin', `${CLAUDECRAFT_ROOT}/creatures/goblin.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-mobs', 'woc-orc', 'Orc', `${CLAUDECRAFT_ROOT}/creatures/orc.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-mobs', 'woc-giant', 'Giant', `${CLAUDECRAFT_ROOT}/creatures/giant.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-mobs', 'woc-yeti', 'Yeti', `${CLAUDECRAFT_ROOT}/creatures/yetialt.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-mobs', 'woc-necromancer', 'Necromancer', `${CLAUDECRAFT_ROOT}/chars/enemies/necromancer.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-mobs', 'woc-skeleton-warrior', 'Skeleton Warrior', `${CLAUDECRAFT_ROOT}/chars/enemies/skeleton_warrior.glb`, 'World of ClaudeCraft', 'Idle'),
  model('claudecraft-mobs', 'woc-skeleton-mage', 'Skeleton Mage', `${CLAUDECRAFT_ROOT}/chars/enemies/skeleton_mage.glb`, 'World of ClaudeCraft', 'Idle_Combat'),
  model('claudecraft-mobs', 'woc-skeleton-golem', 'Skeleton Golem Boss', `${CLAUDECRAFT_ROOT}/chars/enemies/skeleton_golem.glb`, 'World of ClaudeCraft', 'Idle'),

  model('opengameart-mobs', 'oga-cethiels-dragon', 'Cethiel Dragon Original', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-animated.glb`, 'OpenGameArt CC0 - Cethiel and Drummyfish', 'Dragon_Idle'),
  model('opengameart-mobs', 'oga-cethiels-dragon-red', 'Cethiel Dragon Red', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-animated.glb`, 'OpenGameArt CC0 - color preview', 'Dragon_Idle', 'red'),
  model('opengameart-mobs', 'oga-cethiels-dragon-black', 'Cethiel Dragon Black', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-animated.glb`, 'OpenGameArt CC0 - color preview', 'Dragon_Idle', 'black'),
  model('opengameart-mobs', 'oga-cethiels-dragon-frost', 'Cethiel Dragon Frost', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-animated.glb`, 'OpenGameArt CC0 - color preview', 'Dragon_Idle', 'frost'),
  model('opengameart-mobs', 'oga-cethiels-dragon-emerald', 'Cethiel Dragon Emerald', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-animated.glb`, 'OpenGameArt CC0 - color preview', 'Dragon_Idle', 'emerald'),
  model('opengameart-mobs', 'oga-cethiels-dragon-idle', 'Cethiel Dragon Idle Anim', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-idle-anim.glb`, 'OpenGameArt CC0 - animation preview', 'Armature_Armature'),
  model('opengameart-mobs', 'oga-cethiels-dragon-walk', 'Cethiel Dragon Walk Anim', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-walk-anim.glb`, 'OpenGameArt CC0 - animation preview', 'Armature_Armature'),
  model('opengameart-mobs', 'oga-cethiels-dragon-attack', 'Cethiel Dragon Attack Anim', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-attack-anim.glb`, 'OpenGameArt CC0 - animation preview', 'Armature_Armature'),
  model('opengameart-mobs', 'oga-cethiels-dragon-die', 'Cethiel Dragon Death Anim', `${OPENGAMEART_ROOT}/dragon-oga/dragon-oga-die-anim.glb`, 'OpenGameArt CC0 - animation preview', 'Armature_Armature'),

  daeModel('downloaded-monsters', 'downloaded-blue-eyes-white-dragon', 'Blue-Eyes White Dragon', `${YUGIOH_ROOT}/blue-eyes-white-dragon/MMD_004007.dae`, 'Downloaded DAE monster model'),
  daeModel('downloaded-monsters', 'downloaded-blue-eyes-ultimate-dragon', 'Blue-Eyes Ultimate Dragon', `${YUGIOH_ROOT}/blue-eyes-ultimate-dragon/MMD_004386.dae`, 'Downloaded DAE monster model'),
  daeModel('downloaded-monsters', 'downloaded-red-eyes-black-dragon', 'Red-Eyes Black Dragon', `${YUGIOH_ROOT}/red-eyes-black-dragon/MMD_004088.dae`, 'Downloaded DAE monster model'),
  objModel('downloaded-monsters', 'downloaded-big-eyes-red-dragon', 'Big Eyes Red Dragon', `${DOWNLOADED_ROOT}/big-eyes-red-dragon/redeyesdragon.obj`, 'Downloaded OBJ monster model', `${DOWNLOADED_ROOT}/big-eyes-red-dragon/redeyesdragon.mtl`),
  daeModel('downloaded-monsters', 'downloaded-gandora-dragon', 'Gandora Dragon', `${YUGIOH_ROOT}/gandora-the-dragon-of-destruction/MMD_006076.dae`, 'Downloaded DAE monster model'),
  model('downloaded-monsters', 'downloaded-black-dragon-animated', 'Black Dragon Animated', `${DOWNLOADED_ROOT}/black-dragon-new/black-dragon-baked.glb`, 'Downloaded FBX monster model converted to GLB', 'Armature|Idel_New'),
  model('downloaded-monsters', 'downloaded-low-poly-spider-animated', 'Low-Poly Spider Animated', `${DOWNLOADED_ROOT}/low-poly-spider/low-poly-spider.glb`, 'Downloaded FBX monster model converted to GLB', 'Spider_Armature|warte_pose'),
  daeModel('downloaded-monsters', 'downloaded-gate-guardian', 'Gate Guardian', `${YUGIOH_ROOT}/gate-guardian/MMD_004380.dae`, 'Downloaded DAE monster model'),
  daeModel('downloaded-monsters', 'downloaded-insect-queen', 'Insect Queen', `${YUGIOH_ROOT}/insect-queen/MMD_004768.dae`, 'Downloaded DAE monster model'),
  daeModel('downloaded-monsters', 'downloaded-pumpking', 'Pumpking the King of Ghosts', `${YUGIOH_ROOT}/pumpking-the-king-of-ghosts/MMD_004105.dae`, 'Downloaded DAE monster model'),

  ...downloadedDungeonProps(
    'banner_blue',
    'barrel_large',
    'barrel_large_decorated',
    'barrel_small_stack',
    'barrier_column',
    'bed_decorated',
    'box_stacked',
    'candle_lit',
    'candle_triple',
    'chair',
    'chest',
    'chest_gold',
    'coin_stack_large',
    'column',
    'crates_stacked',
    'floor_tile_big_grate_open',
    'floor_tile_big_spikes',
    'keyring_hanging',
    'pillar',
    'pillar_decorated',
    'rubble_large',
    'shelf_small_candles',
    'stairs_long',
    'stairs_wide',
    'sword_shield_gold',
    'table_long_decorated_A',
    'torch_lit',
    'torch_mounted',
    'wall_archedwindow_open',
    'wall_broken',
    'wall_doorway',
    'wall_gated',
    'wall_window_open',
  ),

  model('downloaded-previews', 'ual1-standard', 'UAL 1 Mannequin Animations', `${DOWNLOADED_ROOT}/ual/UAL1_Standard.glb`, 'Universal Animation Library / Quaternius CC0', 'Idle_Loop'),
  model('downloaded-previews', 'ual1-standard-rm', 'UAL 1 Root Motion Animations', `${DOWNLOADED_ROOT}/ual/UAL1_Standard_RM.glb`, 'Universal Animation Library / Quaternius CC0 - root motion', 'Jog_Fwd_Loop'),
  model('downloaded-previews', 'ual2-standard', 'UAL 2 Mannequin Animations', `${DOWNLOADED_ROOT}/ual/UAL2_Standard.glb`, 'Universal Animation Library 2 / Quaternius CC0', 'Idle_Shield_Loop'),
  model('downloaded-previews', 'ual2-standard-rm', 'UAL 2 Root Motion Animations', `${DOWNLOADED_ROOT}/ual/UAL2_Standard_RM.glb`, 'Universal Animation Library 2 / Quaternius CC0 - root motion', 'Melee_Hook'),
  model('downloaded-previews', 'ual2-female-mannequin', 'UAL Female Mannequin', `${DOWNLOADED_ROOT}/ual/Mannequin_F.glb`, 'Universal Animation Library 2 / Quaternius CC0'),
  model('downloaded-previews', 'kaykit-mannequin-medium', 'KayKit Medium Mannequin', `${DOWNLOADED_ROOT}/kaykit-character-animations/mannequin/Mannequin_Medium.glb`, 'KayKit Character Animations CC0'),
  model('downloaded-previews', 'kaykit-mannequin-large', 'KayKit Large Mannequin', `${DOWNLOADED_ROOT}/kaykit-character-animations/mannequin/Mannequin_Large.glb`, 'KayKit Character Animations CC0'),
  model('downloaded-previews', 'kaykit-medium-combat-melee', 'KayKit Medium Melee Animations', `${DOWNLOADED_ROOT}/kaykit-character-animations/rig-medium/Rig_Medium_CombatMelee.glb`, 'KayKit Character Animations CC0', 'Melee_1H_Attack_Chop'),
  model('downloaded-previews', 'kaykit-medium-combat-ranged', 'KayKit Medium Ranged Animations', `${DOWNLOADED_ROOT}/kaykit-character-animations/rig-medium/Rig_Medium_CombatRanged.glb`, 'KayKit Character Animations CC0', 'Ranged_Magic_Spellcasting'),
  model('downloaded-previews', 'kaykit-medium-general', 'KayKit Medium General Animations', `${DOWNLOADED_ROOT}/kaykit-character-animations/rig-medium/Rig_Medium_General.glb`, 'KayKit Character Animations CC0', 'Idle_A'),
  model('downloaded-previews', 'kaykit-medium-movement-basic', 'KayKit Medium Basic Movement', `${DOWNLOADED_ROOT}/kaykit-character-animations/rig-medium/Rig_Medium_MovementBasic.glb`, 'KayKit Character Animations CC0', 'Running_A'),
  model('downloaded-previews', 'kaykit-medium-movement-advanced', 'KayKit Medium Advanced Movement', `${DOWNLOADED_ROOT}/kaykit-character-animations/rig-medium/Rig_Medium_MovementAdvanced.glb`, 'KayKit Character Animations CC0', 'Dodge_Forward'),
  model('downloaded-previews', 'kaykit-medium-special', 'KayKit Medium Special Animations', `${DOWNLOADED_ROOT}/kaykit-character-animations/rig-medium/Rig_Medium_Special.glb`, 'KayKit Character Animations CC0', 'Skeletons_Idle'),
  model('downloaded-previews', 'kaykit-medium-simulation', 'KayKit Medium Simulation Animations', `${DOWNLOADED_ROOT}/kaykit-character-animations/rig-medium/Rig_Medium_Simulation.glb`, 'KayKit Character Animations CC0', 'Cheering'),
  model('downloaded-previews', 'kaykit-medium-tools', 'KayKit Medium Tool Animations', `${DOWNLOADED_ROOT}/kaykit-character-animations/rig-medium/Rig_Medium_Tools.glb`, 'KayKit Character Animations CC0', 'Chopping'),

  model('action-bosses', 'action-bulldrome', 'Bulldrome', `${CLAUDECRAFT_ROOT}/creatures/wild_boar.glb`, 'Action Mode boss model', 'Idle1'),
  procedural('action-bosses', 'action-bullfango', 'Bullfango', 'bullfango'),
  procedural('action-bosses', 'action-yian-kut-ku', 'Yian Kut-Ku', 'yian-kut-ku'),
  procedural('action-bosses', 'action-cyber-dragon', 'Cyber Dragon', 'cyber-dragon'),

  model('kaykit-skeletons', 'kaykit-warrior', 'Skeleton Warrior', `${KAYKIT_ROOT}/Skeleton_Warrior.glb`, 'KayKit Skeletons', 'Idle_Combat'),
  model('kaykit-skeletons', 'kaykit-rogue', 'Skeleton Rogue', `${KAYKIT_ROOT}/Skeleton_Rogue.glb`, 'KayKit Skeletons', 'Idle_Combat'),
  model('kaykit-skeletons', 'kaykit-mage', 'Skeleton Mage', `${KAYKIT_ROOT}/Skeleton_Mage.glb`, 'KayKit Skeletons', 'Spellcasting'),
  model('kaykit-skeletons', 'kaykit-minion', 'Skeleton Minion', `${KAYKIT_ROOT}/Skeleton_Minion.glb`, 'KayKit Skeletons', 'Idle_Combat'),
]

export const ACTION_WEAPON_ASSETS: ActionWeaponAsset[] = [
  { id: 'none', label: 'None', url: '' },
  { id: 'sword-1h', label: 'Sword 1H', url: `${CLAUDECRAFT_ROOT}/weapons/sword_1handed.glb` },
  { id: 'sword-2h', label: 'Sword 2H', url: `${CLAUDECRAFT_ROOT}/weapons/sword_2handed.glb` },
  { id: 'dagger', label: 'Dagger', url: `${CLAUDECRAFT_ROOT}/weapons/dagger.glb` },
  { id: 'axe-1h', label: 'Axe 1H', url: `${CLAUDECRAFT_ROOT}/weapons/axe_1handed.glb` },
  { id: 'axe-2h', label: 'Axe 2H', url: `${CLAUDECRAFT_ROOT}/weapons/axe_2handed.glb` },
  { id: 'staff', label: 'Staff', url: `${CLAUDECRAFT_ROOT}/weapons/staff.glb` },
  { id: 'crossbow', label: 'Crossbow', url: `${CLAUDECRAFT_ROOT}/weapons/crossbow_1handed.glb` },
  { id: 'wand', label: 'Wand', url: `${CLAUDECRAFT_ROOT}/weapons/wand.glb` },
  { id: 'shield-round', label: 'Round Shield', url: `${CLAUDECRAFT_ROOT}/weapons/shield_round.glb` },
  { id: 'skeleton-blade', label: 'Skeleton Blade', url: `${CLAUDECRAFT_ROOT}/weapons/skeleton_blade.glb` },
  { id: 'skeleton-staff', label: 'Skeleton Staff', url: `${CLAUDECRAFT_ROOT}/weapons/skeleton_staff.glb` },
  { id: 'halberd', label: 'Halberd', url: `${CLAUDECRAFT_ROOT}/weapons/halberd.glb` },
  { id: 'scythe', label: 'Scythe', url: `${CLAUDECRAFT_ROOT}/weapons/scythe.glb` },
]

function model(
  category: ActionModelAssetCategory,
  id: string,
  label: string,
  url: string,
  source: string,
  defaultAnimation?: string,
  materialVariant?: ActionFileModelAsset['materialVariant'],
): ActionFileModelAsset {
  return { category, defaultAnimation, format: 'gltf', id, label, materialVariant, source, type: 'file', url }
}

function daeModel(
  category: ActionModelAssetCategory,
  id: string,
  label: string,
  url: string,
  source: string,
): ActionFileModelAsset {
  return { category, format: 'dae', id, label, source, type: 'file', url }
}

function objModel(
  category: ActionModelAssetCategory,
  id: string,
  label: string,
  url: string,
  source: string,
  mtlUrl?: string,
): ActionFileModelAsset {
  return { category, format: 'obj', id, label, mtlUrl, source, type: 'file', url }
}

function downloadedDungeonProps(...ids: string[]): ActionFileModelAsset[] {
  return ids.map((id) => model(
    'downloaded-dungeon-props',
    `kaykit-dungeon-${id.toLowerCase().replace(/_/g, '-')}`,
    toLabel(id),
    `${KAYKIT_DUNGEON_ROOT}/${id}.gltf`,
    'KayKit Dungeon Remastered CC0',
  ))
}

function toLabel(id: string) {
  return id
    .split('_')
    .map((part) => part.length === 1 ? part : `${part[0].toUpperCase()}${part.slice(1)}`)
    .join(' ')
}

function procedural(
  category: ActionModelAssetCategory,
  id: string,
  label: string,
  enemyKind: EnemyKind,
): ActionProceduralModelAsset {
  return { category, enemyKind, id, label, source: 'Action Mode procedural', type: 'procedural' }
}
