import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:making_school_asignment_app/common/config/request_config.dart';

part 'do_paper_details_result.g.dart';

@JsonSerializable(checked: true, includeIfNull: false)
class DoPaperDetailsResult extends Object {
  @JsonKey(name: 'templateIds')
  Map<String, bool> templateIds;

  // 自定义字段
  @JsonKey(name: 'templateIdKeys')
  List<int>? templateIdKeys;

  // 自定义字段
  @JsonKey(name: 'templateIdKeyMap')
  Map<int, int>? templateIdKeyMap;

  @JsonKey(name: 'submitStudents', toJson: _paperStudentsToJson) // 当前页 总提交学生集合
  List<PaperStudents> students;

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

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

  // 自定义字段
  @JsonKey(name: 'priority', defaultValue: false)
  bool priority;

  @JsonKey(name: 'annotatedCount') // 当前试题批阅数量
  int annotatedCount;

  @JsonKey(name: 'submitCount') // 当前试题提交数量
  int submitCount;

  @JsonKey(name: 'zgtAnswer') // 主观题作答图片
  String zgtAnswer;

  @JsonKey(name: 'showZgtAnnotate') // 主观题老师批注图片
  String? showZgtAnnotate;

  @JsonKey(name: 'zgtAnnotate') // 主观题老师批注图片
  String? zgtAnnotate;

  @JsonKey(name: 'lastAnswerTime') // 学生提交作答试题时间
  String lastAnswerTime;

  // 批注时间
  @JsonKey(name: 'annotateTime')
  String? annotateTime;

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

  @JsonKey(name: 'studentQuestions', toJson: _studentQuestionsToJson)
  List<StudentQuestions> studentQuestions;

  // 当前页 未提交学生集合
  @JsonKey(name: 'unSubmitStudents', toJson: _paperStudentsToJson)
  List<PaperStudents> unSubmitStudents;

  @JsonKey(name: 'lastPage', toJson: _lastPageToJson)
  LastPage? lastPage;

  @JsonKey(name: 'nextPage', toJson: _nextPageToJson)
  NextPage? nextPage;

  // 所有页 总待提交数量
  @JsonKey(name: 'totalUnAnnotateCount')
  int totalUnAnnotateCount;

  @JsonKey(name: 'needAnnotate') // 是否需要批阅
  bool needAnnotate;

  DoPaperDetailsResult(
      this.totalUnAnnotateCount,
      this.templateIds,
      this.students,
      this.templateId,
      this.studentId,
      this.annotatedCount,
      this.submitCount,
      this.zgtAnswer,
      this.zgtAnnotate,
      this.lastAnswerTime,
      this.isFav,
      this.studentQuestions,
      this.unSubmitStudents,
      this.lastPage,
      this.nextPage,
      this.templateIdKeys,
      this.templateIdKeyMap,
      this.priority,
      this.annotateTime,
      this.showZgtAnnotate,
      {this.needAnnotate = false}) {
    if (templateIds.keys.isNotEmpty) {
      templateIdKeys = templateIds.keys.map((e) => int.parse(e)).toList();
      templateIdKeyMap = <int, int>{};
      for (var i = 1; i <= templateIdKeys!.length; i++) {
        var theVal = templateIdKeys![i - 1];
        templateIdKeyMap![i] = theVal;
        templateIdKeyMap![theVal] = i;
      }
    }

    // 找到当前学生是否是优先批阅
    if (students.isNotEmpty) {
      var currentStudent = students.firstWhereOrNull((e) => e.id == studentId);
      if (currentStudent != null) priority = currentStudent.isPriority;
    }

    if (!zgtAnswer.contains(RequestConfig.imgUrl)) {
      zgtAnswer = '${RequestConfig.imgUrl}$zgtAnswer?$lastAnswerTime';
    }

    if (zgtAnnotate?.isNotEmpty ?? false) {
      showZgtAnnotate = RequestConfig.imgUrl + zgtAnnotate!; // 批注图片地址赋值
      if (annotateTime != null) showZgtAnnotate = '${showZgtAnnotate!}?$annotateTime';
    }

    // 判断当前试题是否需要批阅
    if (annotateTime == null || studentQuestions.indexWhere((e) => e.studentScore == null) != -1) {
      needAnnotate = true;
    }
    // print('学生作答图片：${annotatedCount}');
    // print('老师批注图片提交数量：${submitCount}');
  }

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

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

  static List<Map<String, dynamic>> _paperStudentsToJson(List<PaperStudents> examples) => examples.map((e) => e.toJson()).toList();

  static List<Map<String, dynamic>> _studentQuestionsToJson(List<StudentQuestions> examples) => examples.map((e) => e.toJson()).toList();

  static Map<String, dynamic>? _nextPageToJson(NextPage? example) => example?.toJson();
  static Map<String, dynamic>? _lastPageToJson(LastPage? example) => example?.toJson();
}

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

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

  // 优先批阅
  @JsonKey(name: 'isPriority')
  bool isPriority;

  PaperStudents(
    this.id,
    this.name,
    this.isPriority,
  );

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

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

@JsonSerializable()
class StudentQuestions extends Object {
  @JsonKey(name: 'questionNo')
  String questionNo;

  // 总分
  @JsonKey(name: 'score')
  int? score;

  // 分值步长
  @JsonKey(name: 'scoreStep')
  int? scoreStep;

  // 是否已经批阅
  @JsonKey(name: 'isNeedAnnotate')
  bool isNeedAnnotate;

  // 学生得分
  @JsonKey(name: 'studentScore')
  int? studentScore;

  // 是否正确
  @JsonKey(name: 'isCorrect')
  bool? isCorrect;

  // 正确率
  @JsonKey(name: 'correctRate')
  int correctRate;

  @JsonKey(name: 'height')
  double? height;

  @JsonKey(name: 'useTime')
  int? useTime;

  StudentQuestions({
    required this.height,
    required this.questionNo,
    required this.correctRate,
    required this.isNeedAnnotate,
    this.score,
    this.scoreStep,
    this.studentScore,
    this.isCorrect,
    this.useTime,
  });

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

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

@JsonSerializable()
class LastPage extends Object {
  @JsonKey(name: 'templateId')
  int templateId;

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

  LastPage(
    this.templateId,
    this.studentId,
  );

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

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

@JsonSerializable()
class NextPage extends Object {
  @JsonKey(name: 'templateId')
  int templateId;

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

  NextPage(
    this.templateId,
    this.studentId,
  );

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

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