#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imageInput.h"

#define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__"

// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei

/* ---------------- Hilfsfunktionen ---------------- */

static int readHeader(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
{
    unsigned short headerlength = strlen(FILE_HEADER_STRING);
    char buffer[headerlength + 1];
    if (fread(buffer, 1, headerlength, file) != headerlength)
        return 0;

    buffer[headerlength] = '\0';

    if (strcmp(buffer, FILE_HEADER_STRING) != 0)
        return 0;

    if (fread(count, sizeof(unsigned short), 1, file) != 1)
        return 0;

    if (fread(width, sizeof(unsigned short), 1, file) != 1)
        return 0;

    if (fread(height, sizeof(unsigned short), 1, file) != 1)
        return 0;

    return 1;
}

static int readSingleImage(FILE *file, GrayScaleImage *image)
{
    unsigned int pixelCount = image->width * image->height;

    if (fread(image->buffer, sizeof(GrayScalePixelType), pixelCount, file) != pixelCount)
        return 0;

    return 1;
}


// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
    FILE *file = fopen(path, "rb");
    if (!file) 
        return NULL;

    unsigned short count, width, height;

    if (!readHeader(file, &count, &width, &height)) {
        fclose(file);
        return NULL;
    }

    GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
    series->count = count;
    series->images = malloc(count * sizeof(GrayScaleImage));
    series->labels = malloc(count * sizeof(unsigned char));

    if (!series || !series->images || !series->labels) 
    {
        free(series->images);
        free(series->labels);
        free(series);
        fclose(file);
        return NULL;
    }

    for (unsigned int i = 0; i < count; i++)
    {
        series->images[i].width = width;
        series->images[i].height = height;
        series->images[i].buffer = malloc(width * height * sizeof(GrayScalePixelType));

        //malloc prüfen
        if (!series->images[i].buffer) 
        {
            clearSeries(series);
            fclose(file);
            return NULL;
        }

        //Image einlesen + prüfen
        if (!readSingleImage(file, &series->images[i])) {
            fclose(file);
            clearSeries(series);
            return NULL;
        }

        // label einlesen
        if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
            fclose(file);
            clearSeries(series);
            return NULL;
        }
    }

    fclose(file);
    return series;
}

// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
    if(series)
    {
        for(unsigned int i = 0; i < series->count; i++)
        {
            free(series->images[i].buffer);
        }

        free(series->images);
        free(series->labels);
        free(series);
    }
}