package cn.com.qmth.stmms.subjectheader.arbitration;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.qmth.stmms.biz.distributpaper.model.FormallyLibrary;
import cn.com.qmth.stmms.biz.distributpaper.model.TryLibrary;
import cn.com.qmth.stmms.biz.distributpaper.service.FormallyLibraryService;
import cn.com.qmth.stmms.biz.distributpaper.service.TryLibraryService;
import cn.com.qmth.stmms.biz.exam.model.SubjectiveBlock;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.exam.service.SubjectiveBlockService;
import cn.com.qmth.stmms.biz.subjectheader.model.ArbitrationHistory;
import cn.com.qmth.stmms.biz.subjectheader.model.ArbitrationLibrary;
import cn.com.qmth.stmms.biz.subjectheader.model.SubjectHeader;
import cn.com.qmth.stmms.biz.subjectheader.query.ArbitrationLibrarySearchQuery;
import cn.com.qmth.stmms.biz.subjectheader.service.ArbitrationHistoryService;
import cn.com.qmth.stmms.biz.subjectheader.service.ArbitrationLibraryService;
import cn.com.qmth.stmms.biz.subjectheader.service.SubjectHeaderService;
import cn.com.qmth.stmms.common.controller.BaseController;
import cn.com.qmth.stmms.common.enums.LibraryStatus;
import cn.com.qmth.stmms.common.session.model.StmmsSession;
import cn.com.qmth.stmms.common.utils.RequestUtils;

@Controller
@RequestMapping("/subjectheader/arbitration")
public class ArbitrationController extends BaseController {

    @Autowired
    private SubjectiveBlockService subjectiveBlockService;

    @Autowired
    private ArbitrationLibraryService arbitrationLibraryService;

    @Autowired
    private SubjectHeaderService subjectHeaderService;

    @Autowired
    private ArbitrationHistoryService arbitrationHistoryService;

    @Autowired
    private TryLibraryService tryLibraryService;

    @Autowired
    private FormallyLibraryService formallyLibraryService;

    @Autowired
    private ExamSubjectService examSubjectService;

    @Autowired
    private ExamService examService;

    @Value("${slice.image.server}")
    private String imageServer;

    @RequestMapping
    public ModelAndView index(HttpServletRequest request) {
        StmmsSession session = RequestUtils.getSession(request);
        int examId = Integer.parseInt(session.getParameter("examId"));
        String subjectCode = session.getParameter("subjectCode");

        List<SubjectiveBlock> bList = null;
        bList = subjectiveBlockService.findBlockByExamIdAndSubjectCode(examId, subjectCode);
        ModelAndView view = new ModelAndView("subjectheader/arbitration/list");
        view.addObject("bList", bList);
        return view;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> pageList(HttpServletRequest request, @RequestParam Integer page,
            @RequestParam Integer rows, ArbitrationLibrarySearchQuery query) {
        StmmsSession session = RequestUtils.getSession(request);
        String examId = session.getParameter("examId");
        String subjectId = session.getParameter("subjectId");
        String classType = session.getParameter("classType");

        query.setExamId(Integer.parseInt(examId));
        query.setSubjectId(Integer.parseInt(subjectId));
        query.setClassType(Integer.parseInt(classType));
        query.setPageNumber(page);
        query.setPageSize(rows);
        query.setStatus(LibraryStatus.WAITING);
        query = arbitrationLibraryService.findByQuery(query);

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("rows", query.getResult());
        result.put("total", query.getTotalCount());
        return result;
    }

    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public ModelAndView view(HttpServletRequest request, @PathVariable Integer id) {
        ArbitrationLibrary arbitrationLibrary = arbitrationLibraryService.findById(id);
        SubjectiveBlock subjectiveBlock = subjectiveBlockService.findBlockById(arbitrationLibrary.getBlockId(), true);
        SubjectHeader subjectHeader = RequestUtils.getWebUser(request).getSubjectHeader();

        ModelAndView view = new ModelAndView("subjectheader/arbitration/view");
        StmmsSession session = RequestUtils.getSession(request);
        session.setParameter("imageServer", imageServer);
        view.addObject("imageServer", imageServer);

        view.addObject("arbitrationLibrary", arbitrationLibrary);
        view.addObject("subjectiveBlock", subjectiveBlock);
        view.addObject("subjectHeader", subjectHeader);
        view.addObject("startTime", new Date().getTime());
        return view;
    }

    @RequestMapping(value = "/mark", method = RequestMethod.POST)
    public ModelAndView mark(HttpServletRequest request, ArbitrationHistory arbitrationHistory) {
        SubjectHeader subjectHeader = RequestUtils.getWebUser(request).getSubjectHeader();
        arbitrationHistory.setSubmitter(subjectHeader.getId());

        arbitrationHistoryService.saveMark(arbitrationHistory);

        ArbitrationLibrary arbitrationLibrary = arbitrationLibraryService.findById(arbitrationHistory.getLibraryId());

        if (arbitrationLibrary.getType().getValue() == 1) {
            // 试评
            List<TryLibrary> tryLibraryList = tryLibraryService.findByBlockIdAndSecretNo(
                    arbitrationLibrary.getBlockId(), arbitrationLibrary.getSecretNo());
            for (TryLibrary tryLibrary : tryLibraryList) {
                tryLibrary.setHeaderScore(arbitrationHistory.getTotalScore());
                tryLibrary.setHeaderScoreList(arbitrationHistory.getScoreList());
                tryLibrary.setStatus(LibraryStatus.MARKED);
                tryLibraryService.save(tryLibrary);
            }
        } else {
            // 正评
            List<FormallyLibrary> formallyLibraryList = formallyLibraryService.findByBlockIdAndSecretNo(
                    arbitrationLibrary.getBlockId(), arbitrationLibrary.getSecretNo());
            for (FormallyLibrary formallyLibrary : formallyLibraryList) {
                formallyLibrary.setHeaderScore(arbitrationHistory.getTotalScore());
                formallyLibrary.setHeaderScoreList(arbitrationHistory.getScoreList());
                formallyLibrary.setStatus(LibraryStatus.MARKED);
                formallyLibraryService.save(formallyLibrary);
                // 记录已给分任务
                // BlockTaskCountUtil.addBlockMarkedTask(stringRedisTemplate,
                // formallyLibrary.getBlockId(),
                // formallyLibrary.getId());
            }
        }
        return new ModelAndView("redirect:/subjectheader/arbitration");
    }
}
