#include <png.h>

#include "client/rendering/Texture.h"

Texture::Texture(const char* path) : data(nullptr), texture(0) {
    load(path);
}

Texture::~Texture() {
    if(data != nullptr) {
        delete[] data;
    }
    glDeleteTextures(1, &texture);
}

void Texture::load(const char* path) {
    FILE* file = fopen(path, "r");
    if(file == nullptr) {
        std::cout << "texture '" << path << "' does not exist\n";
        return;
    }
    bool b = load(path, file);
    fclose(file);
    if(b) {
        initGL();
    }
}

bool Texture::load(const char* path, FILE* file) {
    // check signature of png
    unsigned char buffer[8];
    if(fread(buffer, sizeof (char), 8, file) != 8) {
        std::cout << "cannot read signature of texture '" << path << "'\n";
        return false;
    }

    if(png_sig_cmp(buffer, 0, 8)) {
        std::cout << "file '" << path << "' is not a texture\n";
        return false;
    }

    // create structures for data
    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;

    // set callback for errors
    if(setjmp(png_jmpbuf(png))) {
        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;
    }

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

    // read info data
    png_read_info(png, info);
    width = png_get_image_width(png, info);
    height = png_get_image_height(png, info);

    // read image data
    data = new u32[width * height];

    // allocate and set row pointer to correct places in block
    rowPointers = (u32**) png_malloc(png, height * (sizeof (u32*)));
    for(unsigned int i = 0; i < height; i++) {
        rowPointers[i] = (data + i * width);
    }
    png_set_rows(png, info, (png_bytepp) rowPointers);

    png_read_image(png, (png_bytepp) rowPointers);

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

void Texture::initGL() {
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
    delete[] data;
    data = nullptr;
}

void Texture::bind(unsigned int index) const {
    glActiveTexture(GL_TEXTURE0 + index);
    glBindTexture(GL_TEXTURE_2D, texture);
}