package info.aspirecn.iov.sjjh.supplier10000027.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import info.aspirecn.iov.sjjh.commons.lang.ChannelTypeHandleResponseObject;
import info.aspirecn.iov.sjjh.commons.lang.Constant;
import info.aspirecn.iov.sjjh.supplier10000027.constant.SjjhConstant;
import info.aspirecn.iov.sjjh.supplier10000027.service.ChannelService;
import info.aspirecn.rdc.aspirecloud.node.except.utils.ErrorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @description:
 * @author: xusonglin
 * @create: 2019/9/3 14:12
 * @version: V1.0
 **/
@RestController
@Slf4j
public class ChannelAction {
    @Autowired
    private ChannelService service;

    @ApiOperation(value = "身份证有效期")
    @PostMapping(value = "/validateCompositeIdentityInfo.do")
    public ChannelTypeHandleResponseObject validateCompositeIdentityInfo(
            @ApiParam(value = "通道ID") @RequestHeader(name = "channelId") String channelId,
            @ApiParam(value = "超时时间,单位：毫秒", example = "10000") @RequestParam(name = "outTime") int outTime,
            @ApiParam(value = "请求参数JSON串") @RequestParam(name = "customBody") String customBody) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        //调用service
        ChannelTypeHandleResponseObject responseObject = service.validateCompositeIdentityInfo(request, customBody, outTime);

