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

#define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__"

static void strip_newline(char *s)
{
    if (!s) return;
    size_t len = strlen(s);
    while (len > 0 && (s[len-1] == '\n' || s[len-1] == '\r')) {
        s[len-1] = '\0';
        len--;
    }
}

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

// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
    if (!path) return NULL;

    FILE *fp = fopen(path, "r");
    if (!fp) return NULL;

    char header[BUFFER_SIZE];
    if (!fgets(header, sizeof(header), fp)) {
        fclose(fp);
        return NULL;
    }
    strip_newline(header);
    if (strcmp(header, FILE_HEADER_STRING) != 0) {
        fclose(fp);
        return NULL;
    }

    unsigned int count = 0;
    if (fscanf(fp, "%u", &count) != 1) {
        fclose(fp);
        return NULL;
    }

    GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
    if (!series) {
        fclose(fp);
        return NULL;
    }
    series->count = count;
    series->images = NULL;
    series->labels = NULL;

    if (count == 0) {
        series->images = NULL;
        series->labels = NULL;
        fclose(fp);
        return series;
    }

    series->images = calloc(count, sizeof(GrayScaleImage));
    series->labels = calloc(count, sizeof(unsigned char));
    if (!series->images || !series->labels) {
        clearSeries(series);
        fclose(fp);
        return NULL;
    }

    for (unsigned int i = 0; i < count; ++i) {
        unsigned int label = 0;
        unsigned int width = 0, height = 0;
        if (fscanf(fp, "%u %u %u", &label, &width, &height) != 3) {
            clearSeries(series);
            fclose(fp);
            return NULL;
        }
        series->labels[i] = (unsigned char)(label & 0xFF);
        series->images[i].width = width;
        series->images[i].height = height;

        size_t pixels = (size_t)width * (size_t)height;
        if (pixels == 0) {
            series->images[i].buffer = NULL;
            continue;
        }

        series->images[i].buffer = malloc(pixels * sizeof(GrayScalePixelType));
        if (!series->images[i].buffer) {
            clearSeries(series);
            fclose(fp);
            return NULL;
        }

        for (size_t p = 0; p < pixels; ++p) {
            int pix = 0;
            if (fscanf(fp, "%d", &pix) != 1) {
                clearSeries(series);
                fclose(fp);
                return NULL;
            }
            if (pix < 0) pix = 0;
            if (pix > 255) pix = 255;
            series->images[i].buffer[p] = (GrayScalePixelType)pix;
        }
    }

    fclose(fp);
    return series;
}

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