// packages/feature_game_colormatch/lib/models/colormatch_models.dart

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

/// 스트룹 효과에 사용될 핵심 색상 및 이름 정의
class MatchColor {
  final String name; // 예: "빨강", "파랑"
  final Color color; // 예: Colors.red, Colors.blue

  const MatchColor({required this.name, required this.color});
}

/// 현재 라운드의 정답 목표를 정의 (인지 부하 증가)
enum MatchMode {
  matchColor, // 단어의 '텍스트 색상'을 맞춰야 함
  matchWord,  // 단어가 '표현하는 의미'를 맞춰야 함
  matchBackground, // 단어의 '배경색'을 맞춰야 함
}

/// 게임 난이도 및 규칙 정의
class ColorMatchDifficulty extends GameDifficulty {
  final int levelIndex;
  final int totalRounds; // 총 라운드 수
  final int timeLimitSeconds; // 라운드당 시간 제한
  final bool enableStroopEffect;
  final List<MatchColor> colorSet;

  ColorMatchDifficulty({
    required this.levelIndex,
    required super.name,
    required super.contextId,
    required this.totalRounds,
    required this.timeLimitSeconds,
    required this.enableStroopEffect,
    required this.colorSet,
  });
}

class ColorMatchDifficulties {
  // 10가지 색상 세트 정의
  static final List<MatchColor> baseColors = [
    const MatchColor(name: "빨강", color: Colors.red),
    const MatchColor(name: "파랑", color: Colors.blue),
    const MatchColor(name: "노랑", color: Colors.yellow),
    const MatchColor(name: "초록", color: Colors.green),
    const MatchColor(name: "보라", color: Colors.purple),
    const MatchColor(name: "주황", color: Colors.orange),
    const MatchColor(name: "분홍", color: Colors.pink),
    const MatchColor(name: "갈색", color: Colors.brown),
    const MatchColor(name: "하늘", color: Colors.cyan),
    const MatchColor(name: "회색", color: Colors.grey),
  ];
  
  // [🔥 수정] 난이도 목록 (시간 증가 적용)
  static final List<ColorMatchDifficulty> allDifficulties = [
    // --- Lv 1-4: 기본 색상 매칭 (시간 증가) ---
    ColorMatchDifficulty( levelIndex: 1, name: 'Lv. 1: 초급 (2색)', contextId: 'MATCH_L1_BASE_2', totalRounds: 15, timeLimitSeconds: 6, enableStroopEffect: false, colorSet: baseColors.sublist(0, 2)),
    ColorMatchDifficulty( levelIndex: 2, name: 'Lv. 2: 기본 (4색)', contextId: 'MATCH_L2_BASE_4', totalRounds: 20, timeLimitSeconds: 5, enableStroopEffect: false, colorSet: baseColors.sublist(0, 4)),
    ColorMatchDifficulty( levelIndex: 3, name: 'Lv. 3: 숙련 (6색)', contextId: 'MATCH_L3_BASE_6', totalRounds: 25, timeLimitSeconds: 4, enableStroopEffect: false, colorSet: baseColors.sublist(0, 6)),
    ColorMatchDifficulty( levelIndex: 4, name: 'Lv. 4: 전문가 (8색)', contextId: 'MATCH_L4_BASE_8', totalRounds: 30, timeLimitSeconds: 3, enableStroopEffect: false, colorSet: baseColors.sublist(0, 8)),
    // --- Lv 5-6: 스트룹 시작 ---
    ColorMatchDifficulty( levelIndex: 5, name: 'Lv. 5: 스트룹 시작 (4색)', contextId: 'MATCH_L5_STROOP_4', totalRounds: 20, timeLimitSeconds: 5, enableStroopEffect: true, colorSet: baseColors.sublist(0, 4)),
    ColorMatchDifficulty( levelIndex: 6, name: 'Lv. 6: 스트룹 중급 (6색)', contextId: 'MATCH_L6_STROOP_6', totalRounds: 30, timeLimitSeconds: 4, enableStroopEffect: true, colorSet: baseColors.sublist(0, 6)),
    
    // --- Lv 7-9: 3가지 목표 중 무작위 선택 (배경색 도입) ---
    ColorMatchDifficulty( levelIndex: 7, name: 'Lv. 7: 3중 인지 (6색)', contextId: 'MATCH_L7_TRIPLE_6', totalRounds: 30, timeLimitSeconds: 4, enableStroopEffect: true, colorSet: baseColors.sublist(0, 6)),
    ColorMatchDifficulty( levelIndex: 8, name: 'Lv. 8: 3중 인지 (8색)', contextId: 'MATCH_L8_TRIPLE_8', totalRounds: 40, timeLimitSeconds: 3, enableStroopEffect: true, colorSet: baseColors.sublist(0, 8)),
    ColorMatchDifficulty( levelIndex: 9, name: 'Lv. 9: 3중 인지 (10색)', contextId: 'MATCH_L9_TRIPLE_10', totalRounds: 50, timeLimitSeconds: 3, enableStroopEffect: true, colorSet: baseColors.sublist(0, 10)),
    
    // --- Lv 10-12: 최상 난이도 (배경색 + 초고속, 시간 증가) ---
    ColorMatchDifficulty( levelIndex: 10, name: 'Lv. 10: 마스터 (10색 초고속)', contextId: 'MATCH_L10_MASTER', totalRounds: 50, timeLimitSeconds: 2, enableStroopEffect: true, colorSet: baseColors.sublist(0, 10)),
    ColorMatchDifficulty( levelIndex: 11, name: 'Lv. 11: 지옥 (10색 초고속)', contextId: 'MATCH_L11_HELL', totalRounds: 60, timeLimitSeconds: 2, enableStroopEffect: true, colorSet: baseColors.sublist(0, 10)),
    ColorMatchDifficulty( levelIndex: 12, name: 'Lv. 12: 궁극 (10색, 초고속)', contextId: 'MATCH_L12_ULTIMATE', totalRounds: 70, timeLimitSeconds: 2, enableStroopEffect: true, colorSet: baseColors.sublist(0, 10)),
  ];

  static ColorMatchDifficulty 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]);
  }
}

/// 화면에 제시될 단일 문제 (Text와 Color의 조합)
class MatchChallenge {
  final String displayedWord;
  final Color textColor;
  final Color backgroundColor;
  final Color targetColor;

  MatchChallenge({
    required this.displayedWord,
    required this.textColor,
    required this.backgroundColor,
    required this.targetColor,
  });
}