package com.jsowell.netty.handler;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 错误报文Handler
 *
 *  GBT-27930 充电桩与 BMS 充电错误报文
 * @author JS-ZZA
 * @date 2022/9/19 13:29
 */
@Slf4j
@Component
public class ErrorMessageHandler extends AbstractHandler{
    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.ERROR_MESSAGE_CODE.getBytes());

    @Override
    public void afterPropertiesSet() throws Exception {
        YKCOperateFactory.register(type, this);
    }

    @Override
    public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
        log.info("[===错误报文===] param:{}, channel:{}", JSONObject.toJSONString(ykcDataProtocol), channel.toString());
        // 获取消息体
        byte[] msgBody = ykcDataProtocol.getMsgBody();

        int startIndex = 0;
        int length = 16;

        // 交易流水号

        byte[] serialNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 桩编码
        startIndex += length;
        length = 7;
        byte[] pileSnByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String pileSn = BytesUtil.bcd2Str(pileSnByteArr);

        // 保存时间
        saveLastTime(pileSn);

        // 枪号
        startIndex += length;
        length = 1;
        byte[] pileConnectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 序号4-6  <00>： =正常； <01>： =超时； <10>： =不可信状态
        startIndex += length;
        byte[] NumFourToSix = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 序号7-9
        startIndex += length;
        byte[] NumSevenToNine = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 序号10-12
        startIndex += length;
        byte[] NumTenToTwelve = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 序号13-14
        startIndex += length;
        byte[] NumThirteenToFourteen = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 序号15-16
        startIndex += length;
        byte[] NumFifteenToSixteen = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 序号17-19
        startIndex += length;
        byte[] NumSeventeenToNineteen = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 序号20-23
        startIndex += length;
        byte[] NumTwentyToTwentyThree = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 序号24-25
        startIndex += length;
        byte[] NumTwentyFourToTwentyFive = BytesUtil.copyBytes(msgBody, startIndex, length);



        return null;
    }
}
