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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.qmth.examcloud.api.commons.enums.DataRuleType;
import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.api.commons.security.bean.UserDataRule;
import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.api.request.GetRandomPaperReq;
import cn.com.qmth.examcloud.core.questions.api.response.GetRandomPaperResp;
import cn.com.qmth.examcloud.core.questions.service.RandomPaperService;
import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.PaperQuestionViewQuery;
import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.RandomPaperDomain;
import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.RandomPaperListVo;
import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.RandomPaperQuery;
import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.StructInfo;
import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
import cn.com.qmth.examcloud.web.security.DataRule;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "随机组卷模板")
@RequestMapping("${api_cqb}/randompaper")
public class RandomPaperController extends ControllerSupport {

	@Autowired
	private RandomPaperService randomPaperService;

	@DataRule(type = DataRuleType.COURSE)
	@ApiOperation("获取分页")
	@PostMapping("page")
	public Page<RandomPaperListVo> getPage(RandomPaperQuery req) {
		User user = getAccessUser();
		UserDataRule ud = super.getUserDataRule(DataRuleType.COURSE);
		req.setUd(ud);
		req.setRootOrgId(user.getRootOrgId());
		return randomPaperService.getPage(req);
	}
	
	@ApiOperation("获取信息")
	@PostMapping("info")
	public RandomPaperListVo getInfo(@RequestParam String id) {
		return randomPaperService.getInfo(id);
	}

	@ApiOperation(value = "启用、禁用")
	@PostMapping("toggle")
	public void toggle(@RequestParam String id, @RequestParam Boolean enable) {
		randomPaperService.toggle(id, enable, getAccessUser());
	}

	@ApiOperation(value = "获取组卷结构试题信息")
	@PostMapping("struct/question/info")
	public StructInfo getStructQuestionInfo(@RequestParam String structId) {
		if(StringUtils.isBlank(structId)) {
			throw new StatusException("组卷结构ID不能为空");
		}
		return randomPaperService.getStructQuestionInfo(structId);
	}

	@ApiOperation(value = "选中试卷范围预览")
	@PostMapping("struct/question/view/info")
	public StructInfo getPaperQuestionViewInfo(PaperQuestionViewQuery query) {
		return randomPaperService.getPaperQuestionViewInfo(query);
	}
	
	@ApiOperation(value = "保存模板")
	@PostMapping("save")
	public StructInfo save(RandomPaperDomain domain) {
		if (StringUtils.isBlank(domain.getId())) {
			domain.setRootOrgId(getAccessUser().getRootOrgId());
		}
		return randomPaperService.saveRandomPaper(domain);
	}
	
	/**组卷小工具调用
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "获取随机试卷")
	@PostMapping("create")
	public GetRandomPaperResp getRandomPaper(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;
	}
}
