package com.yfd.platform.component.voiceserver;

import cn.hutool.core.util.HexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class AudioDataDecoder extends ByteToMessageDecoder {

    private static final int HEAD_LENGTH = 4;
    private static final int VERSION_LENGTH = 1;
    private static final int SIZE_LENGTH = 4;
    private static final int TIMESTAMP_LENGTH = 8;
    private static final int BASICINFO_LENGTH = 6;
    private static final int ID_LENGTH = 6;
    private static final int FIRMWARE_LENGTH = 3;
    private static final int HARDWARE_LENGTH = 1;
    private static final int PROTOCOL_LENGTH = 2;
    private static final int FLAG_LENGTH = 3;
    private static final int CRC_LENGTH = 2;
    private static final int END_LENGTH = 1;


    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        if (in.readableBytes() < HEAD_LENGTH + VERSION_LENGTH + SIZE_LENGTH) {
            // 等待更多的数据
            return;
        }
        // 读取头标志、版本和包大小
        in.markReaderIndex(); // 标记当前读取位置
        byte[] headBytes = new byte[HEAD_LENGTH];
        in.readBytes(headBytes);
        String head = new String(HexUtil.encodeHex(headBytes, false));
        byte version = in.readByte(); // 读取版本
        int size = in.readInt(); // 读取包大小

        // 检查是否有足够的数据来读取整个包
        if (in.readableBytes() < size) {
            in.resetReaderIndex(); // 重置读取位置
            return;
        }
        // 读取整个数据包
        ByteBuf buf = in.readBytes(size);
        byte lastByte = buf.getByte(buf.readerIndex() + buf.readableBytes() - 1);
        int decimalValue = lastByte & 0xFF;
        if (253 != decimalValue) {
            // 重置读取位置
            in.resetReaderIndex();
        }
        // 解析数据包（这里只展示如何读取头部信息，实际使用时需要解析整个数据包）
        VoicePacket packet = new VoicePacket();
        packet.setHead(head);
        packet.setSize(size);
        packet.setVersion(version);
        packet.setTimestamp(buf.readLong());
        packet.setBasicInfo(readBytes(buf, BASICINFO_LENGTH));
        packet.setId(readBytes(buf, ID_LENGTH));
        packet.setFirmware(readBytes(buf, FIRMWARE_LENGTH));
        packet.setHardware(buf.readByte());
        packet.setProtocol(buf.readShort());
        packet.setFlag(readBytes(buf, FLAG_LENGTH));

        DataGroup dataGroup = new DataGroup();
        dataGroup.setSType(buf.readByte());
        dataGroup.setSampleRate(buf.readShort());
        dataGroup.setBits(buf.readByte());
        dataGroup.setChannel(buf.readByte());
        // 减去CRC和End
        dataGroup.setData(readBytes(buf, buf.readableBytes() - 3));
        packet.setDataGroup(dataGroup);

        packet.setCrc(buf.readShort());
        packet.setEnd(buf.readByte());

        // 将解析后的数据添加到输出列表中
        // 假设我们只需要数据部分
        out.add(packet);
    }

    private byte[] readBytes(ByteBuf buf, int length) {
        byte[] bytes = new byte[length];
        buf.readBytes(bytes);
        return bytes;
    }
}
