#pragma once

#include "PngHeader.h"
#include "Image.h"
#include "DeflateElements.h"

#include <memory>
#include <string>
#include <filesystem>

using Path = std::filesystem::path;

class BitStream;
class BufferBitStream;
class File;

class PngWriter
{
public:
    PngWriter();
    ~PngWriter();

    static std::unique_ptr<PngWriter> Create();

    void setCompressionMethod(Deflate::CompressionMethod method);

    void setPath(const Path& path);

    void setPngInfo(const PngInfo& info);

    void write(const std::unique_ptr<Image<unsigned char> >& image);

private:
    void writeSignature();
    void writeHeader();

    void writeDataChunks(const BufferBitStream& buffer);

    void writeEndChunk();

    //void writeIDatChunk();

    Path mPath;
    Image<unsigned char>* mWorkingImage{nullptr};
    std::unique_ptr<BitStream> mInStream;
    std::unique_ptr<BitStream> mOutStream;
    std::unique_ptr<File> mWorkingFile;

    unsigned mPngInfoUserSet{false};
    PngInfo mPngInfo;
    PngHeader mPngHeader;

    Deflate::CompressionMethod mCompressionMethod{Deflate::CompressionMethod::DYNAMIC_HUFFMAN};
};

using PngWriterPtr = std::unique_ptr<PngWriter>;
