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

import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.api.ExtractConfigCloudService;
import cn.com.qmth.examcloud.core.questions.api.request.*;
import cn.com.qmth.examcloud.core.questions.api.response.*;
import cn.com.qmth.examcloud.core.questions.service.ExtractConfigProviderService;
import cn.com.qmth.examcloud.core.questions.service.ExtractConfigService;
import cn.com.qmth.examcloud.core.questions.service.RandomPaperService;
import cn.com.qmth.examcloud.core.questions.service.bean.extract.ExtractConfigPaper;
import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
import cn.com.qmth.examcloud.question.commons.core.question.DefaultQuestion;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author weiwenhai
 * @date 2018.8.15
 * @company qmth
 * @describle 调卷规则远程调用请求接口实现类
 * @code 010
 */
@Transactional
@RestController
@RequestMapping("${$rmp.cloud.questions}" + "extract_config")
public class ExtractConfigCloudServiceProvider implements ExtractConfigCloudService {

    private static final Logger LOG = LoggerFactory.getLogger(ExtractConfigCloudServiceProvider.class);

    private static final long serialVersionUID = 2346453260179681094L;

    @Autowired
    private ExtractConfigProviderService extractConfigExamService;
    @Autowired
    private ExtractConfigService extractConfigService;
    
    @Autowired
    private RandomPaperService randomPaperService;

    @ApiOperation(value = "根据调卷规则抽取考试试卷")
    @PostMapping("getPaper")
    @Override
    public GetPaperResp getPaper(@RequestBody GetPaperReq paperReq) {
        if (paperReq.getExamId() == null) {
            throw new StatusException("400", "examId is null");
        }

        if (StringUtils.isBlank(paperReq.getCourseCode())) {
            throw new StatusException("400", "courseCode is empty");
        }

        if (StringUtils.isBlank(paperReq.getGroupCode())) {
            throw new StatusException("400", "groupCode is empty");
        }

        ExtractConfigPaper result = extractConfigExamService.getDefaultPaper(paperReq.getExamId(), paperReq.getCourseCode(), paperReq.getGroupCode());

        GetPaperResp resp = new GetPaperResp();
        resp.setPaperId(result.getPaperId());
        resp.setSortQuestionOrder(result.getSortQuestionOrder());
        resp.setSortOptionOrder(result.getSortOptionOrder());
        resp.setDefaultPaper(result.getDefaultPaper());

        return resp;
    }

    @ApiOperation(value = "根据调卷规则抽取考试试题")
    @PostMapping("getQuestion")
    @Override
    public GetQuestionResp getQuestion(@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);
        GetQuestionResp resp = new GetQuestionResp();
        resp.setDefaultQuestion(defaultQuestion);
        return resp;
    }


    @ApiOperation(value = "根据试卷id获取试卷结构")
    @PostMapping("getBasePaper")
    @Override
    public GetBasePaperResp getBasePaper(@RequestBody GetBasePaperReq req) {
        String paperId = req.getPaperId();
        if (null == paperId) {
            throw new StatusException("Q-010105", "paperId is null");
        }
        DefaultPaper defaultPaper = extractConfigExamService.getBaseDefaultPaper(paperId);
        GetBasePaperResp resp = new GetBasePaperResp();
        resp.setDefaultPaper(defaultPaper);
        return resp;
    }

    @ApiOperation(value = "根据试题id集合获取试题集合")
    @PostMapping("getQuestionList")
    @Override
    public GetQuestionListResp getQuestionList(@RequestBody GetQuestionListReq req) {
        Set<String> questionIds = req.getQuestionIds();
        if (questionIds == null || questionIds.size() < 1) {
            throw new StatusException("Q-010119", "questionIds is null");
        }
        Map<String, DefaultQuestion> map = extractConfigExamService.getDefaultQuestions(questionIds);
        GetQuestionListResp resp = new GetQuestionListResp();
        resp.setMap(map);
        return resp;
    }

    @Override
    @ApiOperation(value = "跟试题id查询试题答案")
    @PostMapping("getQuestionAnswerList")
    public GetQuestionAnswerResp getQuestionAnswer(GetQuestionAnswerReq req) {
        if (StringUtils.isBlank(req.getQuestionId())) {
            throw new StatusException("Q-010135", "questionId is null");
        }
        List<String> list = extractConfigExamService.getAnswer(req.getQuestionId());
        GetQuestionAnswerResp resp = new GetQuestionAnswerResp();
        resp.setAnswerList(list);
        return resp;
    }

    @ApiOperation(value = "根据试题id集合获取试题集合")
    @PostMapping("getBaseQuestion")
    @Override
    public GetQuestionResp getBaseQuestion(@RequestBody GetQuestionReq req) {
        String id = req.getQuestionId();
        if (StringUtils.isBlank(id)) {
            throw new StatusException("Q-010149", "questionId is null");
        }
        DefaultQuestion defaultQuestion = extractConfigExamService.getDefaultQuestion(null, null, null, id);
        GetQuestionResp resp = new GetQuestionResp();
        resp.setDefaultQuestion(defaultQuestion);
        return resp;
    }

    @ApiOperation(value = "根据试卷id查询主观题与客观题数量")
    @PostMapping("getQuestionStructure")
    @Override
    public GetQuestionListResp getQuestionStructure(@RequestBody GetBasePaperReq req) {
        String paperId = req.getPaperId();
        if (null == paperId) {
            throw new StatusException("Q-010163", "paperId is null");
        }
        try {
            String json = extractConfigExamService.getQuestionStructure(paperId);
            GetQuestionListResp resp = new GetQuestionListResp();
            resp.setJson(json);
            return resp;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
    }
    
    @ApiOperation("随机抽卷")
    @PostMapping("randompaper")
    @Override
	public GetRandomPaperResp getRandomPaper(@RequestBody GetRandomPaperReq req) {
    	if (StringUtils.isBlank(req.getRandomPaperId())) {
            throw new StatusException("RandomPaperId is null");
        }
    	if (req.getPlayTime()==null) {
            throw new StatusException("PlayTime is null");
        }
    	GetRandomPaperResp res=new GetRandomPaperResp();
    	DefaultPaper defaultPaper=randomPaperService.getRandomPaper(req.getRandomPaperId(),req.getPlayTime());
    	res.setDefaultPaper(defaultPaper);
		return res;
	}

    @ApiOperation("随机抽卷调卷规则创建")
    @PostMapping("randompaper/save")
    @Override
	public SaveRandomPaperExtractConfigResp saveRandomPaperExtractConfig(@RequestBody SaveRandomPaperExtractConfigReq req) {
    	extractConfigService.saveExtractConfigForRandomPaperByOuter(req);
    	SaveRandomPaperExtractConfigResp res=new SaveRandomPaperExtractConfigResp();
		return res;
	}

}
