package cn.com.qmth.examcloud.core.questions.api.controller;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.api.request.GetDefaultQuesionReq;
import cn.com.qmth.examcloud.core.questions.api.request.GetDefaultQuesionsReq;
import cn.com.qmth.examcloud.core.questions.api.request.GetQuestionListReq;
import cn.com.qmth.examcloud.core.questions.api.request.GetQuestionReq;
import cn.com.qmth.examcloud.core.questions.service.ExtractConfigProviderService;
import cn.com.qmth.examcloud.core.questions.service.QuestionProviderService;
import cn.com.qmth.examcloud.question.commons.core.question.DefaultQuestion;
import cn.com.qmth.examcloud.support.handler.QuestionBodyHandler;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author weiwenhai
 * @date 20180.9.10
 * @company qmth
 * @description controller
 * @code 013
 */
@RestController
@RequestMapping("${api_cqb}/default_question")
public class DefaultQuesionController extends ControllerSupport {

    @Autowired
    private QuestionProviderService questionProviderService;

    @Autowired
    private ExtractConfigProviderService extractConfigExamService;

    @ApiOperation(value = "外部接口保存单个试题", notes = "外部接口保存单个试题")
    @PostMapping("/save")
    public ResponseEntity<Object> save(@RequestBody GetDefaultQuesionReq req) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException("Q-013041", "用户不存在！");
        }
        DefaultQuestion defaultQuestion = req.getDefaultQuestion();
        if (defaultQuestion.getRootOrgId() == null) {
            throw new StatusException("Q-013045", "rootOrgId is null");
        }
        String id = questionProviderService.save(defaultQuestion);
        return new ResponseEntity<>(id, HttpStatus.OK);
    }

    @ApiOperation(value = "修改试题")
    @PutMapping("/update")
    public ResponseEntity<Object> update(@RequestBody GetDefaultQuesionReq req) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException("Q-013057", "用户不存在！");
        }
        DefaultQuestion defaultQuestion = req.getDefaultQuestion();
        if (defaultQuestion.getRootOrgId() == null) {
            throw new StatusException("Q-013061", "rootOrgId is null");
        }
        String id = questionProviderService.save(defaultQuestion);
        return new ResponseEntity<>(id, HttpStatus.OK);
    }

    @ApiOperation(value = "查询试题")
    @PostMapping("/find")
    public ResponseEntity<Object> find(@RequestBody GetDefaultQuesionsReq req) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException("Q-013075", "用户不存在！");
        }
        Long rootOrgId = req.getRootOrgId();
        if (rootOrgId == null) {
            throw new StatusException("Q-011079", "rootOrgId is null");
        }
        Page<DefaultQuestion> defaultQuestions = questionProviderService.findQustions(rootOrgId, req.getProperties(), req.getCurPage(), req.getPageSize());
        return new ResponseEntity<>(defaultQuestions, HttpStatus.OK);
    }

    @ApiOperation(value = "查询试题")
    @PostMapping("/find_questions")
    public ResponseEntity<Object> find(@RequestBody GetQuestionListReq req) {
        Set<String> questionIds = req.getQuestionIds();
        if (questionIds == null || questionIds.size() < 1) {
            throw new StatusException("Q-011089", "questionId is null");
        }
        List<DefaultQuestion> defaultQuestions = questionProviderService.findQuestions(questionIds);
        return new ResponseEntity<>(defaultQuestions, HttpStatus.OK);
    }

    @ApiOperation(value = "查询试题")
    @PostMapping("/question")
    public ResponseEntity<Object> findOne(@RequestBody GetQuestionReq questionReq) {
        Long examId = questionReq.getExamId();
        if (null == examId) {
            throw new StatusException("Q-010078", "examId is null");
        }
        String courseCode = questionReq.getCourseCode();
        if (StringUtils.isBlank(courseCode)) {
            throw new StatusException("Q-010082", "courseCode is null");
        }
        String groupCode = questionReq.getGroupCode();
        if (StringUtils.isBlank(groupCode)) {
            throw new StatusException("Q-010086", "groupCode is null");
        }
        String questionId = questionReq.getQuestionId();
        if (StringUtils.isBlank(questionId)) {
            throw new StatusException("Q-010090", "questionId is null");
        }
        DefaultQuestion defaultQuestion = extractConfigExamService.getDefaultQuestion(examId, courseCode, groupCode, questionId);
        return new ResponseEntity<>(defaultQuestion, HttpStatus.OK);
    }

    @ApiOperation(value = "查询试题（客户端）")
    @PostMapping("/question/for/client")
    public ResponseEntity<Object> findOneForClient(@RequestBody GetQuestionReq questionReq) {
        Long examId = questionReq.getExamId();

        if (null == examId) {
            throw new StatusException("Q-010078", "examId is null");
        }
        String courseCode = questionReq.getCourseCode();
        if (StringUtils.isBlank(courseCode)) {
            throw new StatusException("Q-010082", "courseCode is null");
        }
        String groupCode = questionReq.getGroupCode();
        if (StringUtils.isBlank(groupCode)) {
            throw new StatusException("Q-010086", "groupCode is null");
        }
        String questionId = questionReq.getQuestionId();
        if (StringUtils.isBlank(questionId)) {
            throw new StatusException("Q-010090", "questionId is null");
        }
        DefaultQuestion defaultQuestion = extractConfigExamService.getDefaultQuestion(examId, courseCode, groupCode, questionId);

        // 将题干、选项等 HTML结构转换为“富文本”JSON结构
        QuestionBodyHandler.convertRichText(defaultQuestion);

        return new ResponseEntity<>(defaultQuestion, HttpStatus.OK);
    }

}
