import 'package:askiineverdie/src/core/animation/ascii_animation_data.dart';
import 'package:askiineverdie/src/core/animation/background_data.dart';
import 'package:askiineverdie/src/core/animation/background_layer.dart';
import 'package:askiineverdie/src/core/animation/canvas/ascii_cell.dart';
import 'package:askiineverdie/src/core/animation/canvas/ascii_layer.dart';
import 'package:askiineverdie/src/core/animation/character_frames.dart';
import 'package:askiineverdie/src/core/animation/monster_size.dart';
import 'package:askiineverdie/src/core/animation/race_character_frames.dart';
import 'package:askiineverdie/src/core/animation/weapon_category.dart';
import 'package:askiineverdie/src/core/animation/weapon_effects.dart';

/// Canvas용 전투 프레임 합성기
///
/// 기존 BattleComposer의 로직을 레이어 기반으로 변환.
/// 출력: `List<AsciiLayer>` (z-order 정렬됨)
class CanvasBattleComposer {
  const CanvasBattleComposer({
    required this.weaponCategory,
    required this.hasShield,
    required this.monsterCategory,
    required this.monsterSize,
    this.raceId,
  });

  final WeaponCategory weaponCategory;
  final bool hasShield;
  final MonsterCategory monsterCategory;
  final MonsterSize monsterSize;

  /// 종족 ID (Phase 4: 종족별 캐릭터 애니메이션)
  final String? raceId;

  /// 프레임 상수
  static const int frameWidth = 60;
  static const int frameHeight = 8;
  static const int monsterWidth = 18;

  /// 레이어 기반 프레임 생성
  List<AsciiLayer> composeLayers(
    BattlePhase phase,
    int subFrame,
    String? monsterBaseName,
    EnvironmentType environment,
    int globalTick, {
    AttackerType attacker = AttackerType.none,
  }) {
    final layers = <AsciiLayer>[
      _createBackgroundLayer(environment, globalTick),
      _createCharacterLayer(phase, subFrame, attacker),
      _createMonsterLayer(phase, subFrame, attacker),
    ];

    // 이펙트 레이어 (준비/공격/히트 페이즈에서, 공격자 있을 때)
    if ((phase == BattlePhase.prepare ||
            phase == BattlePhase.attack ||
            phase == BattlePhase.hit) &&
        attacker != AttackerType.none) {
      final effectLayer = _createEffectLayer(phase, subFrame, attacker);
      if (effectLayer != null) {
        layers.add(effectLayer);
      }
    }

    // z-order 정렬
    layers.sort((a, b) => a.zIndex.compareTo(b.zIndex));

    return layers;
  }

  /// 배경 레이어 생성 (z=0)
  AsciiLayer _createBackgroundLayer(
    EnvironmentType environment,
    int globalTick,
  ) {
    final cells = List.generate(
      frameHeight,
      (_) => List.filled(frameWidth, AsciiCell.empty),
    );

    final bgLayers = getBackgroundLayers(environment);
    for (final layer in bgLayers) {
      // 스크롤 오프셋 계산
      final offset = (globalTick * layer.scrollSpeed).toInt();

      for (var i = 0; i < layer.lines.length; i++) {
        final y = layer.yStart + i;
        if (y >= frameHeight) break;

        final pattern = layer.lines[i];
        if (pattern.isEmpty) continue;

        for (var x = 0; x < frameWidth; x++) {
          final patternIdx = (x + offset) % pattern.length;
          final char = pattern[patternIdx];
          if (char != ' ') {
            cells[y][x] = AsciiCell.fromChar(char);
          }
        }
      }
    }

    // 배경 레이어는 50% 투명으로 캐릭터 부각
    return AsciiLayer(cells: cells, zIndex: 0, opacity: 0.5);
  }

