#include "Texture.h"
#include <png.h>

using namespace std;

GLuint Texture::boundTexture = 0; 

Texture::Texture(const char* path)
{
    if(!load(path))
    {
        loaded = true;
        cout << "cannot load texture " << path << endl;
    }
}

Texture::Texture(const Texture& orig)
{
}

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

bool Texture::load(const char* path)
{
    FILE* file = fopen(path, "r");
    if(file == NULL)
    {
        cerr << "image '" << path << "' does not exist" << endl;
        return false;
    }
    bool b = load(path, file);
    fclose(file);
    if(b)
    {
        initGL();
    }
    return b;
}

bool Texture::load(const char* path, FILE* file)
{
    // check signature of png
    unsigned char buffer[8];
    if(fread(buffer, sizeof(char), 8, file) != 8)
    {
        cerr << "cannot read signature of image '" << path << "'" << endl;
        return false;
    }
    
    if(png_sig_cmp(buffer, 0, 8))        
    {
        cerr << "file '" << path << "' is not an image" << endl;
        return false;
    }
    
    // create structures for data
    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if(png == NULL)
    {
        cerr << "cannot create image data structure" << endl;
        return false;
    }
    
    png_infop info = png_create_info_struct(png);
    if(info == NULL)
    {
       cerr << "cannot create image info structure" << endl;
       return false;
    }

    unsigned int** rowPointers = NULL;
    
    // set callback for errors
    if(setjmp(png_jmpbuf(png)))
    {
        if(rowPointers != NULL)
        {
            png_free(png, rowPointers);
        }
        png_destroy_read_struct(&png, &info, NULL);
        cerr << "image '" << path << "' has used error callback" << endl;
        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 unsigned int[width * height];
    
    // allocate and set row pointer to correct places in block
    rowPointers = (unsigned int**) png_malloc(png, height * (sizeof(unsigned int*)));
    for(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()
{
    glActiveTexture(GL_TEXTURE0);
    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);
    
    /*for(int i = 0; i < width * height; i++)
    {
        if(data[i] != 0)
        {
            cout << "X";
        }
        else
        {
            cout << " ";
        }
        
        if(i != 0 && i % width == 0)
        {
            cout << endl;
        }
    }*/
    
    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;

    glGenerateMipmap(GL_TEXTURE_2D);
}

void Texture::bind()
{
    if(boundTexture != texture)
    {
        boundTexture = texture;
        glBindTexture(GL_TEXTURE_2D, texture);
    }
}

bool Texture::isLoaded()
{
    return loaded;
}