package com.jsowell.netty.handler.yunkuaichong;

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;

/**
 * 离线卡数据清除应答
 *
 * @author JS-ZZA
 * @date 2022/9/27 9:59
 */
@Slf4j
@Component
public class OfflineCardDataCleaningResponseHandler extends AbstractHandler{
    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.OFFLINE_CARD_DATA_CLEANING_ANSWER_CODE.getBytes());

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

    @Override
    public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
        // log.info("[===离线卡数据清除应答===] param:{}, channel:{}", JSON.toJSONString(ykcDataProtocol), channel.toString());
        // 消息体
        byte[] msgBody = ykcDataProtocol.getMsgBody();

        int startIndex = 0;
        int length = 7;

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

        // 保存时间
        saveLastTimeAndCheckChannel(pileSn, channel);

        // 第 1 个卡物理卡号
        startIndex += length;
        length = 8;
        byte[] firstCardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 清除标记  0x00 清除失败 0x01 清除成功
        startIndex += length;
        length = 1;
        byte[] cleanFlagByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 失败原因  0x01 卡号格式错误  0x02 清除成功
        startIndex += length;
        byte[] failedReasonByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 第N个物理卡号

        // 清除标记

        // 失败原因



        return null;
    }
}