  /// 캐릭터 레이어 생성 (z=1)
  ///
  /// Phase 4: 종족별 캐릭터 프레임 지원
  /// Phase 7: 공격자별 위치 분리 (플레이어가 공격자일 때만 이동)
  AsciiLayer _createCharacterLayer(
    BattlePhase phase,
    int subFrame,
    AttackerType attacker,
  ) {
    CharacterFrame charFrame;

    // 종족 ID가 있으면 종족별 프레임 사용
    if (raceId != null && raceId!.isNotEmpty) {
      final raceData = RaceCharacterFrames.get(raceId!);
      if (raceData != null) {
        final frames = raceData.getFrames(phase);
        charFrame = frames[subFrame % frames.length];
      } else {
        // 종족 데이터 없으면 기본 프레임 사용
        charFrame = getCharacterFrame(phase, subFrame);
      }
    } else {
      charFrame = getCharacterFrame(phase, subFrame);
    }

    if (hasShield) {
      charFrame = charFrame.withShield();
    }

    // 플레이어가 공격자인지 확인
    final isPlayerAttacking =
        attacker == AttackerType.player || attacker == AttackerType.both;

    // 페이즈별 X 위치 (Phase 7: 공격자별 위치 분리)
    // 플레이어가 공격자일 때만 이동, 아니면 제자리(12)
    // hit 페이즈에서도 플레이어 공격 중이면 위치 유지 (Bug fix)
    final charX = switch (phase) {
      BattlePhase.idle => 12,
      BattlePhase.prepare => isPlayerAttacking ? 15 : 12,
      BattlePhase.attack => isPlayerAttacking ? 18 : 12,
      BattlePhase.hit => isPlayerAttacking ? 18 : 12,
      BattlePhase.recover => isPlayerAttacking ? 15 : 12,
    };

    final cells = _spriteToCells(charFrame.lines);
    // 바닥 레이어(Y=7) 위에 서있도록
    final charY = frameHeight - cells.length - 1;

    return AsciiLayer(
      cells: cells,
      zIndex: 2, // 몬스터(z=1) 위에 캐릭터 표시
      offsetX: charX,
      offsetY: charY,
    );
  }

  /// 몬스터 레이어 생성 (z=1, 캐릭터보다 뒤)
  ///
  /// Phase 7: 공격자별 위치 분리 (몬스터가 공격자일 때만 이동)
  AsciiLayer _createMonsterLayer(
    BattlePhase phase,
    int subFrame,
    AttackerType attacker,
  ) {
    final monsterFrames = _getAnimatedMonsterFrames(
      monsterCategory,
      monsterSize,
      phase,
    );
    final monsterFrame = monsterFrames[subFrame % monsterFrames.length];

    // 몬스터 스프라이트를 오른쪽 정렬하여 셀로 변환
    final cells = _spriteToRightAlignedCells(monsterFrame, monsterWidth);

    // 몬스터가 공격자인지 확인
    final isMonsterAttacking =
        attacker == AttackerType.monster || attacker == AttackerType.both;

    // 페이즈별 X 위치 (Phase 7: 공격자별 위치 분리)
    // 몬스터가 공격자일 때만 이동, 아니면 제자리(48)
    // attack 페이즈에서도 몬스터 공격 중이면 위치 유지 (Bug fix)
    final monsterRightEdge = switch (phase) {
      BattlePhase.idle => 48,
      BattlePhase.prepare => isMonsterAttacking ? 45 : 48,
      BattlePhase.attack => isMonsterAttacking ? 42 : 48,
      BattlePhase.hit => isMonsterAttacking ? 42 : 48,
      BattlePhase.recover => isMonsterAttacking ? 45 : 48,
    };
    final monsterX = monsterRightEdge - monsterWidth;

    // 바닥 레이어(Y=7) 위에 서있도록
    final monsterY = frameHeight - cells.length - 1;

    return AsciiLayer(
      cells: cells,
      zIndex: 1,
      offsetX: monsterX,
      offsetY: monsterY,
    );
  }

