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

#include "client/utils/PNGReader.h"

static bool checkSignature(const char* path, FILE* file) {
    unsigned char buffer[8];
    if(fread(buffer, sizeof (char), 8, file) != 8) {
        std::cout << "cannot read signature of texture '" << path << "'\n";
        return true;
    }
    if(png_sig_cmp(buffer, 0, 8)) {
        std::cout << "file '" << path << "' is not a texture\n";
        return true;
    }
    return false;
}

static bool load(const char* path, FILE* file, u32* buffer, u32& maxWidth, u32& maxHeight) {
    if(checkSignature(path, file)) {
        return false;
    }

    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
    if(png == nullptr) {
        std::cout << "cannot create texture data structure\n";
        return false;
    }

    png_infop info = png_create_info_struct(png);
    if(info == nullptr) {
        std::cout << "cannot create image info structure\n";
        return false;
    }

    u32** rowPointers = nullptr;

    if(setjmp(png_jmpbuf(png))) { // set callback for errors
        if(rowPointers != nullptr) {
            png_free(png, rowPointers);
        }
        png_destroy_read_struct(&png, &info, nullptr);
        std::cout << "texture '" << path << "' has used error callback\n";
        return false;
    }

    png_init_io(png, file); // set reading function
    png_set_sig_bytes(png, 8); // notify about already used signature bytes

    png_read_info(png, info); // read info data
    
    u32 width = png_get_image_width(png, info);
    u32 height = png_get_image_height(png, info);
    if(width > maxWidth || height > maxHeight) {
        std::cout << "texture '" << path << "' is too big: " << maxWidth << " x " << maxHeight << " allowed\n";
        return false;
    }
    maxWidth = width;
    maxHeight = height;
    
    // allocate and set row pointer to correct places in block
    rowPointers = static_cast<u32**> (png_malloc(png, height * (sizeof (u32*))));
    for(uint i = 0; i < height; i++) {
        rowPointers[i] = (buffer + i * width);
    }
    png_set_rows(png, info, reinterpret_cast<png_bytepp> (rowPointers));

    png_read_image(png, reinterpret_cast<png_bytepp> (rowPointers));

    png_free(png, rowPointers);
    png_destroy_read_struct(&png, &info, nullptr);
    return true;
}

bool PNGReader::load(const char* path, u32* buffer, u32& maxWidth, u32& maxHeight) {
    FILE* file = fopen(path, "r");
    if(file == nullptr) {
        std::cout << "texture '" << path << "' cannot be read: " << strerror(errno) << "\n";
        return false;
    }
    bool result = load(path, file, buffer, maxWidth, maxHeight);
    fclose(file);
    return result;
}