package com.cusc.nirvana.user.rnr.fp.controller;

import com.alibaba.fastjson.JSONObject;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.exception.CuscUserException;
import com.cusc.nirvana.user.rnr.fp.dto.H5ValidationOCRReqDTO;
import com.cusc.nirvana.user.rnr.fp.dto.LiveLoginReqDTO;
import com.cusc.nirvana.user.rnr.fp.dto.LiveLoginRespDTO;
import com.cusc.nirvana.user.rnr.fp.dto.LivenessCodeRequestDTO;
import com.cusc.nirvana.user.rnr.fp.dto.LivenessCodeResponseDTO;
import com.cusc.nirvana.user.rnr.fp.dto.LivenessResultDTO;
import com.cusc.nirvana.user.rnr.fp.dto.PersonH5CallBackRequestDTO;
import com.cusc.nirvana.user.rnr.fp.dto.RnrRequestDTO;
import com.cusc.nirvana.user.rnr.fp.dto.RnrResponseDTO;
import com.cusc.nirvana.user.rnr.fp.service.IRnrService;
import com.cusc.nirvana.user.rnr.mg.constants.SubjectTypeEnum;
import com.cusc.nirvana.user.rnr.mg.dto.RnrOrderDTO;
import com.cusc.nirvana.user.rnr.mg.dto.RnrRelationDTO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author stayAnd
 * @date 2022/4/18
 */
@Slf4j
@RestController
@RequestMapping("/rnr")
public class RnrController {

    @Resource
    private IRnrService rnrService;


    @PostMapping("/getLivenessCode")
    public Response<LivenessCodeResponseDTO> getLivenessCode(@Validated @RequestBody LivenessCodeRequestDTO dto) {
        return Response.createSuccess(rnrService.getLivenessCode(dto));
    }


    @PostMapping("/personRnr/{serialNumber}/{requestId}")
    public Response<Integer> personRnr(@PathVariable("serialNumber") String serialNumber,
                                       @PathVariable("requestId") String requestId,
                                       @RequestBody RnrRelationDTO dto) {
        return Response.createSuccess(rnrService.personRnr(serialNumber, requestId, dto));
    }

    @PostMapping("/personSecondHandRnr/{serialNumber}/{requestId}")
    public Response<Integer> personSecondHandRnr(@PathVariable("serialNumber") String serialNumber,
                                                 @PathVariable("requestId") String requestId,
                                                 @RequestBody RnrRelationDTO dto) {
        return Response.createSuccess(rnrService.personSecondHandRnr(serialNumber, requestId, dto));
    }

    /**
     * H5页面提交接口
     *
     * @param serialNumber
     * @param dto
     * @return
     */
    @PostMapping("/personRnrH5/{serialNumber}")
    public Response personRnrH5(@PathVariable("serialNumber") String serialNumber,
                                @RequestBody RnrRelationDTO dto) {
        rnrService.personRnrH5(serialNumber, dto);
        return Response.createSuccess();
    }

//    @PostMapping("/personRnrH5CallBack")
//    public Response<Integer> personH5CallBack(@RequestBody PersonH5CallBackRequestDTO dto) {
//        return Response.createSuccess(rnrService.personH5CallBack(dto));
//    }

    @PostMapping("/personRnrH5CallBack")
    public Response<Integer> personH5CallBack(@RequestBody PersonH5CallBackRequestDTO dto) {
        try {
            return Response.createSuccess(rnrService.personH5CallBack(dto));
        } catch (CuscUserException e) {
            Response ret = Response.createError(e.getMessage());
            ret.setData(0);
            return ret;
        }
    }

    @PostMapping("/getRnrLivenessType/{tenantNo}")
    @ApiOperation(value = "获取活体实名类型配置", notes = "获取活体实名类型配置")
    public Response<String> getRnrLivenessType(@PathVariable("tenantNo") String tenantNo) {
        return Response.createSuccess("", rnrService.getRnrLivenessType(tenantNo));
    }


    /**
     * 审核页面重试接口
     *
     * @param dto
     * @return
     */
    @PostMapping("/personalInfoOnlyRnr")
    public Response<RnrResponseDTO> personalInfoOnlyRnr(@RequestBody RnrRelationDTO dto) {
        RnrRequestDTO rnrRequest = rnrService.rnrPersonRelationDto2RnrRequestDTO(dto);
        rnrRequest.setBizUuid(dto.getInfo().getUuid());
        rnrRequest.setSerialNumber(dto.getSerialNumber());
        //只做自然人活体以及身份证等验证信息  无业务逻辑验证
        return Response.createSuccess(rnrService.doPersonRnr(rnrRequest, null));
    }

    @PostMapping("/doIdAndFaceCompare")
    public Response<RnrResponseDTO> doIdAndFaceCompare(@RequestBody RnrRelationDTO dto) {
        RnrRequestDTO rnrRequest = rnrService.rnrPersonRelationDto2RnrRequestDTO(dto);
        rnrRequest.setBizUuid(dto.getInfo().getUuid());
        rnrRequest.setSerialNumber(dto.getSerialNumber());
        return Response.createSuccess(rnrService.doIdAndFaceCompare(rnrRequest, null,
                dto.getIsTrust() == 1 ? SubjectTypeEnum.CONSIGNOR.getCode() : SubjectTypeEnum.ONESELF.getCode()));
    }

    /**
     * 获取H5活体检测URL
     *
     * @param dto
     * @return
     */
    @PostMapping("/h5LiveLoginUrl")
    public Response<LiveLoginRespDTO> h5LiveLoginUrl(@RequestBody LiveLoginReqDTO dto) {
        log.info("h5LiveLoginUrl 请求参数:" + JSONObject.toJSONString(dto));
        Response<LiveLoginRespDTO> response = rnrService.h5LiveLoginUrl(dto);
        log.info("h5LiveLoginUrl 返回参数:" + JSONObject.toJSONString(response));
        return response;
    }

    /**
     * 获取H5活体检测URL
     *
     * @param dto
     * @return
     */
    @PostMapping("/h5LivenessResult")
    public Response<LivenessResultDTO> h5LivenessResult(@RequestBody LiveLoginReqDTO dto) {
        log.info("h5LivenessResult 请求参数:" + JSONObject.toJSONString(dto));
        Response<LivenessResultDTO> response = rnrService.h5LivenessResult(dto);
        log.info("h5LivenessResult 返回参数:" + JSONObject.toJSONString(response));
        return response;
    }

    /**
     * h5验证身份证信息
     *
     * @param dto
     * @return
     */
    @PostMapping("/h5ValidationOCR")
    public Response<RnrResponseDTO> h5ValidationOCR(@RequestBody H5ValidationOCRReqDTO dto) {
        log.info("h5ValidationOCR 请求参数:" + JSONObject.toJSONString(dto));
        Response<RnrResponseDTO> response = rnrService.h5ValidationOCR(dto);
        log.info("h5ValidationOCR 返回参数:" + JSONObject.toJSONString(response));
        return response;
    }

    /**
     * 修改实名信息为无效
     *
     * @return
     */
    @PostMapping("/updateRnrInValid")
    public Response updateRnrInValid(@RequestBody RnrOrderDTO bean) {
        return rnrService.updateRnrInValid(bean);
    }

}