  /// 이펙트 레이어 생성 (z=3, 캐릭터/몬스터 위에 표시)
  ///
  /// Phase 8: 공격자에 따라 이펙트 위치/모양 분리
  /// - 플레이어 공격: 몬스터 왼쪽에 무기 이펙트 (→ 방향)
  /// - 몬스터 공격: 캐릭터 오른쪽에 공격 이펙트 (← 방향)
  AsciiLayer? _createEffectLayer(
    BattlePhase phase,
    int subFrame,
    AttackerType attacker,
  ) {
    // 공격자별 위치 계산을 위한 플래그 (동일 로직 재사용)
    final isPlayerAttacking =
        attacker == AttackerType.player || attacker == AttackerType.both;
    final isMonsterAttacking =
        attacker == AttackerType.monster || attacker == AttackerType.both;

    // 캐릭터 위치 계산 (characterLayer와 동일 로직)
    const charWidth = 6;
    final charX = switch (phase) {
      BattlePhase.idle => 12,
      BattlePhase.prepare => isPlayerAttacking ? 15 : 12,
      BattlePhase.attack => isPlayerAttacking ? 18 : 12,
      BattlePhase.hit => isPlayerAttacking ? 18 : 12,
      BattlePhase.recover => isPlayerAttacking ? 15 : 12,
    };

    // 몬스터 위치 계산 (monsterLayer와 동일 로직)
    final monsterRightEdge = switch (phase) {
      BattlePhase.idle => 48,
      BattlePhase.prepare => isMonsterAttacking ? 45 : 48,
      BattlePhase.attack => isMonsterAttacking ? 42 : 48,
      BattlePhase.hit => isMonsterAttacking ? 42 : 48,
      BattlePhase.recover => isMonsterAttacking ? 45 : 48,
    };
    final monsterX = monsterRightEdge - monsterWidth;

    // 공격자에 따라 다른 이펙트 사용
    final List<String> effectLines;
    final int effectX;

    if (attacker == AttackerType.player) {
      // 플레이어 공격: 무기 이펙트 → 몬스터 왼쪽 바로 옆
      final effect = getWeaponEffect(weaponCategory);
      effectLines = _getEffectLines(effect, phase, subFrame);
      effectX = monsterX - 2;
    } else if (attacker == AttackerType.monster) {
      // 몬스터 공격: 왼쪽 방향 이펙트 → 캐릭터 오른쪽 바로 옆
      effectLines = _getMonsterAttackEffect(phase, subFrame);
      effectX = charX + charWidth;
    } else {
      // 동시 공격(both): 무기 이펙트 두 캐릭터 중앙
      final effect = getWeaponEffect(weaponCategory);
      effectLines = _getEffectLines(effect, phase, subFrame);
      effectX = (charX + charWidth + monsterX) ~/ 2;
    }

    if (effectLines.isEmpty) return null;

    final cells = _spriteToCells(effectLines);

    // 이펙트 높이에 따른 동적 Y 위치 (캔버스 하단 기준)
    final effectHeight = effectLines.length;
    final effectY = frameHeight - effectHeight - 1;

    return AsciiLayer(
      cells: cells,
      zIndex: 3,
      offsetX: effectX,
      offsetY: effectY,
    );
  }

  /// 몬스터 공격 이펙트 (← 방향)
  List<String> _getMonsterAttackEffect(BattlePhase phase, int subFrame) {
    return switch (phase) {
      BattlePhase.prepare => _monsterPrepareFrames[subFrame % _monsterPrepareFrames.length],
      BattlePhase.attack => _monsterAttackFrames[subFrame % _monsterAttackFrames.length],
      BattlePhase.hit => _monsterHitFrames[subFrame % _monsterHitFrames.length],
      _ => <String>[],
    };
  }

  /// 문자열 스프라이트를 AsciiCell 2D 배열로 변환
  List<List<AsciiCell>> _spriteToCells(List<String> lines) {
    return lines.map((line) {
      return line.split('').map(AsciiCell.fromChar).toList();
    }).toList();
  }

  /// 문자열 스프라이트를 오른쪽 정렬된 AsciiCell 2D 배열로 변환
  List<List<AsciiCell>> _spriteToRightAlignedCells(
    List<String> lines,
    int width,
  ) {
    // 전체 스프라이트에서 최대 너비 계산
    int maxWidth = 0;
    for (final line in lines) {
      final trimmed = line.trimRight().length;
      if (trimmed > maxWidth) maxWidth = trimmed;
    }

    // 왼쪽 패딩 계산
    final leftPadding = width - maxWidth;

    return lines.map((line) {
      final trimmed = line.trimRight();
      final cells = <AsciiCell>[];

      // 왼쪽 패딩
      for (var i = 0; i < leftPadding; i++) {
        cells.add(AsciiCell.empty);
      }

      // 실제 문자
      for (var i = 0; i < trimmed.length; i++) {
        cells.add(AsciiCell.fromChar(trimmed[i]));
      }

      // 오른쪽 채우기 (필요시)
      while (cells.length < width) {
        cells.add(AsciiCell.empty);
      }

      return cells;
    }).toList();
  }

  /// 멀티라인 이펙트 프레임 반환
  List<String> _getEffectLines(
    WeaponEffect effect,
    BattlePhase phase,
    int subFrame,
  ) {
    final frames = switch (phase) {
      BattlePhase.idle => <List<String>>[],
      BattlePhase.prepare => effect.prepareFrames,
      BattlePhase.attack => effect.attackFrames,
      BattlePhase.hit => effect.hitFrames,
      BattlePhase.recover => <List<String>>[],
    };
    if (frames.isEmpty) return [];
    return frames[subFrame % frames.length];
  }
}

