import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:marking_app/common/mixin/common.dart';
import 'package:marking_app/common/model/job/job_review_submission.dart';
import 'package:marking_app/common/model/job/marking_text_question_job.dart';

class UseDoPaperScoring with CommonMixin {
  ValueNotifier<JobReviewSubmission> submittedData; // 待提交数据
  ValueNotifier<Map<String, JobReviewQuestions>> questionsMap; // 待提交数据

  MarkingTextQuestionJob? questionData;

  UseDoPaperScoring._({required this.questionData, required this.questionsMap, required this.submittedData});

  // 工厂构造函数
  factory UseDoPaperScoring.use({required MarkingTextQuestionJob data}) {
    var submittedData = useState<JobReviewSubmission>(JobReviewSubmission(
      paperId: data.paperId,
      score: 0,
      questions:
          data.questions.map((e) => JobReviewQuestions(questionNo: e.questionNo, score: e.score?.toInt())).toList(),
    ));
    List<JobReviewQuestions> questions = submittedData.value.questions;

    var theQuestionsMap = useState<Map<String, JobReviewQuestions>>(Map<String, JobReviewQuestions>.fromIterable(
      questions,
      key: (key) => key.questionNo,
      value: (value) => questions.firstWhere((element) => element.questionNo == value.questionNo),
    ));

    return UseDoPaperScoring._(
      questionData: data,
      submittedData: submittedData,
      questionsMap: theQuestionsMap,
    );
  }
}
