#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

static int read_header(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
{
    size_t headerLEN = strlen(FILE_HEADER_STRING);
    char buffer[BUFFER_SIZE];

    if (headerLEN >= BUFFER_SIZE)
    {
        return 0;
    }

    if (fread(buffer, 1, headerLEN, file) != headerLEN)
    {
        return 0;
    }

    buffer[headerLEN] = '\0';

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

    if (fread(count, sizeof(unsigned short), 1, file) != 1 || fread(width, sizeof(unsigned short), 1, file) != 1 ||
        fread(height, sizeof(unsigned short), 1, file) != 1)
    {
        return 0;
    }

    return 1;
}

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

    if (fread(image->buffer, sizeof(GrayScalePixelType), number_of_pixel, file) != number_of_pixel) // fehler beim lesen
    {
        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 0;
    }

    unsigned short count, width, height;

    if (!read_header(file, &count, &width, &height))
    {
        fclose(file);
        return 0;
    }

    GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));

    if (!series)
    {
        fclose(file);
        return 0;
    }

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

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

    for (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));

        if (!series->images[i].buffer)
        {
            clearSeries(series);
            fclose(file);
            return 0;
        }

        if (!read_single_image(file, &series->images[i]))
        {
            clearSeries(series);
            fclose(file);
            return 0;
        }

        if (fread(&series->labels[i], 1, 1, file) != 1)
        {
            clearSeries(series);
            fclose(file);
            return 0;
        }
    }

    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 (int i = 0; i < series->count; i++)
        {
            free(series->images[i].buffer);
        }
        free(series->images);
        free(series->labels);
        free(series);
    }
}