import 'package:flutter/material.dart';
import 'package:service_api/service_api.dart';

/// 문제 유형 (무엇이 다른가?)
enum FindDiffType {
  color,  // 색상이 다름 (빨강 vs 핑크)
  icon,   // 모양이 다름 (😀 vs 😃)
  rotate, // 각도가 다름 (↑ vs ↗)
}

/// 개별 아이템 데이터
class FindDiffItem {
  final int id;
  final IconData icon;
  final Color color;
  final double angle; // 라디안 (0.0 ~ 2*pi)
  final bool isTarget; // 정답 여부 (다른 그림)

  FindDiffItem({
    required this.id,
    required this.icon,
    required this.color,
    this.angle = 0.0,
    this.isTarget = false,
  });
}

/// 난이도 정의
class FindDiffDifficulty extends GameDifficulty {
  final int levelIndex;
  final int rows; // 격자 행
  final int cols; // 격자 열
  final int timeLimitSeconds; // 제한 시간
  final FindDiffType diffType; // 문제 유형

  const FindDiffDifficulty({
    required this.levelIndex,
    required super.name,
    required super.contextId,
    required this.rows,
    required this.cols,
    required this.timeLimitSeconds,
    required this.diffType,
  });

  int get totalItems => rows * cols;
}

class FindDiffDifficulties {
  // --- 아이콘 풀 (모양 찾기용) ---
  // 서로 비슷하게 생긴 아이콘 쌍 (정답 vs 오답)
  static const List<List<IconData>> iconPairs = [
    [Icons.sentiment_satisfied_alt, Icons.sentiment_satisfied], // 웃음 vs 미소
    [Icons.star, Icons.star_border], // 별 vs 빈별
    [Icons.check_circle, Icons.check_circle_outline], // 체크 vs 빈체크
    [Icons.favorite, Icons.favorite_border], // 하트 vs 빈하트
    [Icons.lock, Icons.lock_open], // 잠금 vs 열림
    [Icons.volume_up, Icons.volume_down], // 소리 큼 vs 작음
    [Icons.battery_full, Icons.battery_alert], // 배터리 가득 vs 경고
    [Icons.signal_wifi_4_bar, Icons.signal_wifi_off], // 와이파이 vs 꺼짐
    [Icons.brightness_5, Icons.brightness_4], // 해 vs 달
    [Icons.directions_walk, Icons.directions_run], // 걷기 vs 뛰기
  ];

  // --- 단일 아이콘 풀 (색상/회전 찾기용) ---
  static const List<IconData> basicIcons = [
    Icons.circle, Icons.square, Icons.star, Icons.favorite, Icons.change_history,
    Icons.hexagon, Icons.pentagon, Icons.emoji_emotions, Icons.pets, Icons.flight,
  ];

  // [15단계 난이도 구성]
  static final List<FindDiffDifficulty> allDifficulties = [
    // --- Phase 1: 색상 구분 (초급) ---
    // 색상 차이가 뚜렷함 -> 미세함
    const FindDiffDifficulty(levelIndex: 1, name: 'Lv. 1: 색상 (2x2)', contextId: 'DIFF_L1_COLOR_2x2', rows: 2, cols: 2, timeLimitSeconds: 10, diffType: FindDiffType.color),
    const FindDiffDifficulty(levelIndex: 2, name: 'Lv. 2: 색상 (3x3)', contextId: 'DIFF_L2_COLOR_3x3', rows: 3, cols: 3, timeLimitSeconds: 10, diffType: FindDiffType.color),
    const FindDiffDifficulty(levelIndex: 3, name: 'Lv. 3: 색상 (4x4)', contextId: 'DIFF_L3_COLOR_4x4', rows: 4, cols: 4, timeLimitSeconds: 8, diffType: FindDiffType.color),
    const FindDiffDifficulty(levelIndex: 4, name: 'Lv. 4: 미세 색상 (4x4)', contextId: 'DIFF_L4_COLOR_HARD', rows: 4, cols: 4, timeLimitSeconds: 6, diffType: FindDiffType.color),

    // --- Phase 2: 모양 구분 (중급) ---
    // 비슷한 아이콘 찾기
    const FindDiffDifficulty(levelIndex: 5, name: 'Lv. 5: 모양 (3x3)', contextId: 'DIFF_L5_ICON_3x3', rows: 3, cols: 3, timeLimitSeconds: 10, diffType: FindDiffType.icon),
    const FindDiffDifficulty(levelIndex: 6, name: 'Lv. 6: 모양 (4x4)', contextId: 'DIFF_L6_ICON_4x4', rows: 4, cols: 4, timeLimitSeconds: 8, diffType: FindDiffType.icon),
    const FindDiffDifficulty(levelIndex: 7, name: 'Lv. 7: 모양 (5x5)', contextId: 'DIFF_L7_ICON_5x5', rows: 5, cols: 5, timeLimitSeconds: 8, diffType: FindDiffType.icon),
    const FindDiffDifficulty(levelIndex: 8, name: 'Lv. 8: 모양 (6x6)', contextId: 'DIFF_L8_ICON_6x6', rows: 6, cols: 6, timeLimitSeconds: 8, diffType: FindDiffType.icon),

    // --- Phase 3: 회전 구분 (상급) ---
    // 같은 아이콘인데 각도가 다름
    const FindDiffDifficulty(levelIndex: 9, name: 'Lv. 9: 회전 (4x4)', contextId: 'DIFF_L9_ROT_4x4', rows: 4, cols: 4, timeLimitSeconds: 8, diffType: FindDiffType.rotate),
    const FindDiffDifficulty(levelIndex: 10, name: 'Lv. 10: 회전 (5x5)', contextId: 'DIFF_L10_ROT_5x5', rows: 5, cols: 5, timeLimitSeconds: 7, diffType: FindDiffType.rotate),
    const FindDiffDifficulty(levelIndex: 11, name: 'Lv. 11: 회전 (6x6)', contextId: 'DIFF_L11_ROT_6x6', rows: 6, cols: 6, timeLimitSeconds: 6, diffType: FindDiffType.rotate),

    // --- Phase 4: 마스터 (대형 그리드 + 짧은 시간) ---
    const FindDiffDifficulty(levelIndex: 12, name: 'Lv. 12: 마스터 (색상 7x7)', contextId: 'DIFF_L12_COLOR_7x7', rows: 7, cols: 7, timeLimitSeconds: 5, diffType: FindDiffType.color),
    const FindDiffDifficulty(levelIndex: 13, name: 'Lv. 13: 마스터 (모양 7x7)', contextId: 'DIFF_L13_ICON_7x7', rows: 7, cols: 7, timeLimitSeconds: 5, diffType: FindDiffType.icon),
    const FindDiffDifficulty(levelIndex: 14, name: 'Lv. 14: 마스터 (회전 7x7)', contextId: 'DIFF_L14_ROT_7x7', rows: 7, cols: 7, timeLimitSeconds: 5, diffType: FindDiffType.rotate),
    const FindDiffDifficulty(levelIndex: 15, name: 'Lv. 15: 갓모드 (8x8)', contextId: 'DIFF_L15_GOD_8x8', rows: 8, cols: 8, timeLimitSeconds: 4, diffType: FindDiffType.rotate),
  ];

  static FindDiffDifficulty getLevel(int levelIndex) {
    if (levelIndex < 1) levelIndex = 1;
    if (levelIndex > allDifficulties.length) levelIndex = allDifficulties.length;
    return allDifficulties.firstWhere((level) => level.levelIndex == levelIndex,
        orElse: () => allDifficulties[0]);
  }
}