package vip.xiaonuo.exam.controller.student;

import vip.xiaonuo.exam.base.BaseApiController;
import vip.xiaonuo.exam.base.RestResponse;
import vip.xiaonuo.exam.domain.ExamPaperQuestionCustomerAnswer;
import vip.xiaonuo.exam.domain.Subject;
import vip.xiaonuo.exam.domain.TextContent;
import vip.xiaonuo.exam.domain.question.QuestionObject;
import vip.xiaonuo.exam.service.ExamPaperQuestionCustomerAnswerService;
import vip.xiaonuo.exam.service.QuestionService;
import vip.xiaonuo.exam.service.SubjectService;
import vip.xiaonuo.exam.service.TextContentService;
import vip.xiaonuo.exam.utility.DateTimeUtil;
import vip.xiaonuo.exam.utility.HtmlUtil;
import vip.xiaonuo.exam.utility.JsonUtil;
import vip.xiaonuo.exam.utility.PageInfoHelper;
import vip.xiaonuo.exam.viewmodel.admin.question.QuestionEditRequestVM;
import vip.xiaonuo.exam.viewmodel.student.exam.ExamPaperSubmitItemVM;
import vip.xiaonuo.exam.viewmodel.student.question.answer.QuestionAnswerVM;
import vip.xiaonuo.exam.viewmodel.student.question.answer.QuestionPageStudentRequestVM;
import vip.xiaonuo.exam.viewmodel.student.question.answer.QuestionPageStudentResponseVM;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("StudentQuestionAnswerController")
@RequestMapping(value = "/api/student/question/answer")
public class QuestionAnswerController extends BaseApiController {

    private final ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService;
    private final QuestionService questionService;
    private final TextContentService textContentService;
    private final SubjectService subjectService;

    @Autowired
    public QuestionAnswerController(ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService, QuestionService questionService, TextContentService textContentService, SubjectService subjectService) {
        this.examPaperQuestionCustomerAnswerService = examPaperQuestionCustomerAnswerService;
        this.questionService = questionService;
        this.textContentService = textContentService;
        this.subjectService = subjectService;
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<QuestionPageStudentResponseVM>> pageList(@RequestBody QuestionPageStudentRequestVM model) {
        model.setCreateUser(getCurrentUser().getId());
        PageInfo<ExamPaperQuestionCustomerAnswer> pageInfo = examPaperQuestionCustomerAnswerService.studentPage(model);
        PageInfo<QuestionPageStudentResponseVM> page = PageInfoHelper.copyMap(pageInfo, q -> {
            Subject subject = subjectService.selectById(q.getSubjectId());
            QuestionPageStudentResponseVM vm = modelMapper.map(q, QuestionPageStudentResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(q.getCreateTime()));
            TextContent textContent = textContentService.selectById(q.getQuestionTextContentId());
            QuestionObject questionObject = JsonUtil.toJsonObject(textContent.getContent(), QuestionObject.class);
            String clearHtml = HtmlUtil.clear(questionObject.getTitleContent());
            vm.setShortTitle(clearHtml);
            vm.setSubjectName(subject.getName());
            return vm;
        });
        return RestResponse.ok(page);
    }


    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<QuestionAnswerVM> select(@PathVariable Integer id) {
        QuestionAnswerVM vm = new QuestionAnswerVM();
        ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer = examPaperQuestionCustomerAnswerService.selectById(id);
        ExamPaperSubmitItemVM questionAnswerVM = examPaperQuestionCustomerAnswerService.examPaperQuestionCustomerAnswerToVM(examPaperQuestionCustomerAnswer);
        QuestionEditRequestVM questionVM = questionService.getQuestionEditRequestVM(examPaperQuestionCustomerAnswer.getQuestionId());
        vm.setQuestionVM(questionVM);
        vm.setQuestionAnswerVM(questionAnswerVM);
        return RestResponse.ok(vm);
    }

}
