package com.qmth.qrzk.web.controller;

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

import javax.servlet.http.HttpSession;

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

import com.qmth.qrzk.repository.task.PaperTask;
import com.qmth.qrzk.repository.user.model.TeacherAssessment;
import com.qmth.qrzk.repository.user.model.TeacherAssessmentDetail;
import com.qmth.qrzk.repository.user.model.User;
import com.qmth.qrzk.repository.user.service.IUserService;
import com.qmth.qrzk.repository.user.service.impl.PaperTaskServiceImpl;
import com.qmth.qrzk.repository.user.service.impl.TeacherAssessmentDetailServiceImpl;
import com.qmth.qrzk.repository.user.service.impl.TeacherAssessmentServiceImpl;
import com.qmth.qrzk.repository.utils.Consts;

@Controller
@RequestMapping("/teacherAssessment")
public class TeacherAssessmentController extends BaseAjaxController {
	private static final String BASE_JSP_PATH = "teacherAssessment/";

	@Autowired
	private PaperTaskServiceImpl paperTaskService;

	@Autowired
	private TeacherAssessmentServiceImpl teacherAssessmentService;

	@Autowired
	private TeacherAssessmentDetailServiceImpl teacherAssessmentDetailService;

	@Autowired
	@Qualifier("userServiceImpl")
	private IUserService userService;

	@RequestMapping("/view/{taskId}/{teacherId}")
	public ModelAndView view(HttpSession session, @PathVariable int taskId,
			@PathVariable int teacherId) {
		ModelAndView mov = new ModelAndView(BASE_JSP_PATH + "view");

		PaperTask task = paperTaskService.getPaperTaskById(taskId);
		User teacher = userService.findById(teacherId);
		User curUser = this.getCurUser(session);
		
		TeacherAssessment assessment =  teacherAssessmentService.queryAssessmentByTeacherIdAndTaskId(teacherId, taskId);

		Map<String, Object> assessmentMap = new HashMap<String, Object>();
		
		if(assessment != null && assessment.getDetails() != null){
			for(int i = 0; i < assessment.getDetails().size(); i++){
				TeacherAssessmentDetail detail = assessment.getDetails().get(i);
				assessmentMap.put("score_" + i, detail.getScore());
				assessmentMap.put("remark_" + i, detail.getRemark());
			}
			curUser = Consts.UserMap.get(assessment.getCreateById());
		}
		mov.addObject("assessment", assessment);
		mov.addObject("assessmentMap", assessmentMap);
		mov.addObject("task", task);
		mov.addObject("teacher", teacher);
		mov.addObject("curUser", curUser);

		return mov;
	}

	@RequestMapping("/getAssessmentByTaskAndUserId/{taskId}/{userId}")
	@ResponseBody
	public Map<String, Object> getAssessmentByTaskAndUserId(HttpSession session, @PathVariable int taskId, @PathVariable int userId){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			TeacherAssessment assessment =  teacherAssessmentService.queryAssessmentByTeacherIdAndTaskId(userId, taskId);
			result.put("assessment", assessment);
			
			User curUser = this.getCurUser(session);
			result.put("curUser", curUser);
			
			PaperTask task = paperTaskService.getPaperTaskById(taskId);
			result.put("task", task);
			
			result.put("flag", "success");
		}catch(Exception e){
			return failedResult("获取评分数据失败!");
		}
		
		return result;
	}
	
	@RequestMapping("/setScore")
	@ResponseBody
	public Map<String, Object> setScore(@RequestBody TeacherAssessment teacher) {
		try {
			TeacherAssessment assessmentDB =  teacherAssessmentService.queryAssessmentByTeacherIdAndTaskId(teacher.getTeacherId(), teacher.getTaskId());
			if(assessmentDB == null)
				teacherAssessmentService.addTeacherAssessment(teacher);
			else{
				teacher.setId(assessmentDB.getId());
				teacherAssessmentService.update(teacher);
			}
			
			return successResult("评分成功");
		} catch (Exception e) {
			// log.error("保存难度预估表失败!");
			e.printStackTrace();
			return failedResult("评分失败!");
		}
	}
	
	@RequestMapping("/getAssessmentsByBatchId/{batchId}")
	@ResponseBody
	public Map<String, Object> getAssessmentsByBatchId(HttpSession session, @PathVariable int batchId){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			List<TeacherAssessment> assessments =  teacherAssessmentService.queryAssessmentsByBatchId(batchId);
			result.put("assessments", assessments);
			
			result.put("flag", "success");
		}catch(Exception e){
			return failedResult("获取评分数据失败!");
		}
		
		return result;
	}

}
