import 'package:json_annotation/json_annotation.dart';
part 'additional_conditions_for_review.g.dart';

// 回评外加条件
@JsonSerializable()
class AdditionalConditionsForReview extends Object {
  // 排序规则集合
  static List<CollationItem> collationArr = [
    CollationItem(
        title: '评阅时间:近>远', type: 1, field: OrderField.TIME, rule: RuleEnum.ASC),
    CollationItem(
        title: '评阅时间:远>近', type: 2, field: OrderField.TIME, rule: RuleEnum.DESC),
    CollationItem(
        title: '评阅分值:高>低', type: 3, field: OrderField.SCORE, rule: RuleEnum.DESC),
    CollationItem(
        title: '评阅分值:低>高', type: 4, field: OrderField.SCORE, rule: RuleEnum.ASC),
  ];

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

  @JsonKey(name: 'markingTimeOrderType')
  int? markingTimeOrderType; // 评阅时间排序方式 0:从远到近 1:从近到远

  @JsonKey(name: 'scoreOrderType')
  int? scoreOrderType; // 分数排序方式  0:从低到高 1:从高到低

  @JsonKey(name: 'orderItem')
  CollationItem? orderItem; // 选中排序规则

  Future<void> cleanCollation() async {
    orderItem = null;
    markingTimeOrderType = null;
    scoreOrderType = null;
    questionId = null;
  }

  // 同步实例化实例数据
  void synchronizationInstance(CollationItem? item, String? testId) {
    orderItem = item;
    questionId = testId;

    if (item != null) {
      RuleEnum rule = item.rule;
      OrderField field = item.field;

      switch (field) {
        case OrderField.TIME:
          // 评阅时间排序方式 0:从远到近 1:从近到远
          markingTimeOrderType = RuleEnum.DESC == rule ? 0 : 1;
          break;
        case OrderField.SCORE:
          // 分数排序方式  0:从低到高 1:从高到低
          scoreOrderType = RuleEnum.DESC == rule ? 1 : 0;
          break;
      }
    }
  }

  AdditionalConditionsForReview(
      {this.questionId,
      this.markingTimeOrderType,
      this.scoreOrderType,
      this.orderItem});

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

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

// 排序方式实体ITEM
@JsonSerializable()
class CollationItem extends Object {
  String title;
  OrderField field;
  RuleEnum rule;
  int type;

  CollationItem({
    required this.title,
    required this.field,
    required this.rule,
    required this.type,
  });

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

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

// 排序字段枚举
enum OrderField { TIME, SCORE }

// 排序方式枚举
enum RuleEnum { DESC, ASC }

// 条件确定回调
typedef CollationCallback = Future<void> Function(
    {required bool reset, CollationItem? item, String? testId});
