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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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

import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.model.SubjectiveBlock;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.exam.service.MarkerService;
import cn.com.qmth.stmms.biz.exam.service.SubjectiveBlockService;
import cn.com.qmth.stmms.biz.subjectheader.service.SubjectHeaderService;
import cn.com.qmth.stmms.common.enums.MarkStage;
import cn.com.qmth.stmms.common.session.model.StmmsSession;
import cn.com.qmth.stmms.common.utils.RequestUtils;

@Controller
@RequestMapping("/subjectheader/distribute")
public class DistributeController {

    @Autowired
    private SubjectiveBlockService subjectiveBlockService;

    @Autowired
    private SubjectHeaderService subjectHeaderService;

    @Autowired
    private ExamSubjectService examSubjectService;

    @Autowired
    private MarkerService markerService;

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

        ExamSubject subject = examSubjectService.find(Integer.parseInt(examId), subjectCode);

        List<SubjectiveBlock> subjectiveBlockList = subjectiveBlockService.findBlockByExamIdAndSubjectCode(
                Integer.parseInt(examId), subjectCode);
        List<SubjectiveBlock> delList = new ArrayList<SubjectiveBlock>();
        if (delList.size() > 0) {
            subjectiveBlockList.removeAll(delList);
        }
        for (SubjectiveBlock subjectiveBlock : subjectiveBlockList) {
            // 已评
            int totalCount = 0;
            int toDispatchedCount = 0;
            subjectiveBlock.setTotalCount(totalCount);
            subjectiveBlock.setToDispatchedCount(toDispatchedCount);

        }

        ModelAndView view = new ModelAndView("subjectheader/distribute/list");
        view.addObject("subjectiveBlockList", subjectiveBlockList);
        view.addObject("stage", MarkStage.FORMAL);
        view.addObject("markSubjectStatus", subject.getStatus().getName());
        return view;
    }

    @RequestMapping(value = "/do", method = RequestMethod.POST)
    @ResponseBody
    public boolean distribute(HttpServletRequest request, @RequestBody List<Map<String, String>> list) {
        StmmsSession session = RequestUtils.getSession(request);
        Map<String, String> m = list.get(0);
        Integer stage = Integer.parseInt(m.get("value"));

        // 把科目状态放入session
        session.setParameter("subjectStage", stage.toString());

        Integer examId = Integer.parseInt(session.getParameter("examId"));
        String subjectCode = session.getParameter("subjectCode");

        for (int i = 1; i < list.size(); i++) {
            Map<String, String> map = list.get(i);
            SubjectiveBlock sb = subjectiveBlockService.findBlockById(Integer.parseInt(map.get("id")));
            if (Double.valueOf(map.get("reMarkPercent")).doubleValue() != sb.getReMarkPercent().doubleValue()) {
                sb.setReMarkPercent(Double.valueOf(map.get("reMarkPercent")));
            }
            if (Integer.parseInt(map.get("markSelfFrequency")) != sb.getMarkSelfFrequency().intValue()) {
                sb.setMarkSelfFrequency(Integer.parseInt(map.get("markSelfFrequency")));
            }
            if (Integer.parseInt(map.get("standardFrequency")) != sb.getStandardFrequency().intValue()) {
                sb.setStandardFrequency(Integer.parseInt(map.get("standardFrequency")));
            }
            if (Double.valueOf(map.get("mistakeValue")).doubleValue() != sb.getMistakeValue().doubleValue()) {
                sb.setMistakeValue(Double.valueOf(map.get("mistakeValue")).doubleValue());
            }
            if (Boolean.parseBoolean(map.get("avg")) != sb.isAvg()) {
                sb.setAvg(Boolean.parseBoolean(map.get("avg")));
            }
            // 为评卷员分配评卷数量
            boolean isAvg = Boolean.parseBoolean(map.get("avg"));
            updateBlockMarker(isAvg, examId, subjectCode, map, sb.getId());
            // 更新redis
            // updateRedis(sb.getId(), map);

            subjectiveBlockService.saveBlock(sb);
        }
        return true;
    }

    private void updateBlockMarker(boolean isAvg, Integer examId, String subjectCode, Map<String, String> map,
            Integer blockId) {
        if (isAvg) {
            int paperCount = 0;
            int studentCount = 0;
            paperCount += studentCount;
            if (Double.valueOf(map.get("reMarkPercent")).doubleValue() > 0) {
                Double reMarkCount = studentCount * Double.valueOf(map.get("reMarkPercent")).doubleValue();
                paperCount += reMarkCount.intValue();
            }
            if (Integer.parseInt(map.get("markSelfFrequency")) > 0) {
                int selfCount = studentCount % Integer.parseInt(map.get("markSelfFrequency"));
                paperCount += selfCount;
            }
            if (Integer.parseInt(map.get("exampleFrequency")) > 0) {
                int exampleCount = studentCount % Integer.parseInt(map.get("exampleFrequency"));
                paperCount += exampleCount;
            }

        }
    }
}
