package cn.com.qmth.examcloud.tool.controller;

import cn.com.qmth.examcloud.tool.cache.LoginSessionManager;
import cn.com.qmth.examcloud.tool.config.Constants;
import cn.com.qmth.examcloud.tool.service.CommonService;
import cn.com.qmth.examcloud.tool.vo.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private CommonService commonService;

    @GetMapping(value = "/")
    public String index() {
        return "index";
    }

    @GetMapping(value = "/logout")
    public String logout() {
        User user = currentLoginUser();
        if (user != null) {
            LoginSessionManager.removeLoginSession(user.getToken());
            currentSession().removeAttribute(Constants.LOGIN_USER);
        }
        return "redirect:/login";
    }

    @GetMapping(value = "/login")
    public String login() {
        if (currentLoginUser() != null) {
            return "redirect:/admin/workspace";
        }
        return "login";
    }

    @PostMapping(value = "/login")
    public void doLogin(@RequestParam String serverUrl, @RequestParam String loginName, @RequestParam String password,
            @RequestParam(required = false) String smsCode) {
        User user = commonService.login(serverUrl, loginName, password, smsCode);
        currentSession().setAttribute(Constants.LOGIN_USER, user);
    }

    @GetMapping(value = "/admin/workspace")
    public String workspace() {
        return "admin/workspace";
    }

    /**
     * 任务列表
     */
    @GetMapping(value = "/admin/taskList")
    public String taskList(Model model) {
        model.addAttribute("menuIndex", 2);
        return "admin/taskList";
    }

    /**
     * 导出考生试题作答和得分明细
     */
    @GetMapping(value = "/admin/exportStudentAnswerAndScoreDetail")
    public String exportStudentAnswerAndScoreDetail() {
        return "admin/exportStudentAnswerAndScoreDetail";
    }

    /**
     * 更新正确答案并重新算分
     */
    @GetMapping(value = "/admin/updateCorrectAnswerAndReFixScore")
    public String updateCorrectAnswerAndReFixScore() {
        return "admin/updateCorrectAnswerAndReFixScore";
    }

    /**
     * 配置考试调卷规则
     */
    @GetMapping(value = "/admin/configExamPaper")
    public String configExamPaper() {
        return "admin/configExamPaper";
    }

    /**
     * 创建随机抽卷模板
     */
    @GetMapping(value = "/admin/createRandomPaperTemplate")
    public String createRandomPaperTemplate() {
        return "admin/createRandomPaperTemplate";
    }

}
