package lingtao.net.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.Information;
import lingtao.net.bean.Msg;
import lingtao.net.bean.Question;
import lingtao.net.bean.SysUser;
import lingtao.net.service.InformationService;
import lingtao.net.service.QuestionService;
import lingtao.net.service.SysUserService;

@RestController
public class QuestionController {

	@Autowired
	private QuestionService questionService;

	@Autowired
	private InformationService informationService;

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 问题列表
	 * 
	 * @return
	 */
	@RequestMapping("/questions")
	public Msg questions(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, Question question) {
		PageHelper.startPage(page, limit);
		List<Question> questionList = questionService.questions(question);
		PageInfo<Question> pageInfo = new PageInfo<Question>(questionList);
		return Msg.success().add("list", pageInfo);
	}

	@RequestMapping("/getQuestions")
	public Msg getQuestions(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit) {
		PageHelper.startPage(page, limit);
		// 单选题
		List<Question> singleQuestionList = questionService.getSingleQuestions();
		// 多选题
		List<Question> multipleQuestionList = questionService.getMultipleQuestions();
		// 填空题
		List<Question> fillQuestionList = questionService.getFillQuestions();
		// 简答题
		List<Information> shortAnswerList = informationService.getShortAnswers();

		PageInfo<Question> single = new PageInfo<Question>(singleQuestionList);
		PageInfo<Question> multiple = new PageInfo<Question>(multipleQuestionList);
		PageInfo<Question> fill = new PageInfo<Question>(fillQuestionList);
		PageInfo<Information> shortAnswer = new PageInfo<Information>(shortAnswerList);
		// 转成json返回JSONArray.fromObject(questionList)
		return Msg.success().add("single", single).add("multiple", multiple).add("fill", fill).add("shortAnswer",
				shortAnswer);
	}

	/**
	 * 添加问题
	 */
	@RequestMapping("/addQuestion")
	public Msg addQuestion(Question question, HttpSession session) {
		questionService.addQuestion(question, session);
		return Msg.success();
	}

	/**
	 * 修改问题
	 */
	@RequestMapping("/updateQuestion")
	public Msg updateQuestion(Question question, HttpSession session) {
		questionService.updateQuestionById(question, session);
		return Msg.success();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/deleteQuestion")
	public Msg deleteQuestion(@RequestParam("id") Integer id) {
		questionService.deleteQuestionById(id);
		return Msg.success();
	}

	/**
	 * 答对80%后，改变系统状态
	 * 
	 * @return
	 */
	@RequestMapping("/videoOverToChangeSysStatus")
	public Msg videoOverToChangeSysStatus(HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		return sysUserService.videoOverToChangeSysStatus(user.getUserId());
	}
	@RequestMapping("/examOverToChangeSysStatus")
	public Msg examOverToChangeSysStatus(HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		return sysUserService.examOverToChangeSysStatus(user.getUserId());
	}
	/*
	 * @RequestMapping("/changeSysStatus") public Msg changeSysStatus(HttpSession
	 * session, @RequestBody Map<String, String> map) { System.out.println(map);
	 * Set<Entry<String, String>> entrySet = map.entrySet(); for (Entry<String,
	 * String> key : entrySet) { System.out.println(key); } SysUser user = (SysUser)
	 * session.getAttribute("USER_SESSION"); return
	 * sysUserService.changeSysStatus(user.getUserId()); }
	 */

}
