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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.qmth.stmms.biz.exam.model.Exam;
import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.model.MarkGroup;
import cn.com.qmth.stmms.biz.exam.service.ExamQuestionService;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.exam.service.MarkGroupService;
import cn.com.qmth.stmms.biz.file.service.FileService;
import cn.com.qmth.stmms.biz.mark.model.MarkLibrary;
import cn.com.qmth.stmms.biz.mark.model.MarkSpecialTag;
import cn.com.qmth.stmms.biz.mark.model.MarkTrack;
import cn.com.qmth.stmms.biz.mark.model.Task;
import cn.com.qmth.stmms.biz.mark.model.TrialLibrary;
import cn.com.qmth.stmms.biz.mark.service.MarkLibraryService;
import cn.com.qmth.stmms.biz.mark.service.MarkSpecialTagService;
import cn.com.qmth.stmms.biz.mark.service.MarkTrackService;
import cn.com.qmth.stmms.biz.mark.service.TaskService;
import cn.com.qmth.stmms.biz.mark.service.TrialService;

@Controller("trackController")
@RequestMapping("/admin/exam/track")
public class MarkTrackController extends BaseExamController {

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

    @Autowired
    private MarkTrackService markTrackService;

    @Autowired
    private MarkGroupService groupService;

    @Autowired
    private ExamStudentService studentService;

    @Autowired
    private MarkLibraryService libraryService;

    @Autowired
    private MarkSpecialTagService markSpecialTagService;

    @Autowired
    private ExamQuestionService questionService;

    @Autowired
    private FileService fileService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private TrialService trialService;

    @Autowired
    private ExamService examService;

    @Autowired
    private ExamSubjectService subjectService;

    @Value("${slice.split.config}")
    private String splitConfig;

    @ResponseBody
    @RequestMapping("/student")
    public Task studentTrack(@RequestParam Integer studentId) {
        Task task = new Task();
        ExamStudent student = studentService.findById(studentId);

        if (student != null && student.isUpload()) {
            // List<String> sliceUrls =
            // fileService.getSliceUris(student.getExamId(),
            // student.getSecretNumber(), 1,
            // student.getSliceCount());
            // map.accumulate("sliceUrls", sliceUrls);
            // Map<MarkGroup, List<OriginTag>> maps =
            // studentService.getSliceTags(student, false);
            // List<OriginTag> tags = new ArrayList<OriginTag>();
            // for (Entry<MarkGroup, List<OriginTag>> entry : maps.entrySet()) {
            // tags.addAll(entry.getValue());
            // }
            // List<SpecialTagDTO> list = new ArrayList<SpecialTagDTO>();
            // for (OriginTag originTag : tags) {
            // SpecialTagDTO dto = new SpecialTagDTO();
            // dto.setOffsetIndex(originTag.getOffsetIndex());
            // dto.setOffsetX(originTag.getOffsetX());
            // dto.setOffsetY(originTag.getOffsetY());
            // dto.setTagName(originTag.getContent());
            // list.add(dto);
            // }
            // map.accumulate("tagList", list);
            // map.accumulate("examNumber", student.getExamNumber());
            task = taskService.build(student);
        }
        return task;
    }

    /**
     *
     */
    @ResponseBody
    @RequestMapping
    public HashMap<String, Object> list(Integer studentId) {
        List<MarkLibrary> librarys = libraryService.findByStudentId(studentId);
        ExamStudent examStudent = studentService.findById(studentId);
        List<Object> list = new ArrayList<Object>();
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (MarkLibrary library : librarys) {
            if (null != library.getTaskNumber() && library.getTaskNumber() == 2) {
                continue;
            }
            HashMap<String, Object> groups = set(library, examStudent);
            list.add(groups);
        }
        map.put("list", list);
        map.put("fileServer", fileService.getFileServer());
        return map;
    }

    @ResponseBody
    @RequestMapping("/library")
    public HashMap<String, Object> byLibrary(@RequestParam Integer libraryId) {
        MarkLibrary library = libraryService.findById(libraryId);
        HashMap<String, Object> map = new HashMap<String, Object>();
        Task task = taskService.build(library);
        map.put("task", task);
        map.put("groupNumber", library.getGroupNumber());
        map.put("fileServer", fileService.getFileServer());
        map.put("splitConfig", getSplitConfig());
        Exam exam = examService.findById(library.getExamId());
        ExamSubject examSubject = subjectService.find(library.getExamId(), library.getSubjectCode());
        map.put("enableSplit",
                examSubject.getEnableSplit() == null ? exam.isEnableSplit() : examSubject.getEnableSplit());
        return map;
    }

    private double[] getSplitConfig() {
        String strs[] = splitConfig.split(",");
        double[] config = new double[strs.length];
        for (int i = 0; i < strs.length; i++) {
            config[i] = Double.parseDouble(strs[i]);
        }
        return config;
    }

    private HashMap<String, Object> set(MarkLibrary library, ExamStudent student) {
        HashMap<String, Object> groups = new HashMap<String, Object>();
        MarkGroup group = groupService.findOne(student.getExamId(), student.getSubjectCode(), library.getGroupNumber());
        group.setQuestionList(questionService.findByExamAndSubjectAndObjectiveAndGroupNumber(student.getExamId(),
                student.getSubjectCode(), false, group.getNumber()));
        List<String> picUrls = fileService.getSliceUris(student.getExamId(), student.getSecretNumber(), 1,
                student.getSliceCount());
        List<MarkTrack> markTracks = markTrackService.findByLibraryId(library.getId());
        List<MarkSpecialTag> markSpecialTagList = markSpecialTagService.findByLibraryId(library.getId());
        groups.put("picUrls", picUrls);
        groups.put("pictureConfig", group.getPictureConfigList());
        groups.put("markTracks", markTracks);
        groups.put("groupTitle", group.getTitle());
        groups.put("groupNumber", group.getNumber());
        groups.put("markSpecialTagList", markSpecialTagList);
        return groups;
    }

    @ResponseBody
    @RequestMapping("/trialLibrary")
    public HashMap<String, Object> trialLibrary(@RequestParam Integer libraryId) {
        TrialLibrary library = trialService.findById(libraryId);
        HashMap<String, Object> map = new HashMap<String, Object>();
        Task task = taskService.build(library);
        map.put("task", task);
        map.put("groupNumber", library.getGroupNumber());
        map.put("fileServer", fileService.getFileServer());
        map.put("splitConfig", getSplitConfig());
        Exam exam = examService.findById(library.getExamId());
        ExamSubject examSubject = subjectService.find(library.getExamId(), library.getSubjectCode());
        map.put("enableSplit",
                examSubject.getEnableSplit() == null ? exam.isEnableSplit() : examSubject.getEnableSplit());
        return map;
    }
}
