import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../models/test_models.dart';
import '../services/test_service.dart';
import '../services/test_service_impl.dart';
import '../../../core/models/api_response.dart';

/// 测试服务提供者
final testServiceProvider = Provider<TestService>((ref) {
  return TestServiceImpl();
});

/// 测试状态类
class TestState {
  final bool isLoading;
  final String? errorMessage;
  final List<TestTemplate> templates;
  final TestSession? currentSession;
  final List<TestResult> testResults;
  final TestResult? currentResult;
  final Map<SkillType, double> skillStatistics;
  final Map<String, dynamic> questionStatistics;
  final int timeRemaining;
  final bool isTimerRunning;

  const TestState({
    this.isLoading = false,
    this.errorMessage,
    this.templates = const [],
    this.currentSession,
    this.testResults = const [],
    this.currentResult,
    this.skillStatistics = const {},
    this.questionStatistics = const {},
    this.timeRemaining = 0,
    this.isTimerRunning = false,
  });

  TestState copyWith({
    bool? isLoading,
    String? errorMessage,
    List<TestTemplate>? templates,
    TestSession? currentSession,
    List<TestResult>? testResults,
    TestResult? currentResult,
    Map<SkillType, double>? skillStatistics,
    Map<String, dynamic>? questionStatistics,
    int? timeRemaining,
    bool? isTimerRunning,
  }) {
    return TestState(
      isLoading: isLoading ?? this.isLoading,
      errorMessage: errorMessage ?? this.errorMessage,
      templates: templates ?? this.templates,
      currentSession: currentSession ?? this.currentSession,
      testResults: testResults ?? this.testResults,
      currentResult: currentResult ?? this.currentResult,
      skillStatistics: skillStatistics ?? this.skillStatistics,
      questionStatistics: questionStatistics ?? this.questionStatistics,
      timeRemaining: timeRemaining ?? this.timeRemaining,
      isTimerRunning: isTimerRunning ?? this.isTimerRunning,
    );
  }

  /// 获取当前题目
  TestQuestion? get currentQuestion {
    if (currentSession == null || 
        currentSession!.currentQuestionIndex >= currentSession!.questions.length) {
      return null;
    }
    return currentSession!.questions[currentSession!.currentQuestionIndex];
  }
}

/// 测试状态管理器
class TestNotifier extends StateNotifier<TestState> {
  final TestService _testService;

  TestNotifier(this._testService) : super(const TestState());

  /// 设置加载状态
  void _setLoading(bool loading) {
    state = state.copyWith(isLoading: loading);
  }

  /// 设置错误信息
  void _setError(String? error) {
    state = state.copyWith(errorMessage: error);
  }

  /// 清除错误信息
  void clearError() {
    _setError(null);
  }

  /// 加载所有测试模板
  Future<void> loadTestTemplates() async {
    _setLoading(true);
    _setError(null);

    try {
      final response = await _testService.getTestTemplates();
      if (response.success && response.data != null) {
        state = state.copyWith(templates: response.data!, isLoading: false);
      } else {
        _setError(response.message);
        _setLoading(false);
      }
    } catch (e) {
      _setError('加载测试模板失败: ${e.toString()}');
      _setLoading(false);
    }
  }

  /// 加载指定类型的测试模板
  Future<void> loadTestTemplatesByType(TestType type) async {
    _setLoading(true);
    _setError(null);

    try {
      final response = await _testService.getTestTemplatesByType(type);
      if (response.success && response.data != null) {
        state = state.copyWith(templates: response.data!, isLoading: false);
      } else {
        _setError(response.message);
        _setLoading(false);
      }
    } catch (e) {
      _setError('加载测试模板失败: ${e.toString()}');
      _setLoading(false);
    }
  }

