#include <cstring>
#include <iostream>
#include <libpng16/png.h>

#include "images/PNGReader.h"

PNGReader::PNGReader(const char* path)
    : path(path), width(0), height(0), channels(0), bitDepth(0), rowBytes(0),
      file(fopen(path, "r")), read(nullptr), info(nullptr),
      rowPointers(nullptr) {
    if(file == nullptr) {
        std::cout << "file '" << path << "' cannot be read: " << strerror(errno)
                  << "\n";
        return;
    }
    if(checkSignature()) {
        return;
    }
    read = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr,
                                  nullptr);
    if(read == nullptr) {
        std::cout << "cannot create png read data structure\n";
        return;
    }
    info = png_create_info_struct(read);
    if(info == nullptr) {
        std::cout << "cannot create png info structure\n";
        return;
    }
    if(setjmp(png_jmpbuf(read))) {
        std::cout << "png file '" << path << "' has used error callback\n";
        return;
    }
    png_init_io(read, file);
    png_set_sig_bytes(read, 8);
    png_read_info(read, info);

    png_set_expand(read);

    width = png_get_image_width(read, info);
    height = png_get_image_height(read, info);
    channels = png_get_channels(read, info);
    bitDepth = png_get_bit_depth(read, info);
    if(png_get_bit_depth(read, info) == 16) {
        png_set_strip_16(read);
    }
    rowBytes = png_get_rowbytes(read, info);
}

PNGReader::~PNGReader() {
    if(file != nullptr) {
        fclose(file);
    }
    if(rowPointers != nullptr) {
        png_free(read, rowPointers);
    }
    png_destroy_read_struct(&read, &info, nullptr);
}

int PNGReader::getWidth() const {
    return width;
}

int PNGReader::getHeight() const {
    return height;
}

int PNGReader::getChannels() const {
    return channels;
}

int PNGReader::getBufferSize() const {
    return width * height * channels;
}

bool PNGReader::hasError() const {
    if(channels < 1 || channels > 4) {
        std::cout << "'" << path
                  << "' has unsupported number of channels: " << channels
                  << "\n";
        return true;
    } else if(width < 1 || width > 2048 || height < 1 || height > 2048) {
        std::cout << "width and height of '" << path << "' are too big\n";
        return true;
    } else if(bitDepth != 8 && bitDepth != 16) {
        std::cout << "bit depth of '" << path << "' is neither 8 or 16\n";
        return true;
    } else if(getBufferSize() != (rowBytes * height * 8 / bitDepth)) {
        std::cout << "'" << path << "' needs an unexpected buffer size\n";
        return true;
    }
    return false;
}

bool PNGReader::checkSignature() {
    png_byte buffer[8];
    if(fread(buffer, sizeof(png_byte), 8, file) != 8) {
        std::cout << "cannot read signature of file '" << path << "'\n";
        return true;
    }
    if(png_sig_cmp(buffer, 0, 8)) {
        std::cout << "file '" << path << "' is not a png\n";
        return true;
    }
    return false;
}

bool PNGReader::readData(ColorChannel* buffer) {
    if(setjmp(png_jmpbuf(read))) {
        std::cout << "png file '" << path << "' has used error callback\n";
        return true;
    }
    rowPointers = static_cast<ColorChannel**>(
        png_malloc(read, height * sizeof(ColorChannel*)));
    for(int i = 0; i < height; i++) {
        rowPointers[i] = (buffer + i * width * channels);
    }
    png_set_rows(read, info, reinterpret_cast<png_bytepp>(rowPointers));
    png_read_image(read, reinterpret_cast<png_bytepp>(rowPointers));
    return false;
}