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;

/**
 * 充电握手
 *
 * GBT-27930 充电桩与 BMS 充电握手阶段报文
 * @author JS-ZZA
 * @date 2022/9/19 13:20
 */
@Slf4j
@Component
public class ChargingHandshakeHandler extends AbstractHandler{
    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.CHARGING_HANDSHAKE_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);

        // BMS 通信协议版本号  当前版本为 V1.1， 表示为： byte3， byte2—0001H；byte1—01H
        startIndex += length;
        length = 3;
        byte[] BMSCommunicationProtocolVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 电池类型  01H:铅酸电池;02H:氢 电池;03H:磷酸铁锂电池;04H:锰 酸锂电池;05H:钴酸锂电池 ;06H: 三元材料电池;07H:聚合物锂离子 电池;08H:钛酸锂电池;FFH:其他
        startIndex += length;
        length = 1;
        byte[] BMSBatteryTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 整车动力蓄电池系统额定容量  0.1 Ah /位， 0 Ah 偏移量
        startIndex += length;
        length = 2;
        byte[] BMSBatteryCapacityByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 整车动力蓄电池系统额定总电压  0.1V/位， 0V 偏移量
        startIndex += length;
        byte[] BMSBatteryVoltageByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 电池生产厂商名称
        startIndex += length;
        length = 4;
        byte[] BMSBatteryFactoryByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 电池组序号
        startIndex += length;
        byte[] BMSBatteryNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 电池组生产日期年  1985 年偏移量， 数据范围： 1985～ 2235 年
        startIndex += length;
        length = 1;
        byte[] BMSProductionDateYearByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 电池组生产日期月  0 月偏移量， 数据范围： 1～12 月
        startIndex += length;
        byte[] BMSProductionDateMonthByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 电池组生产日期日  0 日偏移量， 数据范围： 1～31 日
        startIndex += length;
        byte[] BMSProductionDateDayByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 电池组充电次数  1 次/位， 0 次偏移量， 以 BMS 统 计为准
        startIndex += length;
        length = 3;
        byte[] BMSChargingTimesByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 电池组产权标识  （<0>： =租赁； <1>： =车自有）
        startIndex += length;
        length = 1;
        byte[] BMSPropertyIdentificationByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 预留位
        startIndex += length;
        byte[] BMSReservePosition = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 车辆识别码
        startIndex += length;
        length = 17;
        byte[] BMSCarIdentifyCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 软件版本号
        startIndex += length;
        length = 8;
        byte[] BMSSoftwareVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);



        return null;
    }
}