  /// 加载最近的测试结果
  Future<void> loadRecentResults({String? userId}) async {
    if (userId == null) {
      _setError('用户ID不能为空');
      return;
    }
    
    _setLoading(true);
    _setError(null);

    try {
      final response = await _testService.getRecentTestResults(userId);
      if (response.success && response.data != null) {
        state = state.copyWith(testResults: response.data!, isLoading: false);
      } else {
        _setError(response.message);
        _setLoading(false);
      }
    } catch (e) {
      _setError('加载测试结果失败: ${e.toString()}');
      _setLoading(false);
    }
  }

  /// 创建测试会话
  Future<bool> createTestSession({
    required String templateId,
    required String userId,
  }) async {
    _setLoading(true);
    _setError(null);

    try {
      final response = await _testService.createTestSession(
        templateId: templateId,
        userId: userId,
      );
      
      if (response.success && response.data != null) {
        state = state.copyWith(
          currentSession: response.data!,
          timeRemaining: response.data!.timeRemaining,
          isLoading: false,
        );
        return true;
      } else {
        _setError(response.message);
        _setLoading(false);
        return false;
      }
    } catch (e) {
      _setError('创建测试会话失败: ${e.toString()}');
      _setLoading(false);
      return false;
    }
  }

  /// 开始测试
  Future<bool> startTest() async {
    if (state.currentSession == null) {
      _setError('没有活动的测试会话');
      return false;
    }

    _setLoading(true);
    _setError(null);

    try {
      final response = await _testService.startTest(state.currentSession!.id);
      
      if (response.success && response.data != null) {
        state = state.copyWith(
          currentSession: response.data!,
          isTimerRunning: true,
          isLoading: false,
        );
        return true;
      } else {
        _setError(response.message);
        _setLoading(false);
        return false;
      }
    } catch (e) {
      _setError('开始测试失败: ${e.toString()}');
      _setLoading(false);
      return false;
    }
  }

  /// 设置当前测试会话
  void setCurrentSession(TestSession session) {
    state = state.copyWith(
      currentSession: session,
      timeRemaining: session.timeRemaining,
      isTimerRunning: true,
    );
  }

  /// 设置当前测试结果
  void setCurrentResult(TestResult result) {
    state = state.copyWith(
      currentResult: result,
      isLoading: false,
    );
  }

  /// 记录答案（本地记录，不提交到服务器）
  void recordAnswer(UserAnswer answer) {
    if (state.currentSession == null) {
      _setError('没有活动的测试会话');
      return;
    }

    final currentSession = state.currentSession!;
    final updatedAnswers = List<UserAnswer>.from(currentSession.answers);
    
    // 移除该题目的旧答案
    updatedAnswers.removeWhere((a) => a.questionId == answer.questionId);
    // 添加新答案
    updatedAnswers.add(answer);

    final updatedSession = currentSession.copyWith(answers: updatedAnswers);
    state = state.copyWith(currentSession: updatedSession);
  }

  /// 提交答案
  Future<bool> submitAnswer(UserAnswer answer) async {
    if (state.currentSession == null) {
      _setError('没有活动的测试会话');
      return false;
    }

    try {
      final response = await _testService.submitAnswer(
        sessionId: state.currentSession!.id,
        answer: answer,
      );
      
      if (response.success && response.data != null) {
        state = state.copyWith(currentSession: response.data!);
        return true;
      } else {
        _setError(response.message);
        return false;
      }
    } catch (e) {
      _setError('提交答案失败: ${e.toString()}');
      return false;
    }
  }

  /// 完成测试
  Future<TestResult?> completeTest() async {
    if (state.currentSession == null) {
      _setError('没有活动的测试会话');
      return null;
    }

    _setLoading(true);
    _setError(null);

    try {
      final response = await _testService.completeTest(
        sessionId: state.currentSession!.id,
        answers: state.currentSession!.answers,
      );
      
      if (response.success && response.data != null) {
        state = state.copyWith(
          currentResult: response.data!,
          currentSession: null,
          isTimerRunning: false,
          isLoading: false,
        );
        return response.data!;
      } else {
        _setError(response.message);
        _setLoading(false);
        return null;
      }
    } catch (e) {
      _setError('完成测试失败: ${e.toString()}');
      _setLoading(false);
      return null;
    }
  }

