import 'course.dart';

/// Concurrent evaluation task status
enum TaskStatus {
  waiting, // 等待开始
  preparing, // 准备评教（访问页面、解析问卷）
  countdown, // 倒计时等待
  submitting, // 提交中
  verifying, // 验证中
  completed, // 完成
  failed, // 失败
}

/// Concurrent evaluation task
class ConcurrentTask {
  final int taskId;
  final Course course;
  TaskStatus status;
  String? statusMessage;
  int countdownRemaining;
  int countdownTotal;
  String? errorMessage;
  DateTime? startTime;
  DateTime? endTime;

  ConcurrentTask({
    required this.taskId,
    required this.course,
    this.status = TaskStatus.waiting,
    this.statusMessage,
    this.countdownRemaining = 0,
    this.countdownTotal = 0,
    this.errorMessage,
    this.startTime,
    this.endTime,
  });

  /// Get progress (0.0 to 1.0)
  double get progress {
    switch (status) {
      case TaskStatus.waiting:
        return 0.0;
      case TaskStatus.preparing:
        return 0.1;
      case TaskStatus.countdown:
        if (countdownTotal > 0) {
          return 0.1 +
              0.7 * (countdownTotal - countdownRemaining) / countdownTotal;
        }
        return 0.1;
      case TaskStatus.submitting:
        return 0.85;
      case TaskStatus.verifying:
        return 0.95;
      case TaskStatus.completed:
      case TaskStatus.failed:
        return 1.0;
    }
  }

  /// Get status display text
  String get statusText {
    if (statusMessage != null) return statusMessage!;

    switch (status) {
      case TaskStatus.waiting:
        return '等待开始';
      case TaskStatus.preparing:
        return '准备评教';
      case TaskStatus.countdown:
        return '等待提交 (${countdownRemaining}s)';
      case TaskStatus.submitting:
        return '提交中';
      case TaskStatus.verifying:
        return '验证中';
      case TaskStatus.completed:
        return '完成';
      case TaskStatus.failed:
        return '失败';
    }
  }

  /// Check if task is finished
  bool get isFinished =>
      status == TaskStatus.completed || status == TaskStatus.failed;

  /// Check if task is successful
  bool get isSuccess => status == TaskStatus.completed;

  ConcurrentTask copyWith({
    TaskStatus? status,
    String? statusMessage,
    int? countdownRemaining,
    int? countdownTotal,
    String? errorMessage,
    DateTime? startTime,
    DateTime? endTime,
  }) {
    return ConcurrentTask(
      taskId: taskId,
      course: course,
      status: status ?? this.status,
      statusMessage: statusMessage ?? this.statusMessage,
      countdownRemaining: countdownRemaining ?? this.countdownRemaining,
      countdownTotal: countdownTotal ?? this.countdownTotal,
      errorMessage: errorMessage ?? this.errorMessage,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
    );
  }
}
