package com.qmth.jkserver.controller.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qmth.jkserver.core.annotation.TokenValidate;
import com.qmth.jkserver.core.exception.ApiException;
import com.qmth.jkserver.dto.FaceVerify;
import com.qmth.jkserver.enums.ExamStatus;
import com.qmth.jkserver.enums.ExamType;
import com.qmth.jkserver.enums.OpenMonitor;
import com.qmth.jkserver.enums.TokenPolicy;
import com.qmth.jkserver.model.ExamPlan;
import com.qmth.jkserver.service.ApiSecurityService;
import com.qmth.jkserver.service.JkExamPlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 安全校验接口
 *
 * @author tianjun
 */
@RestController
@RequestMapping("/api/jkdevice")
public class SecurityController {

    @Resource
    private ApiSecurityService apiSecurityService;

    @Resource
    @Lazy
    private JkExamPlanService examPlanService;

    @RequestMapping("/validateExamCode")
    @TokenValidate(policy = TokenPolicy.IGNORE)
    public Object validateExamCode(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
        String examCode = request.getParameter("examCode");
        if (StringUtils.isEmpty(examCode)) {
            throw ApiException.EXAM_CODE_INVALID;
        }
        ExamPlan examPlan = examPlanService.findByExamCode(examCode);
        if (examPlan == null) {
            throw ApiException.EXAM_CODE_INVALID;
        }
        if (!examPlan.getEnable()) {
            throw ApiException.EXAM_PLAN_DISABLED;
        }
        if (!ExamStatus.PUBLISHED.equals(examPlan.getExamStatus())) {
            throw ApiException.EXAM_PLAN_UN_PUBLISHED;
        }
        String mac = request.getParameter("mac");
        if (StringUtils.isEmpty(mac)) {
            throw ApiException.MISSING_PARAMS_MAC;
        }
        String token = apiSecurityService.generateToken(mac, examCode);
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("type", examPlan.getExamType().name());
        if (examPlan.getExamType().equals(ExamType.SOLFEGE)) {
            map.put("loginRule", examPlan.getLoginRule().name());
            map.put("paperRule", examPlan.getPaperRule().name());
            map.put("theme", examPlan.getClientTheme().name());
        }
        OpenMonitor openMonitor = examPlan.getOpenMonitor() == null ? OpenMonitor.NO : examPlan.getOpenMonitor();
        String faceVerifyOpen = examPlan.getFaceVerifyOpen();
        String faceVerifyIntervalTime = examPlan.getFaceVerifyIntervalTime();
//        FaceVerify faceVerify = examPlan.getFaceVerify() == null ? new FaceVerify() : examPlan.getFaceVerify();
        FaceVerify faceVerify = new FaceVerify(faceVerifyOpen, faceVerifyIntervalTime);
        map.put("openMonitor", openMonitor.getId());
        map.put("faceVerify", faceVerify);
        map.put("sceneScore", examPlan.getSceneScore().getId());
        return map;
    }


    @RequestMapping("/syncTime")
    public Object syncTime(HttpServletRequest request, HttpServletResponse response
    ) throws JsonProcessingException {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

}
