package com.zsElectric.boot.charging.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zsElectric.boot.charging.service.ChargingReceptionService;
import com.zsElectric.boot.charging.vo.*;
import com.zsElectric.boot.common.constant.ConnectivityConstants;
import com.zsElectric.boot.common.util.electric.ChargingUtil;
import com.zsElectric.boot.common.util.electric.RequestParmsEntity;
import com.zsElectric.boot.common.util.electric.ResponseParmsEntity;
import com.zsElectric.boot.core.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.zsElectric.boot.common.constant.ConnectivityConstants.FAIL_REASON_NONE;
import static com.zsElectric.boot.common.constant.ConnectivityConstants.STATUS_OK;
import static com.zsElectric.boot.common.util.HmacMD5Util.genSign;
import static com.zsElectric.boot.common.util.HmacMD5Util.verify;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChargingReceptionServiceImpl implements ChargingReceptionService {

    private final ChargingUtil chargingUtil;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public ResponseParmsEntity chargeResponse(RequestParmsEntity requestDTO) {
        log.info("接收推送启动充电结果请求参数：{}", requestDTO);
        ChargingStatusResponseVO chargingStatusResponseVO;
        // 响应参数 答复处理
        ChargeResponseVO chargeResponseVO = new ChargeResponseVO();
        String encryptData;
        String genSign;
        try {
            String data = requestDTO.getOperatorID() + requestDTO.getData() + requestDTO.getTimeStamp() + requestDTO.getSeq();
            if(verify(data, ConnectivityConstants.SIG_SECRET, requestDTO.getSig())){
                String decryptData = chargingUtil.decryptData(requestDTO.getData());
                log.info("解密后的数据：{}", decryptData);
                JsonNode jsonNode = objectMapper.readTree(decryptData);
//                equipChargeStatusResponseVO = objectMapper.readValue(decryptData, EquipChargeStatusResponseVO.class);
                //todo 业务代码待处理



                chargeResponseVO.setStartChargeSeq(jsonNode.get("StartChargeSeq").asText());
//                chargeResponseVO.setStartChargeSeq(equipChargeStatusResponseVO.getStartChargeSeq());
                chargeResponseVO.setSuccStat(STATUS_OK);
                chargeResponseVO.setFailReason(FAIL_REASON_NONE);
                encryptData = chargingUtil.encryptData(objectMapper.writeValueAsString(chargeResponseVO));
                genSign = genSign(STATUS_OK, "请求成功", encryptData, ConnectivityConstants.SIG_SECRET);
            }else {
                log.error("数据验签失败");
                throw new BusinessException("数据验签失败");
            }
        }catch (Exception e){
            log.error("数据解密失败：{}", e.getMessage());
            throw new BusinessException("数据解密失败：" + e.getMessage(), e);
        }
        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
        responseParmsEntity.setRet(STATUS_OK);
        responseParmsEntity.setMsg("请求成功");
        responseParmsEntity.setData(encryptData);
        responseParmsEntity.setSig(genSign);
        return responseParmsEntity;

    }

    @Override
    public ResponseParmsEntity chargeStatusResponse(RequestParmsEntity requestDTO) {
        log.info("接收推送充电状态请求参数：{}", requestDTO);
        EquipChargeStatusResponseVO equipChargeStatusResponseVO;
        ChargeResponseVO chargeResponseVO = new ChargeResponseVO();
        String encryptData;
        String genSign;
        try {
            String data = requestDTO.getOperatorID() + requestDTO.getData() + requestDTO.getTimeStamp() + requestDTO.getSeq();
            if(verify(data, ConnectivityConstants.SIG_SECRET, requestDTO.getSig())){
                String decryptData = chargingUtil.decryptData(requestDTO.getData());
                log.info("解密后的数据：{}", decryptData);
                JsonNode jsonNode = objectMapper.readTree(decryptData);
//                equipChargeStatusResponseVO = objectMapper.readValue(decryptData, EquipChargeStatusResponseVO.class);
                //todo 业务代码待处理



                chargeResponseVO.setStartChargeSeq(jsonNode.get("StartChargeSeq").asText());
//                chargeResponseVO.setStartChargeSeq(equipChargeStatusResponseVO.getStartChargeSeq());
                chargeResponseVO.setSuccStat(STATUS_OK);
                chargeResponseVO.setFailReason(FAIL_REASON_NONE);
                encryptData = chargingUtil.encryptData(objectMapper.writeValueAsString(chargeResponseVO));
                genSign = genSign(STATUS_OK, "请求成功", encryptData, ConnectivityConstants.SIG_SECRET);
            }else {
                log.error("数据验签失败");
                throw new BusinessException("数据验签失败");
            }
        } catch (Exception e) {
            log.error("数据解密失败：{}", e.getMessage());
            throw new BusinessException("数据解密失败：" + e.getMessage(), e);
        }

        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
        responseParmsEntity.setRet(STATUS_OK);
        responseParmsEntity.setMsg("请求成功");
        responseParmsEntity.setData(encryptData);
        responseParmsEntity.setSig(genSign);
        return responseParmsEntity;

    }

    @Override
    public ResponseParmsEntity stopChargeResponse(RequestParmsEntity requestDTO) {
        log.info("接收推送停止充电结果请求参数：{}", requestDTO);
        StopChargingResponseVO stopChargingResponseVO;
        ChargeResponseVO chargeResponseVO = new ChargeResponseVO();
        String encryptData;
        String genSign;
        try {
            String data = requestDTO.getOperatorID() + requestDTO.getData() + requestDTO.getTimeStamp() + requestDTO.getSeq();
            if(verify(data, ConnectivityConstants.SIG_SECRET, requestDTO.getSig())){
                String decryptData = chargingUtil.decryptData(requestDTO.getData());
                log.info("解密后的数据：{}", decryptData);
                JsonNode jsonNode = objectMapper.readTree(decryptData);
//                equipChargeStatusResponseVO = objectMapper.readValue(decryptData, EquipChargeStatusResponseVO.class);
                //todo 业务代码待处理



                chargeResponseVO.setStartChargeSeq(jsonNode.get("StartChargeSeq").asText());
//                chargeResponseVO.setStartChargeSeq(equipChargeStatusResponseVO.getStartChargeSeq());
                chargeResponseVO.setSuccStat(STATUS_OK);
                chargeResponseVO.setFailReason(FAIL_REASON_NONE);
                encryptData = chargingUtil.encryptData(objectMapper.writeValueAsString(chargeResponseVO));
                genSign = genSign(STATUS_OK, "请求成功", encryptData, ConnectivityConstants.SIG_SECRET);
            }else {
                log.error("数据验签失败");
                throw new BusinessException("数据验签失败");
            }
        } catch (Exception e) {
            log.error("数据解密失败：{}", e.getMessage());
            throw new BusinessException("数据解密失败：" + e.getMessage(), e);
        }

        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
        responseParmsEntity.setRet(STATUS_OK);
        responseParmsEntity.setMsg("请求成功");
        responseParmsEntity.setData(encryptData);
        responseParmsEntity.setSig(genSign);
        return responseParmsEntity;

    }

    @Override
    public ResponseParmsEntity chargeOrderResponse(RequestParmsEntity requestDTO) {
        log.info("接收推送充电订单信息请求参数：{}", requestDTO);
        ChargingOrderVO chargingOrderVO;
        ChargeResponseVO chargeResponseVO = new ChargeResponseVO();
        String encryptData;
        String genSign;
        try {
            String data = requestDTO.getOperatorID() + requestDTO.getData() + requestDTO.getTimeStamp() + requestDTO.getSeq();
            if(verify(data, ConnectivityConstants.SIG_SECRET, requestDTO.getSig())){
                String decryptData = chargingUtil.decryptData(requestDTO.getData());
                log.info("解密后的数据：{}", decryptData);
                JsonNode jsonNode = objectMapper.readTree(decryptData);
//                equipChargeStatusResponseVO = objectMapper.readValue(decryptData, EquipChargeStatusResponseVO.class);
                //todo 业务代码待处理



                chargeResponseVO.setStartChargeSeq(jsonNode.get("StartChargeSeq").asText());
//                chargeResponseVO.setStartChargeSeq(equipChargeStatusResponseVO.getStartChargeSeq());
                chargeResponseVO.setSuccStat(STATUS_OK);
                chargeResponseVO.setFailReason(FAIL_REASON_NONE);
                encryptData = chargingUtil.encryptData(objectMapper.writeValueAsString(chargeResponseVO));
                genSign = genSign(STATUS_OK, "请求成功", encryptData, ConnectivityConstants.SIG_SECRET);
            }else {
                log.error("数据验签失败");
                throw new BusinessException("数据验签失败");
            }
        } catch (Exception e) {
            log.error("数据解密失败：{}", e.getMessage());
            throw new BusinessException("数据解密失败：" + e.getMessage(), e);
        }
        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
        responseParmsEntity.setRet(STATUS_OK);
        responseParmsEntity.setMsg("请求成功");
        responseParmsEntity.setData(encryptData);
        responseParmsEntity.setSig(genSign);
        return responseParmsEntity;
    }

    @Override
    public ResponseParmsEntity stationStatus(RequestParmsEntity requestDTO) {
        log.info("接收设备状态变化推送请求参数：{}", requestDTO);
        String encryptData = "";
        String genSign = "";
        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();

        try {
            String data = requestDTO.getOperatorID() + requestDTO.getData() + requestDTO.getTimeStamp() + requestDTO.getSeq();
            if (verify(data, ConnectivityConstants.SIG_SECRET, requestDTO.getSig())) {
                String decryptData = chargingUtil.decryptData(requestDTO.getData());
                log.info("解密后的数据：{}", decryptData);

                // todo 业务代码待处理

                Map<String, Integer> statusMap = new HashMap<>();
                statusMap.put("Status", 0);
                encryptData = chargingUtil.encryptData(objectMapper.writeValueAsString(statusMap));
                genSign = genSign(STATUS_OK, "", encryptData, ConnectivityConstants.SIG_SECRET);

                responseParmsEntity.setRet(STATUS_OK);
                responseParmsEntity.setMsg("");
                responseParmsEntity.setData(encryptData);
                responseParmsEntity .setSig(genSign);
            } else {
                log.error("数据验签失败");
                throw new BusinessException("数据验签失败");
            }
        } catch (Exception e) {
            log.error("数据解密失败：{}", e.getMessage());
            throw new BusinessException("数据解密失败：" + e.getMessage(), e);
        }

        return responseParmsEntity;

    }

}