        //把接口参数、调用结果和是否收费放入访问日志中
        if (responseObject.getCode() != Constant.SUCCESS) {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_ERROR_CODE);
        } else {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_SUCCESS_CODE);
        }
        request.setAttribute(SjjhConstant.LOG_FEE_PARA, responseObject.getIsCharge());

        try {
            request.setAttribute(Constant.CHANNEL_LOG_QUERY, Base64.encodeBase64String(customBody.getBytes(SjjhConstant.PARA_ENCODE)));
        } catch (UnsupportedEncodingException e) {
            ErrorUtils.captureException(e);
        }

        request.setAttribute(Constant.CHANNEL_TYPE_KEY, Constant.CHANNEL_TYPE_SYNC);
        return responseObject;
    }

    @ApiOperation(value = "身份证简项")
    @PostMapping(value = "/validateIdentityInfo.do")
    public ChannelTypeHandleResponseObject validateIdentityInfo(
            @ApiParam(value = "通道ID") @RequestHeader(name = "channelId") String channelId,
            @ApiParam(value = "超时时间,单位：毫秒", example = "10000") @RequestParam(name = "outTime") int outTime,
            @ApiParam(value = "请求参数JSON串") @RequestParam(name = "customBody") String customBody) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        log.info("params === " + customBody);
     /*   try {
            customBody =  Base64.encodeBase64String(customBody.getBytes(SjjhConstant.PARA_ENCODE));
            log.info("action传入customBody转base64:" + customBody);
        } catch (Exception e) {
            log.error("转base64失败");
            ErrorUtils.captureException(e);
        }*/

        //调用service
        ChannelTypeHandleResponseObject responseObject = service.validateIdentityInfo(request, customBody, outTime);

        //把接口参数、调用结果和是否收费放入访问日志中
        if (responseObject.getCode() != Constant.SUCCESS) {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_ERROR_CODE);
        } else {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_SUCCESS_CODE);
        }
        request.setAttribute(SjjhConstant.LOG_FEE_PARA, responseObject.getIsCharge());

        try {
            request.setAttribute(Constant.CHANNEL_LOG_QUERY, Base64.encodeBase64String(customBody.getBytes(SjjhConstant.PARA_ENCODE)));
        } catch (UnsupportedEncodingException e) {
            ErrorUtils.captureException(e);
        }

        request.setAttribute(Constant.CHANNEL_TYPE_KEY, Constant.CHANNEL_TYPE_SYNC);
        return responseObject;
    }

    @ApiOperation(value = "身份证简项+人像")
    @PostMapping(value = "/validatePhotoIdentityInfo.do", produces = "application/json;charset=UTF-8")
    public ChannelTypeHandleResponseObject validatePhotoIdentityInfo(
            @ApiParam(value = "通道ID") @RequestHeader(name = "channelId") String channelId,
            @ApiParam(value = "超时时间,单位：毫秒", example = "10000") @RequestParam(name = "outTime") int outTime,
            @ApiParam(value = "请求参数JSON串") @RequestBody String customBody) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();

        //调用service
        ChannelTypeHandleResponseObject responseObject = service.validatePhotoIdentityInfo(request, customBody, outTime);

        //把接口参数、调用结果和是否收费放入访问日志中
        if (responseObject.getCode() != Constant.SUCCESS) {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_ERROR_CODE);
        } else {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_SUCCESS_CODE);
        }
        request.setAttribute(SjjhConstant.LOG_FEE_PARA, responseObject.getIsCharge());

        try {
            JSONObject paramObject = JSON.parseObject(customBody);
            paramObject.put("photo", "base64");
            log.info("actionBase64={}", Base64.encodeBase64String(paramObject.toJSONString().getBytes(SjjhConstant.PARA_ENCODE)));
            request.setAttribute(Constant.CHANNEL_LOG_QUERY,
                    Base64.encodeBase64String(paramObject.toJSONString().getBytes(SjjhConstant.PARA_ENCODE)));
        } catch (Exception ex) {
            ErrorUtils.captureException(ex);
        }

        request.setAttribute(Constant.CHANNEL_TYPE_KEY, Constant.CHANNEL_TYPE_SYNC);
        return responseObject;
    }

    @ApiOperation(value = "身份证简项 - 中心通用版")
    @PostMapping(value = "/validateIdentityInfoCommon.do")
    public ChannelTypeHandleResponseObject validateIdentityInfoCommon(
            @ApiParam(value = "通道ID") @RequestHeader(name = "channelId") String channelId,
            @ApiParam(value = "超时时间,单位：毫秒", example = "10000") @RequestParam(name = "outTime") int outTime,
            @ApiParam(value = "请求参数JSON串") @RequestParam(name = "customBody") String customBody) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();

        //调用service
        ChannelTypeHandleResponseObject responseObject = service.  validateIdentityInfoCommon(request, customBody, outTime, SjjhConstant.DECODE_TYPE_COMMON);

        //把接口参数、调用结果和是否收费放入访问日志中
        if (responseObject.getCode() != Constant.SUCCESS) {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_ERROR_CODE);
        } else {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_SUCCESS_CODE);
        }
        request.setAttribute(SjjhConstant.LOG_FEE_PARA, responseObject.getIsCharge());

        try {
            request.setAttribute(Constant.CHANNEL_LOG_QUERY, Base64.encodeBase64String(customBody.getBytes(SjjhConstant.PARA_ENCODE)));
        } catch (UnsupportedEncodingException e) {
            ErrorUtils.captureException(e);
        }

        request.setAttribute(Constant.CHANNEL_TYPE_KEY, Constant.CHANNEL_TYPE_SYNC);
        return responseObject;
    }

    @ApiOperation(value = "身份证简项 - 中心通用版-WZ加密版")
    @PostMapping(value = "/validateIdentityInfoCommonWZ.do")
    public ChannelTypeHandleResponseObject validateIdentityInfoCommonWZ(
            @ApiParam(value = "通道ID") @RequestHeader(name = "channelId") String channelId,
            @ApiParam(value = "超时时间,单位：毫秒", example = "10000") @RequestParam(name = "outTime") int outTime,
            @ApiParam(value = "请求参数JSON串") @RequestParam(name = "customBody") String customBody) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();

        //调用service
        ChannelTypeHandleResponseObject responseObject = service.validateIdentityInfoCommon(request, customBody, outTime, SjjhConstant.DECODE_TYPE_WEIZHONG);

        //把接口参数、调用结果和是否收费放入访问日志中
        if (responseObject.getCode() != Constant.SUCCESS) {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_ERROR_CODE);
        } else {
            request.setAttribute(SjjhConstant.LOG_ERROR_PARA, Constant.CHANNEL_LOG_SUCCESS_CODE);
        }
        request.setAttribute(SjjhConstant.LOG_FEE_PARA, responseObject.getIsCharge());

        try {
            request.setAttribute(Constant.CHANNEL_LOG_QUERY, Base64.encodeBase64String(customBody.getBytes(SjjhConstant.PARA_ENCODE)));
        } catch (UnsupportedEncodingException e) {
            ErrorUtils.captureException(e);
        }

        request.setAttribute(Constant.CHANNEL_TYPE_KEY, Constant.CHANNEL_TYPE_SYNC);
        return responseObject;
    }
}
