#include "Image.h"
#include "PngWriter.h"

#include "File.h"
#include "BitStream.h"
#include "ByteUtils.h"
#include "Grid.h"
#include "ImagePrimitives.h"

#include "TestFramework.h"
#include "TestUtils.h"
#include <iostream>

TEST_CASE(TestCompressedPng, "image")
{
    unsigned width = 20;
    unsigned height = 20;
    unsigned numChannels = 1;
    auto image = Image::Create(width, height);
    image->setNumChannels(numChannels);
    image->setBitDepth(8);

    std::vector<unsigned char> data(width*height, 0);
    for (unsigned idx=0; idx<width*height; idx++)
    {
        unsigned char val = 255 * idx /(width*height);
        data[idx] = val;
    }

    image->getGridT<unsigned char>()->setData(data);

    PngWriter writer;
    //writer.setCompressionMethod(Deflate::CompressionMethod::NONE);
    writer.write(TestUtils::getTestOutputDir() / "test_compressed.png", image.get());

    return;
    File test_file(TestUtils::getTestOutputDir() / "test_compressed.png");
    test_file.open(File::AccessMode::Read);

    while(auto byte = test_file.readNextByte())
    {
        //std::cout << static_cast<unsigned>(*byte) << std::endl;
    }
}

TEST_CASE(TestFixedPng, "image")
{
    unsigned width = 10;
    unsigned height = 10;
    unsigned numChannels = 1;
    auto image = Image::Create(width, height);
    image->setNumChannels(numChannels);
    image->setBitDepth(8);

    std::vector<unsigned char> data(width*height, 0);
    for (unsigned idx=0; idx<width*height; idx++)
    {
        //unsigned char val = 100 * idx /(width*height);
        unsigned char val = 10;
        data[idx] = val;
    }

    image->getGridT<unsigned char>()->setData(data);

    PngWriter writer;
    //writer.setCompressionMethod(Deflate::CompressionMethod::FIXED_HUFFMAN);
    writer.write(TestUtils::getTestOutputDir() / "test_fixed.png", image.get());

    //return;
    File test_file(TestUtils::getTestOutputDir() / "test_fixed.png");
    //std::cout << test_file.dumpBinary();

}

TEST_CASE(TestDynamicCompressedPng, "image")
{
    unsigned width = 10;
    unsigned height = 10;
    unsigned numChannels = 1;
    auto image = Image::Create(width, height);
    image->setNumChannels(numChannels);
    image->setBitDepth(8);

    std::vector<unsigned char> data(width*height, 0);
    for (unsigned idx=0; idx<width*height; idx++)
    {
        //unsigned char val = 100 * idx /(width*height);
        unsigned char val = 10;
        data[idx] = val;
    }

    image->getGridT<unsigned char>()->setData(data);

    PngWriter writer;
    //writer.setPath(TestUtils::getTestOutputDir() / "test_dynamic.png");
    writer.write(TestUtils::getTestOutputDir() / "test_dynamic.png", image.get());

    //return;
    File test_file(TestUtils::getTestOutputDir() / "test_dynamic.png");
    //std::cout << test_file.dumpBinary();
}
