import {
  getActionAttack,
  type ActionAttackKind,
} from './actionEncounterConfig'
import type { EnemyKind } from './actionCombatSimulation'

const BOSS_TRACK_SECONDS = 1.15
const BOSS_WINDUP_SECONDS = 0.82
const BOSS_CHARGE_SECONDS = 0.74
const BOSS_RECOVER_SECONDS = 0.86
const BOSS_TANK_FOCUS_SECONDS = 2.8
const BOSS_TANK_SWING_SECONDS = 0.9
const BOSS_TANK_SWING_DAMAGE = 13
const BOSS_CHARGE_SPEED = 650
const BOSS_BODY_DAMAGE = 10
const BOSS_CHARGE_DAMAGE = 24
const BULLFANGO_TRACK_SECONDS = 2.35
const BULLFANGO_WINDUP_SECONDS = 0.68
const BULLFANGO_CHARGE_SECONDS = 0.58
const BULLFANGO_RECOVER_SECONDS = 1.05
const BULLFANGO_TANK_FOCUS_SECONDS = 1.6
const BULLFANGO_TANK_SWING_SECONDS = 1.35
const BULLFANGO_TANK_SWING_DAMAGE = 6
const BULLFANGO_CHARGE_SPEED = 510
const BULLFANGO_BODY_DAMAGE = 4
const BULLFANGO_CHARGE_DAMAGE = 13
const YIAN_TRACK_SECONDS = 2.1
const YIAN_RECOVER_SECONDS = 1.1
const YIAN_TANK_FOCUS_SECONDS = 2.4
const YIAN_TANK_SWING_SECONDS = 1
const YIAN_TANK_SWING_DAMAGE = 11
const YIAN_BODY_DAMAGE = 8
const BIRD_FLIGHT_TIMER_SECONDS = 3.2
const BIRD_FLIGHT_SPEED = 340
const BIRD_TANK_SWING_SECONDS = 1.15
const BIRD_TANK_SWING_DAMAGE = 5
const BIRD_BODY_DAMAGE = 4
const BIRD_CHARGE_DAMAGE = 12
const CYBER_TRACK_SECONDS = 1.9
const CYBER_TANK_FOCUS_SECONDS = 2.2
const CYBER_TANK_SWING_SECONDS = 1
const CYBER_TANK_SWING_DAMAGE = 12
const CYBER_BODY_DAMAGE = 8
const CYBER_CIRCLE_SECONDS = 1.35
const CYBER_CHARGE_SECONDS = 0.72
const CYBER_CHARGE_SPEED = 520
const CYBER_CHARGE_DAMAGE = 18

export function getEnemyWindupSeconds(kind: EnemyKind) {
  if (kind === 'cyber-dragon') return getAttackNumber(kind, 'circularCharge', 'windupSeconds', CYBER_CIRCLE_SECONDS)
  const fallback = kind === 'bulldrome' ? BOSS_WINDUP_SECONDS : BULLFANGO_WINDUP_SECONDS
  return getAttackNumber(kind, 'charge', 'windupSeconds', fallback)
}

export function getEnemyChargeSeconds(kind: EnemyKind) {
  if (kind === 'yian-kut-ku' || kind === 'bird') return 0
  if (kind === 'cyber-dragon') return CYBER_CHARGE_SECONDS
  return kind === 'bulldrome' ? BOSS_CHARGE_SECONDS : BULLFANGO_CHARGE_SECONDS
}

export function getEnemyChargeSpeed(kind: EnemyKind) {
  if (kind === 'yian-kut-ku') return 0
  if (kind === 'bird') return getAttackNumber(kind, 'birdDive', 'speed', BIRD_FLIGHT_SPEED)
  if (kind === 'cyber-dragon') return getAttackNumber(kind, 'circularCharge', 'speed', CYBER_CHARGE_SPEED)
  const fallback = kind === 'bulldrome' ? BOSS_CHARGE_SPEED : BULLFANGO_CHARGE_SPEED
  return getAttackNumber(kind, 'charge', 'speed', fallback)
}

export function getEnemyRecoverSeconds(kind: EnemyKind) {
  if (kind === 'yian-kut-ku') return getAttackNumber(kind, 'fireballVolley', 'recoverSeconds', YIAN_RECOVER_SECONDS)
  if (kind === 'bird') return 0
  if (kind === 'cyber-dragon') return getAttackNumber(kind, 'circularCharge', 'recoverSeconds', 0.8)
  const fallback = kind === 'bulldrome' ? BOSS_RECOVER_SECONDS : BULLFANGO_RECOVER_SECONDS
  return getAttackNumber(kind, 'charge', 'recoverSeconds', fallback)
}