  /// 下一题
  void nextQuestion() {
    if (state.currentSession == null) {
      _setError('没有活动的测试会话');
      return;
    }

    final currentSession = state.currentSession!;
    if (currentSession.currentQuestionIndex < currentSession.questions.length - 1) {
      final updatedSession = TestSession(
        id: currentSession.id,
        userId: currentSession.userId,
        templateId: currentSession.templateId,
        questions: currentSession.questions,
        answers: currentSession.answers,
        startTime: currentSession.startTime,
        endTime: currentSession.endTime,
        status: currentSession.status,
        timeRemaining: currentSession.timeRemaining,
        currentQuestionIndex: currentSession.currentQuestionIndex + 1,
      );
      
      state = state.copyWith(currentSession: updatedSession);
    }
  }

  /// 上一题
  void previousQuestion() {
    if (state.currentSession == null) {
      _setError('没有活动的测试会话');
      return;
    }

    final currentSession = state.currentSession!;
    if (currentSession.currentQuestionIndex > 0) {
      final updatedSession = TestSession(
        id: currentSession.id,
        userId: currentSession.userId,
        templateId: currentSession.templateId,
        questions: currentSession.questions,
        answers: currentSession.answers,
        startTime: currentSession.startTime,
        endTime: currentSession.endTime,
        status: currentSession.status,
        timeRemaining: currentSession.timeRemaining,
        currentQuestionIndex: currentSession.currentQuestionIndex - 1,
      );
      
      state = state.copyWith(currentSession: updatedSession);
    }
  }

  /// 获取测试类型的中文名称
  String getTestTypeName(TestType type) {
    switch (type) {
      case TestType.quick:
        return '快速测试';
      case TestType.standard:
        return '标准测试';
      case TestType.full:
        return '完整测试';
      case TestType.mock:
        return '模拟考试';
      case TestType.vocabulary:
        return '词汇专项';
      case TestType.grammar:
        return '语法专项';
      case TestType.reading:
        return '阅读专项';
      case TestType.listening:
        return '听力专项';
      case TestType.speaking:
        return '口语专项';
      case TestType.writing:
        return '写作专项';
    }
  }

  /// 获取技能类型的中文名称
  String getSkillTypeName(SkillType skill) {
    switch (skill) {
      case SkillType.vocabulary:
        return '词汇';
      case SkillType.grammar:
        return '语法';
      case SkillType.reading:
        return '阅读';
      case SkillType.listening:
        return '听力';
      case SkillType.speaking:
        return '口语';
      case SkillType.writing:
        return '写作';
    }
  }

  /// 获取难度等级的中文名称
  String getDifficultyLevelName(DifficultyLevel level) {
    switch (level) {
      case DifficultyLevel.beginner:
        return '初级';
      case DifficultyLevel.elementary:
        return '基础';
      case DifficultyLevel.intermediate:
        return '中级';
      case DifficultyLevel.upperIntermediate:
        return '中高级';
      case DifficultyLevel.advanced:
        return '高级';
      case DifficultyLevel.expert:
        return '专家级';
    }
  }
}

/// 测试状态提供者
final testProvider = StateNotifierProvider<TestNotifier, TestState>((ref) {
  final testService = ref.watch(testServiceProvider);
  return TestNotifier(testService);
});

/// 当前测试会话提供者
final currentTestSessionProvider = Provider<TestSession?>((ref) {
  return ref.watch(testProvider).currentSession;
});

/// 当前题目提供者
final currentQuestionProvider = Provider<TestQuestion?>((ref) {
  return ref.watch(testProvider).currentQuestion;
});

/// 测试模板提供者
final testTemplatesProvider = Provider<List<TestTemplate>>((ref) {
  return ref.watch(testProvider).templates;
});

/// 测试结果提供者
final testResultsProvider = Provider<List<TestResult>>((ref) {
  return ref.watch(testProvider).testResults;
});