// ============================================================================
// 몬스터 애니메이션 프레임 (기존 BattleComposer에서 이식)
// ============================================================================

/// 몬스터 애니메이션 프레임 반환 (페이즈별 다른 동작)
List<List<String>> _getAnimatedMonsterFrames(
  MonsterCategory category,
  MonsterSize size,
  BattlePhase phase,
) {
  // 피격 상태
  if (phase == BattlePhase.hit) {
    return _getMonsterHitFrames(category, size);
  }
  // 경계 상태 (prepare, attack)
  if (phase == BattlePhase.prepare || phase == BattlePhase.attack) {
    return _getMonsterAlertFrames(category, size);
  }
  // 일반 상태 (idle, recover)
  return _getMonsterIdleFrames(category, size);
}

List<List<String>> _getMonsterIdleFrames(
  MonsterCategory category,
  MonsterSize size,
) {
  return switch (size) {
    MonsterSize.tiny => _tinyIdleFrames(category),
    MonsterSize.small => _smallIdleFrames(category),
    MonsterSize.medium => _mediumIdleFrames(category),
    // large, huge, giant, titanic 모두 8줄 (large 프레임 사용)
    MonsterSize.large ||
    MonsterSize.huge ||
    MonsterSize.giant ||
    MonsterSize.titanic => _largeIdleFrames(category),
  };
}

List<List<String>> _getMonsterHitFrames(
  MonsterCategory category,
  MonsterSize size,
) {
  return switch (size) {
    MonsterSize.tiny => _tinyHitFrames(category),
    MonsterSize.small => _smallHitFrames(category),
    MonsterSize.medium => _mediumHitFrames(category),
    MonsterSize.large ||
    MonsterSize.huge ||
    MonsterSize.giant ||
    MonsterSize.titanic => _largeHitFrames(category),
  };
}

List<List<String>> _getMonsterAlertFrames(
  MonsterCategory category,
  MonsterSize size,
) {
  return switch (size) {
    MonsterSize.tiny => _tinyAlertFrames(category),
    MonsterSize.small => _smallAlertFrames(category),
    MonsterSize.medium => _mediumAlertFrames(category),
    MonsterSize.large ||
    MonsterSize.huge ||
    MonsterSize.giant ||
    MonsterSize.titanic => _largeAlertFrames(category),
  };
}

// ============================================================================
// Tiny 몬스터 (2줄)
// ============================================================================

