#pragma once
#include <htslib/sam.h>
#include <stdio.h>

#define INCREASE_SIZE (8L * 1024 * 1024)

struct DataBuffer {
    uint8_t *data;
    size_t readPos = 0; // 当前读取的位置
    size_t curLen = 0;  // 当前使用的空间
    size_t maxLen = 0;  // 最大空间

    DataBuffer() {
        curLen = 0;
        maxLen = 0;
        data = nullptr;
    }
    DataBuffer(size_t initSize) {
        curLen = 0;
        maxLen = initSize;
        data = (uint8_t *)malloc(maxLen);
    }
    ~DataBuffer() {
        if (data)
            free(data);
    }
    void allocMem(size_t memSize) {
        curLen = 0;
        maxLen = memSize;
        data = (uint8_t *)realloc(data, maxLen);
    }

    void reAllocMem(size_t memSize) {
        if (memSize > maxLen) {
            maxLen = memSize;
            data = (uint8_t *)realloc(data, maxLen);
        }
    }
};

struct HeaderBuf {
    uint8_t *data = nullptr; // 保留除header之外的解压数据（即最开始的bam记录）
    int dataLen = 0; // 解压数据的字节数
    sam_hdr_t *header;
};

// 解压之后的数据结构
struct UBlockBuf {
    uint8_t *data;
    
};

static int unpackInt16(const uint8_t *buffer) { return buffer[0] | buffer[1] << 8; }

void parseSamHeader(FILE *fp, HeaderBuf &hdrBuf);

int bgzfUncompress(uint8_t *dst, size_t *dlen, const uint8_t *src, size_t slen, uint32_t expected_crc);