export function getEnemyTankFocusSeconds(kind: EnemyKind) {
  if (kind === 'yian-kut-ku') return getAttackFrequency(kind, 'fireballVolley', YIAN_TANK_FOCUS_SECONDS)
  if (kind === 'bird') return getAttackFrequency(kind, 'birdDive', BIRD_FLIGHT_TIMER_SECONDS)
  if (kind === 'cyber-dragon') return getAttackFrequency(kind, 'circularCharge', CYBER_TANK_FOCUS_SECONDS)
  const fallback = kind === 'bulldrome' ? BOSS_TANK_FOCUS_SECONDS : BULLFANGO_TANK_FOCUS_SECONDS
  return getAttackFrequency(kind, 'charge', fallback)
}

export function getEnemyTankSwingSeconds(kind: EnemyKind) {
  const fallback = kind === 'yian-kut-ku'
    ? YIAN_TANK_SWING_SECONDS
    : kind === 'bird'
      ? BIRD_TANK_SWING_SECONDS
      : kind === 'cyber-dragon'
        ? CYBER_TANK_SWING_SECONDS
      : kind === 'bulldrome'
        ? BOSS_TANK_SWING_SECONDS
        : BULLFANGO_TANK_SWING_SECONDS
  return getAttackFrequency(kind, 'tankMelee', fallback)
}

export function getEnemyTankSwingDamage(kind: EnemyKind) {
  const fallback = kind === 'yian-kut-ku'
    ? YIAN_TANK_SWING_DAMAGE
    : kind === 'bird'
      ? BIRD_TANK_SWING_DAMAGE
      : kind === 'cyber-dragon'
        ? CYBER_TANK_SWING_DAMAGE
      : kind === 'bulldrome'
        ? BOSS_TANK_SWING_DAMAGE
        : BULLFANGO_TANK_SWING_DAMAGE
  return getEnemyAttackDamage(kind, 'tankMelee', fallback)
}

export function getEnemyChargeDamage(kind: EnemyKind) {
  if (kind === 'yian-kut-ku') return 0
  if (kind === 'bird') return getEnemyAttackDamage(kind, 'birdDive', BIRD_CHARGE_DAMAGE)
  if (kind === 'cyber-dragon') return getEnemyAttackDamage(kind, 'circularCharge', CYBER_CHARGE_DAMAGE)
  const fallback = kind === 'bulldrome' ? BOSS_CHARGE_DAMAGE : BULLFANGO_CHARGE_DAMAGE
  return getEnemyAttackDamage(kind, 'charge', fallback)
}

export function getEnemyBodyDamage(kind: EnemyKind) {
  const fallback = kind === 'yian-kut-ku'
    ? YIAN_BODY_DAMAGE
    : kind === 'bird'
      ? BIRD_BODY_DAMAGE
      : kind === 'cyber-dragon'
        ? CYBER_BODY_DAMAGE
      : kind === 'bulldrome'
        ? BOSS_BODY_DAMAGE
        : BULLFANGO_BODY_DAMAGE
  return getEnemyAttackDamage(kind, 'bodyContact', fallback)
}

export function getEnemyTrackSeconds(kind: EnemyKind) {
  if (kind === 'yian-kut-ku') return getAttackFrequency(kind, 'fireballVolley', YIAN_TRACK_SECONDS)
  if (kind === 'bird') return getAttackFrequency(kind, 'birdDive', BIRD_FLIGHT_TIMER_SECONDS)
  if (kind === 'cyber-dragon') return CYBER_TRACK_SECONDS
  const fallback = kind === 'bulldrome' ? BOSS_TRACK_SECONDS : BULLFANGO_TRACK_SECONDS
  return getAttackFrequency(kind, 'charge', fallback)
}

function getAttackNumber(
  kind: EnemyKind,
  attackKind: ActionAttackKind,
  field: 'windupSeconds' | 'recoverSeconds' | 'speed',
  fallback: number,
) {
  return getActionAttack(kind, attackKind)?.[field] ?? fallback
}

function getAttackFrequency(kind: EnemyKind, attackKind: ActionAttackKind, fallback: number) {
  return getActionAttack(kind, attackKind)?.frequencySeconds ?? fallback
}

function getEnemyAttackDamage(kind: EnemyKind, attackKind: ActionAttackKind, fallback: number) {
  const attack = getActionAttack(kind, attackKind)
  if (!attack) return fallback
  return attack.enabled ? attack.damage : 0
}
