import 'package:json_annotation/json_annotation.dart';
import 'package:marking_app/common/model/enum/job_marking_type_enum.dart';

part 'job_task_item.g.dart';

@JsonSerializable()
class JobTaskItem extends Object {
  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'title')
  String title;

  @JsonKey(name: 'subjectName')
  String subjectName;

  @JsonKey(name: 'genderName')
  String genderName;

  @JsonKey(name: 'isFinish')
  bool isFinish;

  @JsonKey(name: 'studentCount')
  int studentCount;

  @JsonKey(name: 'commitStudentCount')
  int commitStudentCount;

  @JsonKey(name: 'totalCount')
  int totalCount;

  @JsonKey(name: 'finishCount')
  int finishCount;

  @JsonKey(name: 'objectivePrecision')
  double objectivePrecision;

  @JsonKey(name: 'subjectivePrecision')
  double subjectivePrecision;

  @JsonKey(name: 'precision')
  double precision;

  // @JsonKey(name: 'markingTasks')
  // List<MarkingTasks> markingTasks;

  @JsonKey(name: 'createTime')
  String createTime;

  @JsonKey(name: 'progressPercentage')
  double progressPercentage; // 进度百分比

  @JsonKey(name: 'markingType')
  int markingType; // 考试类型

  @JsonKey(name: 'markingTypeEnum')
  JobMarkingTypeEnum markingTypeEnum; // 考试类型

  int taskCount; // 参与班级数量

  JobTaskItem(
      this.id,
      this.title,
      this.subjectName,
      this.genderName,
      this.isFinish,
      this.studentCount,
      this.commitStudentCount,
      this.totalCount,
      this.finishCount,
      this.objectivePrecision,
      this.subjectivePrecision,
      this.precision,
      // this.markingTasks,
      this.createTime,
      this.markingType,
      this.taskCount,
      {this.progressPercentage = 0,
      this.markingTypeEnum = JobMarkingTypeEnum.UNUSED}) {
    try {
      progressPercentage = (finishCount / totalCount) * 100;
      if (progressPercentage.isNaN) {
        progressPercentage = 0;
      } else {
        progressPercentage = double.parse(progressPercentage.toStringAsFixed(2));
      }
    } catch (e) {
      progressPercentage = 0;
    }
    try {
      markingTypeEnum = JobMarkingTypeEnum.values[this.markingType];
    } catch (e) {
      markingTypeEnum = JobMarkingTypeEnum.UNUSED;
    }
  }

  factory JobTaskItem.fromJson(Map<String, dynamic> srcJson) => _$JobTaskItemFromJson(srcJson);

  Map<String, dynamic> toJson() => _$JobTaskItemToJson(this);
}

@JsonSerializable()
class MarkingTasks extends Object {
  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'className')
  String className;

  int dpcSchoolId;

  int dpcGradeId;

  @JsonKey(name: 'teacherName')
  String teacherName;

  @JsonKey(name: 'isFinish')
  bool isFinish;

  @JsonKey(name: 'finishTime')
  String? finishTime;

  @JsonKey(name: 'studentCount')
  int studentCount;

  @JsonKey(name: 'commitStudentCount')
  int commitStudentCount;

  @JsonKey(name: 'totalCount')
  int totalCount;

  @JsonKey(name: 'finishCount')
  int finishCount;

  @JsonKey(name: 'objectivePrecision') // 客观题正确率
  double objectivePrecision;

  @JsonKey(name: 'subjectivePrecision') // 主观题正确率
  double subjectivePrecision;

  @JsonKey(name: 'precision') // 综合正确率
  double precision;

  @JsonKey(name: 'canMarking')
  bool canMarking;

  /** 前端自定义字段 */
  @JsonKey(name: 'progressPercentage')
  double progressPercentage; // 进度百分比

  /** 前端自定义字段 */
  @JsonKey(name: 'canGoReview')
  bool canGoReview; // 是否能批阅

  /** 前端自定义字段 */
  @JsonKey(name: 'collectNumber')
  int collectNumber; // 是否能批阅

  MarkingTasks(
    this.id,
    this.dpcSchoolId,
    this.dpcGradeId,
    this.className,
    this.teacherName,
    this.isFinish,
    this.studentCount,
    this.commitStudentCount,
    this.totalCount,
    this.finishCount,
    this.precision,
    this.objectivePrecision,
    this.subjectivePrecision,
    this.canMarking, {
    this.progressPercentage = 0,
    this.canGoReview = true,
    this.finishTime,
    this.collectNumber = 0,
  }) {
    try {
      progressPercentage = (finishCount / totalCount) * 100;
      if (progressPercentage.isNaN)
        progressPercentage = 0;
      else
        progressPercentage = double.parse(progressPercentage.toStringAsFixed(2));
    } catch (e) {
      progressPercentage = 0;
    }
    try {
      precision = precision / 100;
      objectivePrecision = objectivePrecision / 100;
      subjectivePrecision = subjectivePrecision / 100;
    } catch (e) {}

    canGoReview = totalCount > 0 && canMarking && !isFinish;
  }

  factory MarkingTasks.fromJson(Map<String, dynamic> srcJson) => _$MarkingTasksFromJson(srcJson);

  Map<String, dynamic> toJson() => _$MarkingTasksToJson(this);
}
