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

#ifndef BLOCK_HEADER_LENGTH
#define BLOCK_HEADER_LENGTH 18
#endif
#ifndef BLOCK_FOOTER_LENGTH
#define BLOCK_FOOTER_LENGTH 8
#endif

#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() {
        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;
    int bamPos;  // 第一条bam的位置
    int curLen;
    int maxLen;
    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);