package cn.com.qmth.stmms.admin.exam;

import cn.com.qmth.stmms.biz.mark.model.ProblemType;
import cn.com.qmth.stmms.biz.mark.service.ProblemHistoryService;
import cn.com.qmth.stmms.biz.mark.service.ProblemTypeService;
import cn.com.qmth.stmms.common.annotation.Logging;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.LogType;
import cn.com.qmth.stmms.common.utils.RequestUtils;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

@Controller
@RequestMapping("/admin/exam/problem/type")
public class ProblemTypeController extends BaseExamController {

    protected static Logger log = LoggerFactory.getLogger(ProblemTypeController.class);

    @Autowired
    private ProblemTypeService problemService;

    @Autowired
    private ProblemHistoryService historyService;

    @Logging(menu = "查询问题卷分类", type = LogType.QUERY)
    @RequestMapping
    public String list(HttpServletRequest request, Model model, @RequestParam Integer examId) {
        List<ProblemType> list = problemService.findByExamId(examId);
        model.addAttribute("list", list);
        model.addAttribute("examId", examId);
        return "modules/exam/problemList";
    }

    @RequestMapping("/add")
    public String add(ProblemType problem, Model model, @RequestParam Integer examId) {
        model.addAttribute("problem", problem);
        model.addAttribute("examId", examId);
        return "modules/exam/problemForm";
    }

    @Logging(menu = "新增问题卷分类", type = LogType.ADD)
    @RequestMapping("/save")
    public String save(HttpServletRequest request, ProblemType problem, RedirectAttributes redirectAttributes,
            @RequestParam Integer examId) {
        List<ProblemType> list = problemService.findByExamId(examId);
        if (list.size() >= 10) {
            addMessage(redirectAttributes, "问题卷类型不能超过10个");
            return "redirect:/admin/exam/problem/type?examId=" + examId;
        }
        if (!StringUtils.isNotBlank(problem.getName())) {
            addMessage(redirectAttributes, "名称不能为空");
            return "redirect:/admin/exam/problem/type?examId=" + examId;
        }
        problem.setExamId(examId);
        problem.setCustom(true);
        problem = problemService.save(problem);
        addMessage(redirectAttributes, "创建'" + problem.getName() + "'成功");
        return "redirect:/admin/exam/problem/type?examId=" + examId;
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        ProblemType problem = problemService.findById(id);
        model.addAttribute("problem", problem);
        return "modules/exam/problemEdit";
    }

    @Logging(menu = "编辑问题卷分类", type = LogType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String examEdit(HttpServletRequest request, ProblemType problem) {
        WebUser user = RequestUtils.getWebUser(request);
        ProblemType old = problemService.findById(problem.getId());
        if (user.isSchoolAdmin() && StringUtils.isNotBlank(problem.getName())) {
            old.setName(problem.getName());
            problemService.save(old);
        }
        return "redirect:/admin/exam/problem/type?examId=" + old.getExamId();
    }

    @Logging(menu = "删除问题卷分类", type = LogType.DELETE)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(HttpServletRequest request, RedirectAttributes redirectAttributes,
            @RequestParam Integer problemId) {
        int examId = getSessionExamId(request);
        JSONObject result = new JSONObject();
        long historyCount = historyService.countByExamIdAndProblemId(examId, problemId);
        if (historyCount > 0) {
            result.accumulate("success", false);
            result.accumulate("message", "该问题卷类型已被使用，不可删除");
        } else {
            problemService.deleteById(problemId);
            result.accumulate("success", true);
            RequestUtils.setLog(request, "删除成功，id:" + problemId);
        }
        return result;
    }
}
