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

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.CaseLibrary;
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.CaseLibraryService;
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.SubjectHeader;
import cn.com.qmth.stmms.biz.subjectheader.query.CaseLibrarySearchQuery;
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/case")
public class CaseController extends BaseController {

    @Autowired
    private CaseLibraryService caseLibraryService;

    @Autowired
    private SubjectiveBlockService subjectiveBlockService;

    @Autowired
    private SubjectHeaderService subjectHeaderService;

    @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/case/list");
        view.addObject("bList", bList);
        return view;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> pageList(HttpServletRequest request, @RequestParam Integer page,
            @RequestParam Integer rows, CaseLibrarySearchQuery query) {
        StmmsSession session = RequestUtils.getSession(request);
        String examId = session.getParameter("examId");
        String subjectCode = session.getParameter("subjectCode");
        SubjectHeader subjectHeader = RequestUtils.getWebUser(request).getSubjectHeader();

        if (query.getBlockId() != null && query.getCount() != null) {
            caseLibraryService.example(Integer.parseInt(examId), subjectCode, query.getBlockId(), query.getCount(),
                    subjectHeader.getId());
        }

        query.setExamId(Integer.parseInt(examId));
        query.setSubjectCode(subjectCode);
        query.setPageNumber(page);
        query.setPageSize(rows);
        query = caseLibraryService.findByQuery(query);

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

    @RequestMapping(value = "/mark/{id}", method = RequestMethod.GET)
    public ModelAndView mark(HttpServletRequest request, @PathVariable Integer id) {
        CaseLibrary caseLibrary = caseLibraryService.findById(id);

        List<CaseLibrary> list = caseLibraryService.findByExamIdAndBlockId(caseLibrary.getExamId(),
                caseLibrary.getBlockId());
        SubjectHeader subjectHeader = RequestUtils.getWebUser(request).getSubjectHeader();
        StmmsSession session = RequestUtils.getSession(request);
        ModelAndView view = new ModelAndView("subjectheader/case/view");
        view.addObject("list", list);
        view.addObject("subjectHeader", subjectHeader);
        view.addObject("caseLibraryId", id);
        session.setParameter("imageServer", imageServer);
        view.addObject("imageServer", imageServer);

        return view;
    }

    @RequestMapping("/getlibrary")
    @ResponseBody
    public CaseLibrary getLibrary(HttpServletRequest request, @RequestParam Integer libraryId) {
        CaseLibrary caseLibrary = caseLibraryService.findById(libraryId);
        SubjectiveBlock block = subjectiveBlockService.findBlockById(caseLibrary.getBlockId());
        caseLibrary.setSubjectiveBlock(block);
        caseLibrary.setPictureConfig(block.getPictureConfig());
        // caseLibrary.setPictureUrls(PictureUrlBuilder.getSliceUrls(caseLibrary.getExamId(),
        // caseLibrary.getCampusCode(),
        // block.getSubjectCode(), caseLibrary.getExamNumber(),
        // caseLibrary.getPicCount()));
        return caseLibrary;
    }

    @RequestMapping(value = "/mark", method = RequestMethod.POST)
    public ModelAndView save(HttpServletRequest request, @RequestParam Integer caseLibraryId,
            @RequestParam String scoreList, @RequestParam double totalScore) {
        SubjectHeader subjectHeader = RequestUtils.getWebUser(request).getSubjectHeader();
        CaseLibrary caseLibrary = caseLibraryService.findById(caseLibraryId);
        caseLibrary.setScoreList(scoreList);
        caseLibrary.setTotalScore(totalScore);
        caseLibrary.setStatus(LibraryStatus.MARKED);
        caseLibrary.setSampleTime(new Date());
        caseLibrary.setSampler(subjectHeader.getId());
        caseLibraryService.save(caseLibrary);

        // 试评
        List<TryLibrary> tryLibraryList = tryLibraryService.findByBlockIdAndSecretNo(caseLibrary.getBlockId(),
                caseLibrary.getSecretNo());
        for (TryLibrary tryLibrary : tryLibraryList) {
            tryLibrary.setHeaderScore(totalScore);
            tryLibrary.setHeaderScoreList(scoreList);
            tryLibrary.setStatus(LibraryStatus.MARKED);
            tryLibraryService.save(tryLibrary);
        }
        // 正评
        List<FormallyLibrary> formallyLibraryList = formallyLibraryService.findByBlockIdAndSecretNo(
                caseLibrary.getBlockId(), caseLibrary.getSecretNo());
        for (FormallyLibrary formallyLibrary : formallyLibraryList) {
            formallyLibrary.setHeaderScore(totalScore);
            formallyLibrary.setHeaderScoreList(scoreList);
            formallyLibrary.setStatus(LibraryStatus.MARKED);
            formallyLibraryService.save(formallyLibrary);
        }
        ModelAndView view = new ModelAndView("redirect:/subjectheader/case/mark/" + caseLibraryId);
        return view;
    }
}
