package com.xhpc.pp.logic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
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 com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.common.data.redis.StaticBeanUtil.seqHex;

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

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

    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;


    @Override
    public ServiceResult service(ServiceParameter sp) throws Exception {
        Map<String, Object> req = sp.getParameters();
        String pileNo = (String) req.get("pileNo");
        Map<String, Object> cachePile = REDIS.getCacheMap("pile:".concat(pileNo));
        if(!"已注册".equals(cachePile.get("status").toString())){
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode json = mapper.createObjectNode();
            json.put("error", "桩未注册");
            return new ServiceResult(null, ServiceResult.FAIL, json);
        }

        String resultStr = translate(pileNo, 2);

        String remark = "充电桩主动下发远程重启";
        XhpcDeviceMessage deviceMessage = new XhpcDeviceMessage();
        deviceMessage.setType(StationDeviceEnum.PILE.getCode());
        deviceMessage.setSerialNumber(sp.getPileNo());
        deviceMessage.setRemark(remark);
        deviceMessage.setStatus(0);
        deviceMessage.setContent(resultStr);
        deviceMessageMapper.insertByDomain(deviceMessage);
        return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK);
    }

    public static String translate(String pileNo, int runFlag) {

        String skey = "pile:".concat(pileNo).concat(".seqhex");

        String resultStr = "680C".concat(seqHex(skey)).concat("0092")
                .concat(pileNo).concat(String.format("%02x", runFlag));

        resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
        return resultStr;
    }

}
