package com.api.controller.v1;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.openapi.v1.model.TokenGrantType;
import com.hfkj.openapi.v1.service.AuthTokenService;
import com.hfkj.service.BsMerKeyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @className: CommonController
 * @author: HuRui
 * @date: 2023/5/23
 **/
@Controller
@Api(value = "公共")
@RequestMapping(value = "/v1/auth")
public class AuthController {

    private static Logger log = LoggerFactory.getLogger(AuthController.class);

    @Resource
    private BsMerKeyService merKeyService;
    @Resource
    private AuthTokenService authTokenService;

    @RequestMapping(value="/token",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取token")
    public ResponseData token(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("grantType"))
                    || StringUtils.isBlank(body.getString("merchantNo"))
                    || StringUtils.isBlank(body.getString("merchantKey"))) {
                log.error("获取token error!", "参数错误");
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_REQ_PARAMS_ERR, "");
            }

            // 查询授权类型
            TokenGrantType tokenGrantType = TokenGrantType.getDataByCode(body.getString("grantType"));
            if (tokenGrantType == null) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_TOKEN_GRANT_TYPE_ERR, "");
            }
            String merNo = body.getString("merchantNo");
            String merKey = body.getString("merchantKey");

            // 查询商户秘钥
            String key = merKeyService.getKeyByMerNo(merNo);
            if (StringUtils.isBlank(key)) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "未配置商户秘钥");
            }
            if (!merKey.equals(key)) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_KEY_ERR, "");
            }
            return ResponseMsgUtil.success(authTokenService.createToken(merNo));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/refresh",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "刷新token")
    public ResponseData refresh(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("grantType"))
                    || StringUtils.isBlank(body.getString("merchantKey"))
                    || StringUtils.isBlank(body.getString("merchantKey"))
            ) {
                log.error("获取token error!", "参数错误");
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            String merNo = body.getString("merchantNo");
            String merKey = body.getString("merchantKey");
            // 查询商户秘钥
            String key = merKeyService.getKeyByMerNo(merNo);
            if (StringUtils.isBlank(key)) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "未配置商户秘钥");
            }
            if (!merKey.equals(key)) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_KEY_ERR, "");
            }
            return ResponseMsgUtil.success(authTokenService.refresh(merNo));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getHttpStats",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "getHttpStats")
    public ResponseData getHttpStats() {
        try {

            return ResponseMsgUtil.success(HttpsUtils.getHttpStats());

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
