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

import com.alibaba.fastjson.JSON;
import com.cusc.nirvana.common.result.PageResult;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.rnr.fp.dto.SimVehicleBindingDTO;
import com.cusc.nirvana.user.rnr.fp.dto.SimVehicleBindingResult;
import com.cusc.nirvana.user.rnr.fp.dto.SimVehicleDTO;
import com.cusc.nirvana.user.rnr.fp.dto.*;
import com.cusc.nirvana.user.rnr.fp.service.ISimVehicleRelService;
import com.cusc.nirvana.user.rnr.mg.client.OrgSimRelClient;
import com.cusc.nirvana.user.rnr.mg.constants.BindingStatus;
import com.cusc.nirvana.user.rnr.mg.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.support.BindStatus;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SimVehicleRelServiceImpl implements ISimVehicleRelService {

    private final static Logger LOGGER = LoggerFactory.getLogger(SimVehicleRelServiceImpl.class);

    @Autowired
    private OrgSimRelClient orgSimRelClient;


    @Override
    public Response<SimVehicleBindingResult> importSimVehicle(SimVehicleBindingDTO bindingDTO) {
        String tagUuid = bindingDTO.getTagUuid();
        // SIM卡列表信息
        List<OrgSimRelDTO> list = new ArrayList<>();
        bindingDTO.getBindingList().forEach(simVehicleDTO -> {
            OrgSimRelDTO orgSimRelDTO = simVehicleDTOToOrgSimRelDto(simVehicleDTO);
            orgSimRelDTO.setOrgUuid(bindingDTO.getOrganizationId());
            orgSimRelDTO.setBindStatus(0);
            orgSimRelDTO.setTagUuid(tagUuid);
            orgSimRelDTO.setCompanyCode(bindingDTO.getCompanyCode());
            list.add(orgSimRelDTO);
        });
        OrgSimRelBatchDTO batchDTO = new OrgSimRelBatchDTO();
        batchDTO.setOrgSimRels(list);
        Response response = orgSimRelClient.addBatch(batchDTO);
        if (response.isSuccess()) {
            SimVehicleBindingResult simVehicleBindingResult = new SimVehicleBindingResult();
            simVehicleBindingResult.setFailCount(0);
            simVehicleBindingResult.setSuccessCount(bindingDTO.getBindingList().size());
            simVehicleBindingResult.setFailData(new ArrayList<>());
            return Response.createSuccess(simVehicleBindingResult);
        } else {
            SimVehicleBindingResult simVehicleBindingResult = new SimVehicleBindingResult();
            int size = bindingDTO.getBindingList().size();
            simVehicleBindingResult.setFailCount(size);
            simVehicleBindingResult.setSuccessCount(size);
            simVehicleBindingResult.setFailData(bindingDTO.getBindingList());
            return Response.createError("导入车卡关系失败");
        }
    }


    @Override
    public Response<SimVehicleDTO> unbind(BindRequestDTO requestDTO) {
        return updateBindStatus(requestDTO, 1);
    }

    @Override
    public Response<SimVehicleDTO> bind(BindRequestDTO requestDTO) {
        return updateBindStatus(requestDTO, 0);
    }

    private Response<SimVehicleDTO> updateBindStatus(BindRequestDTO requestDTO, Integer bindStatus) {
        OrgSimRelBindStatusDTO bindStatusDTO = new OrgSimRelBindStatusDTO();
        bindStatusDTO.setIccid(requestDTO.getIccid());
        bindStatusDTO.setVin(requestDTO.getVin());
        bindStatusDTO.setBindStatus(bindStatus);
        Response response = orgSimRelClient.updateBindStatus(bindStatusDTO);
        if (response.isSuccess()) {
            Response<OrgSimRelDTO> orgSimRelDTOResponse = orgSimRelClient.getByIccid(requestDTO.getIccid());
            SimVehicleDTO simVehicleDTO = null;
            if (orgSimRelDTOResponse.isSuccess()) {
                simVehicleDTO = orgSimRelDtoToSimVehicleDto(orgSimRelDTOResponse.getData());
                return Response.createSuccess(simVehicleDTO);
            } else {
                return Response.createSuccess(simVehicleDTO);
            }
        }
        return Response.createError("更新车卡绑定状态失败");
    }

    @Override
    public Response<List<VinIccidDTO>> getIccidByVin(String vin) {
        OrgSimRelVinQueryDTO queryDTO = new OrgSimRelVinQueryDTO();
        queryDTO.setVin(vin);
        Response<List<OrgSimRelDTO>> response = orgSimRelClient.queryByVin(queryDTO);
        List<VinIccidDTO> vinIccidDTOS = new ArrayList<>();
        if (response.isSuccess() && !CollectionUtils.isEmpty(response.getData())) {
            response.getData().forEach(orgSimRelDTO -> {
                VinIccidDTO vinIccidDTO = new VinIccidDTO();
                BeanUtils.copyProperties(orgSimRelDTO, vinIccidDTO);
                vinIccidDTOS.add(vinIccidDTO);
            });
        }
        return Response.createSuccess(vinIccidDTOS);
    }

    @Override
    public Response<VinIccidDTO> getVinByIccid(String iccid) {
        Response<OrgSimRelDTO> response = orgSimRelClient.getByIccid(iccid);
        if (response.isSuccess()) {
            if (response.getData() != null) {
                VinIccidDTO vinIccidDTO = new VinIccidDTO();
                BeanUtils.copyProperties(response.getData(), vinIccidDTO);
                return Response.createSuccess(vinIccidDTO);
            } else {
                return Response.createSuccess();
            }
        } else {
            return Response.createError(response.getMsg());
        }
    }

    @Override
    public Response<VinIccidDTO> getBindingInfo(String iccid, String vin) {

        Response<OrgSimRelDTO> response = orgSimRelClient.getByIccid(iccid);
        if (response.isSuccess()) {
            if (response.getData() != null && response.getData().getBindStatus() == 0) {
                VinIccidDTO vinIccidDTO = new VinIccidDTO();
                BeanUtils.copyProperties(response.getData(), vinIccidDTO);
                return Response.createSuccess(vinIccidDTO);
            }
            return Response.createSuccess();
        } else {
            return Response.createError(response.getMsg());
        }
    }


    @Override
    public Response<List<IccidDetailDTO>> queryByIccids(IccidListDTO listDTO) {
        IccIdListRequestDTO requestDTO = new IccIdListRequestDTO();
        requestDTO.setIccids(listDTO.getIccidList());
        Response<List<OrgSimRelDTO>> response = orgSimRelClient.queryByList(requestDTO);
        if (response.isSuccess()) {
            List<IccidDetailDTO> iccidDetailDTOS = new ArrayList<>();
            response.getData().forEach(orgSimRelDTO -> {
                IccidDetailDTO iccidDetailDTO = new IccidDetailDTO();
                BeanUtils.copyProperties(orgSimRelDTO, iccidDetailDTO);
                iccidDetailDTOS.add(iccidDetailDTO);
            });
            return Response.createSuccess(iccidDetailDTOS);
        } else {
            return Response.createError(response.getMsg());
        }
    }



    @Override
    public Response<IccidDetailDTO> getDetailByIccid(String iccid) {
        List<String> iccids = new ArrayList<>();
        iccids.add(iccid);
        IccidListDTO listDTO = new IccidListDTO();
        listDTO.setIccidList(iccids);
        Response<List<IccidDetailDTO>> response = queryByIccids(listDTO);
        if (response.isSuccess() && !CollectionUtils.isEmpty(response.getData())) {
            return Response.createSuccess(response.getData().get(0));
        }
        return Response.createError("没有查询到该iccid详情");
    }

    @Override
    public Response<PageResult<SimVehicleResultDTO>> queryBindingPage(SimVehicleQueryDTO queryDTO) {

        OrgSimRelQueryDTO orgSimRelQueryDTO = new OrgSimRelQueryDTO();
        BeanUtils.copyProperties(queryDTO, orgSimRelQueryDTO);
        orgSimRelQueryDTO.setOrgUuid(queryDTO.getOrganizationId());

        Response<PageResult<OrgSimRelDTO>> resultResponse = orgSimRelClient.queryByPage(orgSimRelQueryDTO);
        PageResult<SimVehicleResultDTO> pageResult = new PageResult<>();
        if (resultResponse.isSuccess() && resultResponse.getData() != null) {
            pageResult.setCurrPage(resultResponse.getData().getCurrPage());
            pageResult.setPageSize(resultResponse.getData().getPageSize());
            pageResult.setTotalCount(resultResponse.getData().getTotalCount());
            pageResult.setHasMore(resultResponse.getData().isHasMore());
            List<SimVehicleResultDTO> resultDTOS = new ArrayList<>();
            resultResponse.getData().getList().forEach(orgSimRelDTO -> {
                SimVehicleResultDTO simVehicleResultDTO = new SimVehicleResultDTO();
                BeanUtils.copyProperties(orgSimRelDTO, simVehicleResultDTO);
                resultDTOS.add(simVehicleResultDTO);
            });
            pageResult.setList(resultDTOS);
            return Response.createSuccess(pageResult);
        }
        return Response.createError(resultResponse.getMsg());
    }

    private OrgSimRelDTO simVehicleDTOToOrgSimRelDto(SimVehicleDTO simVehicleDTO) {
        LOGGER.info(JSON.toJSONString(simVehicleDTO));
        OrgSimRelDTO orgSimRelDTO = new OrgSimRelDTO();
        BeanUtils.copyProperties(simVehicleDTO, orgSimRelDTO);
        LOGGER.info(JSON.toJSONString(orgSimRelDTO));
        return orgSimRelDTO;
    }

    private SimVehicleDTO orgSimRelDtoToSimVehicleDto(OrgSimRelDTO orgSimRelDTO) {
        SimVehicleDTO simVehicleDTO = new SimVehicleDTO();
        BeanUtils.copyProperties(orgSimRelDTO, simVehicleDTO);
        return simVehicleDTO;
    }
}
