package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.exception.CuscUserException;
import com.cusc.nirvana.user.rnr.fp.common.RnrResultEnum;
import com.cusc.nirvana.user.rnr.fp.common.util.DateUtil;
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.fp.service.IRnrUnbindService;
import com.cusc.nirvana.user.rnr.mg.client.MgRnrCardInfoClient;
import com.cusc.nirvana.user.rnr.mg.client.MgRnrUnboundClient;
import com.cusc.nirvana.user.rnr.mg.constants.RnrFileType;
import com.cusc.nirvana.user.rnr.mg.constants.RnrOrderAuditTypeEnum;
import com.cusc.nirvana.user.rnr.mg.constants.RnrOrderStatusEnum;
import com.cusc.nirvana.user.rnr.mg.constants.RnrStatus;
import com.cusc.nirvana.user.rnr.mg.dto.MgRnrCardInfoDTO;
import com.cusc.nirvana.user.rnr.mg.dto.MgRnrFileDTO;
import com.cusc.nirvana.user.rnr.mg.dto.RnrRelationDTO;
import com.cusc.nirvana.user.util.CuscStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 实名解绑实现类
 *
 * @author yubo
 * @since 2022-04-25 09:45
 */
@Service
public class RnrUnbindServiceImpl implements IRnrUnbindService {

    @Autowired
    IRnrService rnrService;

    @Autowired
    MgRnrUnboundClient mgRnrUnboundClient;

    @Autowired
    MgRnrCardInfoClient cardInfoClient;

    @Value("${rnr.mock:false}")
    private boolean mock;

    /**
     * 二手车现车主解绑-自然人
     */
    @Override
    public Integer secondHandPersonalUnbind(String serialNumber, String requestId, RnrRelationDTO dto) {
//        boolean result = rnrService.checkPersonData(serialNumber, requestId, dto);

        //判断卡是否已经实名
        iccidListCheck(dto);


		RnrRequestDTO rnrRequest = rnrService.rnrPersonRelationDto2RnrRequestDTO(dto);
		rnrRequest.setBizUuid(dto.getInfo().getUuid());
		rnrRequest.setSerialNumber(dto.getOrder().getUuid());
		rnrRequest.setRequestId(requestId);

		RnrResponseDTO rnrResponseDTO = rnrService.doPersonRnr(rnrRequest,null);

        //添加活体图片1
        MgRnrFileDTO livenessPic1 = new MgRnrFileDTO();
        livenessPic1.setUuid(CuscStringUtils.generateUuid());
        livenessPic1.setRnrId(dto.getInfo().getUuid());
        livenessPic1.setLiaisonId(rnrRequest.getMatchVideoPerosinInfo().getPersonId());
        livenessPic1.setFileSystemId(rnrResponseDTO.getLivenessPic1());
        livenessPic1.setFileType(RnrFileType.LIVENESS_SCREEN_FIRST.getCode());
        livenessPic1.setCreator(dto.getInfo().getCreator());
        dto.getRnrFileList().add(livenessPic1);

        //添加活体图片1
        MgRnrFileDTO livenessPic2 = new MgRnrFileDTO();
        livenessPic2.setUuid(CuscStringUtils.generateUuid());
        livenessPic2.setRnrId(dto.getInfo().getUuid());
        livenessPic2.setLiaisonId(rnrRequest.getMatchVideoPerosinInfo().getPersonId());
        livenessPic2.setFileSystemId(rnrResponseDTO.getLivenessPic2());
        livenessPic2.setFileType(RnrFileType.LIVENESS_SCREEN_TWO.getCode());
        livenessPic2.setCreator(dto.getInfo().getCreator());
        dto.getRnrFileList().add(livenessPic2);

        //二手车解绑，直接走人工审核
        dto.getOrder().setAuditType(RnrOrderAuditTypeEnum.MANUAL.getCode());
        dto.getOrder().setOrderStatus(RnrOrderStatusEnum.ASSIGNMENT.getCode());
        dto.getInfo().setRnrStatus(RnrStatus.INIT.getCode());
        dto.getCardList().forEach(card -> card.setRnrStatus(RnrStatus.INIT.getCode()));




        Response response = mgRnrUnboundClient.secondHandUnBound(dto);
        if (!response.isSuccess()) {
            throw new CuscUserException(response.getCode(), response.getMsg());
        }

        return RnrResultEnum.REVIEW.getCode();
    }

    private void iccidListCheck(RnrRelationDTO dto) {
        List<MgRnrCardInfoDTO> cardList = dto.getCardList();

        //检查车和卡是否已经实名了
        for (MgRnrCardInfoDTO mgRnrCardInfoDTO : cardList) {
            Response<MgRnrCardInfoDTO> oneByIccid = cardInfoClient.getOneByIccid(mgRnrCardInfoDTO);
            if (!oneByIccid.isSuccess() || oneByIccid.getData() == null) {
                throw new CuscUserException("", "车【" + mgRnrCardInfoDTO.getIotId() + "】和卡【" + mgRnrCardInfoDTO.getIccid() + "】未绑定");
            }
        }
    }
}
