package com.philisense.www.controller.web;

import com.philisense.www.annotation.log.OperateLog;
import com.philisense.www.api.processor.VcodeProcessorFactory;
import com.philisense.www.common.config.CommonConst;
import com.philisense.www.controller.BaseUtilController;
import com.philisense.www.enums.VcodeType;
import com.philisense.www.exception.UtilsException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(CommonConst.WEB_REST_SERVICE_PREFICX + "vcode")
@Api(tags = "VcodeController|一个用来操作-{验证码}-业务模块的控制器")
public class VcodeController extends BaseUtilController {
    private static final Logger log = LoggerFactory.getLogger(VcodeController.class);

    @Autowired
    private VcodeProcessorFactory vcodeProcessorFactory;

    @PostMapping("/gen/{vcodeGenType}")
    @ApiOperation(value = "根据-{验证码}-生成验证码记录", notes = "成功返回:{\"success\":true,\"message\",\"数据删除成功\"},失败返回:{\"success\":false,\"message\",\"数据删除失败\"}")
    @ApiImplicitParam(paramType = "query", name = "vcodeGenType", value = "生成类型", required = true,  dataTypeClass = String.class)
    @OperateLog(name = "根据-{验证码}-生成验证码记录", value = "query", type = "PC")
    public void gen(@PathVariable String vcodeGenType, HttpServletRequest request, HttpServletResponse response) throws Exception{
        log.info("request url: {}, method:{}", request.getRequestURL(), request.getMethod());

        VcodeType codeType = VcodeType.get(vcodeGenType);

        if (codeType == null) {
            throw new UtilsException("系统不支持该校验码类型：" + vcodeGenType);
        } else {
            vcodeProcessorFactory.get(codeType).create(new ServletWebRequest(request, response));
        }
    }

    @PostMapping("/check/{vcodeGenType}")
    @ApiOperation(value = "根据-{验证码}-校验验证码记录", notes = "成功返回:{\"success\":true,\"message\",\"数据删除成功\"},失败返回:{\"success\":false,\"message\",\"数据删除失败\"}")
    @ApiImplicitParam(paramType = "query", name = "vcodeGenType", value = "生成类型", required = true,  dataTypeClass = String.class)
    @OperateLog(name = "根据-{验证码}-校验验证码记录", value = "query", type = "PC")
    public Map<String, Object> check(@PathVariable String vcodeGenType, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            log.info("request url: {}, method:{}", request.getRequestURL(), request.getMethod());

            VcodeType codeType = VcodeType.get(vcodeGenType);

            if (codeType == null) {
                throw new UtilsException("系统不支持该校验码类型：" + vcodeGenType);
            } else {
                vcodeProcessorFactory.get(codeType).doValidate(new ServletWebRequest(request, response));
            }

            result.put(DATASUCCESS, SUCCESSFLAG);
            result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
        } catch (Exception e) {
            result.put(DATASUCCESS, ERRORFLAG);
            result.put(DATAMESSAGE, QUERY_FAIL_MSG + e.getMessage());
        }
        return result;
    }
}