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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.qmth.stmms.admin.utils.SessionExamUtils;
import cn.com.qmth.stmms.biz.exam.model.Exam;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.utils.RequestUtils;

@Controller
@RequestMapping("/admin/home")
public class HomeController {

    @Autowired
    private ExamService examService;

    @RequestMapping
    public String index(HttpServletRequest request, Model model) {
        WebUser user = RequestUtils.getWebUser(request);
        if (user.getRole() == Role.SYS_ADMIN) {
            return "modules/sys/sysIndex";
        } else {
            int examId = SessionExamUtils.getExamId(request);
            if (examId > 0) {
                Exam exam = examService.findById(examId);
                model.addAttribute("examName", examId + "-" + exam.getName());
                return "modules/exam/examIndex";
            }
            return "redirect:exam/select";
        }
    }

    /**
     * 获取主题方案
     */
    @RequestMapping(value = "/theme/{theme}")
    public String getThemeInCookie(@PathVariable String theme, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isNotBlank(theme)) {
            RequestUtils.setCookie(response, "theme", theme, 60 * 60 * 24);
        } else {
            theme = RequestUtils.getCookie(request, "theme");
        }
        return "redirect:" + request.getParameter("url");
    }

}