List<List<String>> _tinyIdleFrames(MonsterCategory category) {
  return switch (category) {
    MonsterCategory.bug => [
      [r'(o.o)', r' |_|'],
      [r'(o o)', r' |_|'],
    ],
    MonsterCategory.malware => [
      [r'<o_o>', r' \_/'],
      [r'<o-o>', r' /_\'],
    ],
    MonsterCategory.network => [
      [r' @', r'/|\'],
      [r' @', r'\|/'],
    ],
    MonsterCategory.system => [
      [r'[x_x]', r' /_\'],
      [r'[X_X]', r' \_/'],
    ],
    MonsterCategory.crypto => [
      [r'$(.)~', r' /_\'],
      [r'$(.)`', r' \_/'],
    ],
    MonsterCategory.ai => [
      [r'{o}', r'/_\'],
      [r'{O}', r'\_/'],
    ],
    MonsterCategory.boss => [
      [r'(|o|)', r' V V'],
      [r'(|O|)', r' v v'],
    ],
  };
}

List<List<String>> _tinyHitFrames(MonsterCategory category) {
  return [
    [r'(*!*)', r' X_X'],
    [r'(!*!)', r' x_x'],
  ];
}

List<List<String>> _tinyAlertFrames(MonsterCategory category) {
  return switch (category) {
    MonsterCategory.bug => [
      [r'(O!O)', r' |!|'],
      [r'(!O!)', r' |!|'],
    ],
    MonsterCategory.malware => [
      [r'<!_!>', r' \!/'],
      [r'<!-!>', r' /!\'],
    ],
    MonsterCategory.network => [
      [r' @!', r'/|\'],
      [r' !@', r'\|/'],
    ],
    MonsterCategory.system => [
      [r'[!_!]', r' /!\'],
      [r'[!_!]', r' \!/'],
    ],
    MonsterCategory.crypto => [
      [r'$(!)~', r' /!\'],
      [r'$(!)`', r' \!/'],
    ],
    MonsterCategory.ai => [
      [r'{!}', r'/!\'],
      [r'{!}', r'\!/'],
    ],
    MonsterCategory.boss => [
      [r'(|!|)', r' V!V'],
      [r'(|!|)', r' v!v'],
    ],
  };
}

// ============================================================================
// Small 몬스터 (4줄)
// ============================================================================

List<List<String>> _smallIdleFrames(MonsterCategory category) {
  return switch (category) {
    MonsterCategory.bug => [
      [r' /\_/\', r'( O.O )', r' > ^ <', r' /| |\'],
      [r' /\_/\', r'( O O )', r' > v <', r' \| |/'],
    ],
    MonsterCategory.malware => [
      [r'  /\/\', r' (O  O)', r' /    \', r' \/  \/'],
      [r'  \/\/\', r' (O  O)', r' \    /', r' /\  /\'],
    ],
    MonsterCategory.network => [
      [r'   O', r'  /|\', r'  / \', r' _| |_'],
      [r'   O', r'  \|/', r'  | |', r' _/ \_'],
    ],
    MonsterCategory.system => [
      [r'  _+_', r' (x_x)', r'  /|\', r' _/ \_'],
      [r'  _+_', r' (X_X)', r'  \|/', r' _| |_'],
    ],
    MonsterCategory.crypto => [
      [r'   __', r' <(oo)~', r'  /  \', r' <_  _>'],
      [r'   __', r'  (oo)>', r'  \  /', r' <_  _>'],
    ],
    MonsterCategory.ai => [
      [r'  ___', r' (   )', r' (   )', r'  \_/'],
      [r'   _', r'  / \', r' {   }', r'  \_/'],
    ],
    MonsterCategory.boss => [
      [r'  ^w^', r' (|o|)', r'  /|\', r'  V V'],
      [r'  ^W^', r' (|O|)', r'  \|/', r'  v v'],
    ],
  };
}

List<List<String>> _smallHitFrames(MonsterCategory category) {
  return [
    [r'  *!*', r' (>_<)', r'  \X/', r' _/_\_'],
    [r' !*!', r' (@_@)', r'  /X\', r' _\_/_'],
  ];
}

List<List<String>> _smallAlertFrames(MonsterCategory category) {
  return switch (category) {
    MonsterCategory.bug => [
      [r' /\_/\', r'( O!O )', r' > ! <', r' /| |\'],
      [r' /\_/\', r'( !O! )', r' > ! <', r' \| |/'],
    ],
    MonsterCategory.malware => [
      [r'  /\/\', r' (!  !)', r' /    \', r' \/  \/'],
      [r'  \/\/\', r' (!  !)', r' \    /', r' /\  /\'],
    ],
    MonsterCategory.network => [
      [r'   O!', r'  /|\', r'  / \', r' _| |_'],
      [r'   !O', r'  \|/', r'  | |', r' _/ \_'],
    ],
    MonsterCategory.system => [
      [r'  _!_', r' (!_!)', r'  /|\', r' _/ \_'],
      [r'  _!_', r' (!_!)', r'  \|/', r' _| |_'],
    ],
    MonsterCategory.crypto => [
      [r'   __', r' <(!!)~', r'  /  \', r' <_  _>'],
      [r'   __', r'  (!!)>', r'  \  /', r' <_  _>'],
    ],
    MonsterCategory.ai => [
      [r'  ___', r' ( ! )', r' ( ! )', r'  \_/'],
      [r'   _', r'  /!\', r' { ! }', r'  \_/'],
    ],
    MonsterCategory.boss => [
      [r'  ^!^', r' (|!|)', r'  /|\', r'  V V'],
      [r'  ^!^', r' (|!|)', r'  \|/', r'  v v'],
    ],
  };
}

// ============================================================================
// Medium 몬스터 (6줄)
// ============================================================================

List<List<String>> _mediumIdleFrames(MonsterCategory category) {
  return switch (category) {
    MonsterCategory.bug => [
      [
        r'  /\_/\',
        r' ( O.O )',
        r'  > ^ <',
        r' /|   |\',
        r' | |   | |',
        r'_|_|   |_|_',
      ],
      [
        r'  /\_/\',
        r' ( O O )',
        r'  > v <',
        r' \|   |/',
        r' | |   | |',
        r'_|_|   |_|_',
      ],
    ],
    MonsterCategory.malware => [
      [
        r'   /\/\',
        r'  /O  O\',
        r'  \    /',
        r'  /    \',
        r'  \/  \/',
        r' _/    \_',
      ],
      [
        r'   \/\/\',
        r'  \O  O/',
        r'  /    \',
        r'  \    /',
        r'  /\  /\',
        r' _\    /_',
      ],
    ],
    MonsterCategory.network => [
      [r'    O', r'   /|\', r'   / \', r'  |   |', r'  |   |', r' _|   |_'],
      [r'    O', r'   \|/', r'   | |', r'  |   |', r'  |   |', r' _/   \_'],
    ],
    MonsterCategory.system => [
      [r'   _+_', r'  (X_X)', r'   /|\', r'  / | \', r' |  |  |', r'_/  |  \_'],
      [r'   _x_', r'  (x_x)', r'   \|/', r'  \ | /', r' |  |  |', r'_\  |  /_'],
    ],
    MonsterCategory.crypto => [
      [
        r'    __',
        r'  <(OO)~',
        r'   /  \',
        r'  /    \',
        r' |      |',
        r'<__    __>',
      ],
      [
        r'    __',
        r'   (OO)>',
        r'   \  /',
        r'  \    /',
        r' |      |',
        r'<__    __>',
      ],
    ],
    MonsterCategory.ai => [
      [
        r'   ____',
        r'  /    \',
        r' (      )',
        r' (      )',
        r'  \    /',
        r'   \__/',
      ],
      [
        r'    __',
        r'   /  \',
        r'  /    \',
        r' {      }',
        r'  \    /',
        r'   \__/',
      ],
    ],
    MonsterCategory.boss => [
      [r'   ^W^', r'  (|O|)', r'   /|\', r'  / | \', r'  V   V', r' _/   \_'],
      [r'   ^w^', r'  (|o|)', r'   \|/', r'  \ | /', r'  v   v', r' _\   /_'],
    ],
  };
}

List<List<String>> _mediumHitFrames(MonsterCategory category) {
  return [
    [r'   *!*', r'  (>.<)', r'   \X/', r'  /   \', r' |     |', r'_/_   \_\'],
    [r'  !*!', r'  (@_@)', r'   /X\', r'  \   /', r' |     |', r'_\_   /_/'],
  ];
}

List<List<String>> _mediumAlertFrames(MonsterCategory category) {
  return switch (category) {
    MonsterCategory.bug => [
      [
        r'  /\_/\',
        r' ( O!O )',
        r'  > ! <',
        r' /|   |\',
        r' | |   | |',
        r'_|_|   |_|_',
      ],
      [
        r'  /\_/\',
        r' ( !O! )',
        r'  > ! <',
        r' \|   |/',
        r' | |   | |',
        r'_|_|   |_|_',
      ],
    ],
    MonsterCategory.malware => [
      [
        r'   /\/\',
        r'  /!  !\',
        r'  \    /',
        r'  /    \',
        r'  \/  \/',
        r' _/    \_',
      ],
      [
        r'   \/\/\',
        r'  \!  !/',
        r'  /    \',
        r'  \    /',
        r'  /\  /\',
        r' _\    /_',
      ],
    ],
    MonsterCategory.network => [
      [r'    O!', r'   /|\', r'   / \', r'  |   |', r'  |   |', r' _|   |_'],
      [r'    !O', r'   \|/', r'   | |', r'  |   |', r'  |   |', r' _/   \_'],
    ],
    MonsterCategory.system => [
      [r'   _!_', r'  (!_!)', r'   /|\', r'  / | \', r' |  |  |', r'_/  |  \_'],
      [r'   _!_', r'  (!_!)', r'   \|/', r'  \ | /', r' |  |  |', r'_\  |  /_'],
    ],
    MonsterCategory.crypto => [
      [
        r'    __',
        r'  <(!!)~',
        r'   /  \',
        r'  /    \',
        r' |      |',
        r'<__    __>',
      ],
      [
        r'    __',
        r'   (!!)>',
        r'   \  /',
        r'  \    /',
        r' |      |',
        r'<__    __>',
      ],
    ],
    MonsterCategory.ai => [
      [
        r'   ____',
        r'  / !  \',
        r' (   !  )',
        r' (  !   )',
        r'  \    /',
        r'   \__/',
      ],
      [
        r'    __',
        r'   / !\',
        r'  /  !  \',
        r' {   !   }',
        r'  \    /',
        r'   \__/',
      ],
    ],
    MonsterCategory.boss => [
      [r'   ^!^', r'  (|!|)', r'   /|\', r'  / | \', r'  V   V', r' _/   \_'],
      [r'   ^!^', r'  (|!|)', r'   \|/', r'  \ | /', r'  v   v', r' _\   /_'],
    ],
  };
}

// ============================================================================
// Large 몬스터 (8줄)
// ============================================================================

List<List<String>> _largeIdleFrames(MonsterCategory category) {
  return switch (category) {
    MonsterCategory.bug => [
      [
        r'   /\__/\',
        r'  ( O  O )',
        r'   > ^^ <',
        r'  /|    |\',
        r' | |    | |',
        r' | |    | |',
        r'_| |    | |_',
        r'|__|____|__|',
      ],
      [
        r'   /\__/\',
        r'  ( O  O )',
        r'   > vv <',
        r'  \|    |/',
        r' | |    | |',
        r' | |    | |',
        r'_| |    | |_',
        r'|__|____|__|',
      ],
    ],
    MonsterCategory.malware => [
      [
        r'    /\/\',
        r'   /O  O\',
        r'   \    /',
        r'   /    \',
        r'  /      \',
        r'  \/    \/',
        r' _/      \_',
        r'/__      __\\',
      ],
      [
        r'    \/\/\',
        r'   \O  O/',
        r'   /    \',
        r'   \    /',
        r'  \      /',
        r'  /\    /\',
        r' _\      /_',
        r'\__      __/',
      ],
    ],
    MonsterCategory.network => [
      [
        r'     O',
        r'    /|\',
        r'    / \',
        r'   |   |',
        r'   |   |',
        r'  |     |',
        r' _|     |_',
        r'|__     __|',
      ],
      [
        r'     O',
        r'    \|/',
        r'    | |',
        r'   |   |',
        r'   |   |',
        r'  |     |',
        r' _/     \_',
        r'/__     __\\',
      ],
    ],
    MonsterCategory.system => [
      [
        r'    _+_',
        r'   (X_X)',
        r'    /|\',
        r'   / | \',
        r'  |  |  |',
        r'  |  |  |',
        r' _/  |  \_',
        r'|____|____|',
      ],
      [
        r'    _x_',
        r'   (x_x)',
        r'    \|/',
        r'   \ | /',
        r'  |  |  |',
        r'  |  |  |',
        r' _\  |  /_',
        r'|____|____|',
      ],
    ],
    MonsterCategory.crypto => [
      [
        r'     ___',
        r'   <(O O)~',
        r'    /   \',
        r'   /     \',
        r'  |       |',
        r'  |       |',
        r' <__     __>',
        r'|___     ___|',
      ],
      [
        r'     ___',
        r'    (O O)>',
        r'    \   /',
        r'   \     /',
        r'  |       |',
        r'  |       |',
        r' <__     __>',
        r'|___     ___|',
      ],
    ],
    MonsterCategory.ai => [
      [
        r'    _____',
        r'   /     \',
        r'  (       )',
        r'  (       )',
        r'  (       )',
        r'   \     /',
        r'    \   /',
        r'     \_/',
      ],
      [
        r'     ___',
        r'    /   \',
        r'   /     \',
        r'  {       }',
        r'  {       }',
        r'   \     /',
        r'    \   /',
        r'     \_/',
      ],
    ],
    MonsterCategory.boss => [
      [
        r'    ^W^',
        r'   /|O|\',
        r'    /|\',
        r'   / | \',
        r'  |  |  |',
        r'  V  |  V',
        r' _/  |  \_',
        r'|_____|_____|',
      ],
      [
        r'    ^w^',
        r'   \|o|/',
        r'    \|/',
        r'   \ | /',
        r'  |  |  |',
        r'  v  |  v',
        r' _\  |  /_',
        r'|_____|_____|',
      ],
    ],
  };
}

List<List<String>> _largeHitFrames(MonsterCategory category) {
  return [
    [
      r'    *!*',
      r'   (>.<)',
      r'    \X/',
      r'   / | \',
      r'  |  |  |',
      r'  X  |  X',
      r' _/  |  \_',
      r'|_____|_____|',
    ],
    [
      r'   !*!',
      r'   (@_@)',
      r'    /X\',
      r'   \ | /',
      r'  |  |  |',
      r'  x  |  x',
      r' _\  |  /_',
      r'|_____|_____|',
    ],
  ];
}

List<List<String>> _largeAlertFrames(MonsterCategory category) {
  return switch (category) {
    MonsterCategory.bug => [
      [
        r'   /\__/\',
        r'  ( O!O  )',
        r'   > !! <',
        r'  /|    |\',
        r' | |    | |',
        r' | |    | |',
        r'_| |    | |_',
        r'|__|____|__|',
      ],
      [
        r'   /\__/\',
        r'  ( !O!  )',
        r'   > !! <',
        r'  \|    |/',
        r' | |    | |',
        r' | |    | |',
        r'_| |    | |_',
        r'|__|____|__|',
      ],
    ],
    MonsterCategory.malware => [
      [
        r'    /\/\',
        r'   /!  !\',
        r'   \    /',
        r'   /    \',
        r'  /      \',
        r'  \/    \/',
        r' _/      \_',
        r'/__      __\\',
      ],
      [
        r'    \/\/\',
        r'   \!  !/',
        r'   /    \',
        r'   \    /',
        r'  \      /',
        r'  /\    /\',
        r' _\      /_',
        r'\__      __/',
      ],
    ],
    MonsterCategory.network => [
      [
        r'     O!',
        r'    /|\',
        r'    / \',
        r'   |   |',
        r'   |   |',
        r'  |     |',
        r' _|     |_',
        r'|__     __|',
      ],
      [
        r'     !O',
        r'    \|/',
        r'    | |',
        r'   |   |',
        r'   |   |',
        r'  |     |',
        r' _/     \_',
        r'/__     __\\',
      ],
    ],
    MonsterCategory.system => [
      [
        r'    _!_',
        r'   (!_!)',
        r'    /|\',
        r'   / | \',
        r'  |  |  |',
        r'  |  |  |',
        r' _/  |  \_',
        r'|____|____|',
      ],
      [
        r'    _!_',
        r'   (!_!)',
        r'    \|/',
        r'   \ | /',
        r'  |  |  |',
        r'  |  |  |',
        r' _\  |  /_',
        r'|____|____|',
      ],
    ],
    MonsterCategory.crypto => [
      [
        r'     ___',
        r'   <(! !)~',
        r'    /   \',
        r'   /     \',
        r'  |       |',
        r'  |       |',
        r' <__     __>',
        r'|___     ___|',
      ],
      [
        r'     ___',
        r'    (! !)>',
        r'    \   /',
        r'   \     /',
        r'  |       |',
        r'  |       |',
        r' <__     __>',
        r'|___     ___|',
      ],
    ],
    MonsterCategory.ai => [
      [
        r'    _____',
        r'   /  !  \',
        r'  (   !   )',
        r'  (  !    )',
        r'  (   !   )',
        r'   \     /',
        r'    \   /',
        r'     \_/',
      ],
      [
        r'     ___',
        r'    / ! \',
        r'   /  !  \',
        r'  {   !   }',
        r'  {  !    }',
        r'   \     /',
        r'    \   /',
        r'     \_/',
      ],
    ],
    MonsterCategory.boss => [
      [
        r'    ^!^',
        r'   /|!|\',
        r'    /|\',
        r'   / | \',
        r'  |  |  |',
        r'  V  |  V',
        r' _/  |  \_',
        r'|_____|_____|',
      ],
      [
        r'     ^!^',
        r'    \|!|/',
        r'     \|/',
        r'    \ | /',
        r'   |  |  |',
        r'   v  |  v',
        r'  _\  |  /_',
        r'|_____|_____|',
      ],
    ],
  };
}

// ============================================================================
// 몬스터 공격 이펙트 (← 방향, Phase 8) - 5줄
// ============================================================================

/// 몬스터 공격 준비 프레임 (5줄)
const _monsterPrepareFrames = <List<String>>[
  [r'          ', r'          ', r'     <    ', r'          ', r'          '],
  [r'          ', r'      _   ', r'    <<    ', r'      -   ', r'          '],
];

/// 몬스터 공격 프레임 (5줄)
const _monsterAttackFrames = <List<String>>[
  [r'          ', r'     __   ', r'   <--    ', r'     --   ', r'          '],
  [r'          ', r'    ___   ', r'  <---    ', r'    ---   ', r'          '],
  [r'          ', r'   ____   ', r' <-----   ', r'   ----   ', r'          '],
];

/// 몬스터 히트 프레임 (5줄)
const _monsterHitFrames = <List<String>>[
  [r' *SLASH!* ', r'   ****   ', r' <-----   ', r'   ****   ', r'          '],
  [r'*ATTACK!* ', r'  ****    ', r'  <----   ', r'  ****    ', r'          '],
];
