package com.xhpc.pp.logic;

import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.enums.StationDeviceEnum;
import com.xhpc.pp.domain.XhpcDeviceMessage;
import com.xhpc.pp.mapper.XhpcDeviceMessageMapper;
import com.xhpc.pp.tx.ServiceParameter;
import com.xhpc.pp.tx.ServiceResult;
import com.xhpc.pp.tx.logic.ServiceLogic;
import com.xhpc.pp.utils.HexUtils;
import com.xhpc.pp.utils.security.CRCCalculator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

import static cn.hutool.core.util.NumberUtil.isInteger;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.common.data.redis.StaticBeanUtil.seqHex;
import static com.xhpc.pp.utils.HexUtils.toHexInt;

@Lazy
@Component("RateModelValidateLogic")
public class RateModelValidateLogic implements ServiceLogic {

    private static final Logger log = LoggerFactory.getLogger(RateModelValidateLogic.class);

    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;

    @Override
    public ServiceResult service(ServiceParameter sp) throws Exception {

        Map<String, Object> req = sp.getParameters();
        String pileNo = (String) req.get("pileNo");
        String rateModelIdStr = req.get("rateModelId").toString();

        Map<String, Object> cachePile = REDIS.getCacheMap("pile:".concat(pileNo));
        Integer gunNum = (Integer) cachePile.get("gunNum");
        boolean charging = false;
        for (int gunN = 1; gunN <= gunNum; gunN++) {
            String gunId = String.format("%02d", gunN);
            String gunkey = "gun:".concat(pileNo.concat(gunId));
            Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
            Object status = cacheGun.get("status");
            if (isInteger(status.toString())) {
                charging = true;
            }
        }
        String resultCode = ServiceResult.OK;
        String hexCode = ServiceResult.HEX_00;
        Long rateModelId;
        String version = (String) cachePile.get("version");
//        if ("0C".equals(version)) rateModelId = Long.valueOf(rateModelIdStr);
//        else rateModelId = Long.valueOf(Integer.parseInt(rateModelIdStr, 16));

        if("0F".equals(version)){
            rateModelId = Long.valueOf(rateModelIdStr);
        } else if ("0C".equals(version)){
            rateModelId = Long.valueOf(rateModelIdStr);
        } else{
            rateModelId = Long.valueOf(Integer.parseInt(rateModelIdStr, 16));
        }

        Long csRateModelId = rateModelId;
        if (!charging) {
            ChargingStationDto cacheStation = REDIS.getCacheObject("station:".concat(cachePile.get("stationId").toString()));
            csRateModelId = cacheStation.getRateModelId();
            if (csRateModelId.intValue() != rateModelId) {
                hexCode = ServiceResult.HEX_01;
                resultCode = ServiceResult.FAIL;
            }
        }
        String skey = "pile:".concat(pileNo).concat(".seqhex");
        String seq = seqHex(skey);
        String resultStr;

//        if (!"0C".equals(version)){
//            resultStr = "680E".concat(seq).concat("0006").concat(pileNo).concat(String.format("%04X", csRateModelId)).concat(hexCode);
//            resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
//        }else{
//            resultStr = "6810".concat(seq).concat("0006").concat(pileNo).concat(HexUtils.toHexInt(csRateModelId)).concat(hexCode);
//            resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
//        }

        if ("0F".equals(version)){
            //System.out.println("==========0X05======");
            String seq1 =(String) req.get("hex");
            resultStr = "680E".concat(seq1.substring(4,8)).concat("0006").concat(pileNo).concat(String.format("%04X", csRateModelId)).concat(hexCode);
            resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
        }else if("0C".equals(version)){
            resultStr = "6810".concat(seq).concat("0006").concat(pileNo).concat(HexUtils.toHexInt(csRateModelId)).concat(hexCode);
            resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
        }else{
            String seq1 =(String) req.get("hex");
            resultStr = "680E".concat(seq1.substring(4,8)).concat("0006").concat(pileNo).concat(String.format("%04X", csRateModelId)).concat(hexCode);
            resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
        }


        XhpcDeviceMessage deviceMessage = new XhpcDeviceMessage();
        deviceMessage.setType(StationDeviceEnum.PILE.getCode());
        deviceMessage.setSerialNumber(pileNo);
        deviceMessage.setRemark("计费模型验证请求");
        deviceMessage.setStatus(0);
        deviceMessage.setContent((String) req.get("hex"));
        deviceMessageMapper.insertByDomain(deviceMessage);

        return new ServiceResult(HexUtils.toBytes(resultStr), resultCode);
    }


}
