#include <stdio.h>
#include <SDL2/SDL.h>
#include "image_treatment/filter/cleanerimage.h"
#include "tools/arg_parse.h"
#include "neural_network/traitement.h"
#include "neural_network/training.h"
#include "image_treatment/extraction/extraction.h"

int ocr_recognise(char *weight_input_path, char *input_file_path);
int ocr_train(char *weight_input_path, char *weight_output_path);

int main(int argc, char *argv[])
{
    struct arguments arguments;

    arguments.mode               = RECOGNITION;
    arguments.input_file_path    = "";
    arguments.weight_output_path = "";
    arguments.weight_input_path  = "misc/nnweight/nnweight.txt";

    arg_parse(argc, argv, &arguments);

    if (arguments.mode == RECOGNITION)
        ocr_recognise(arguments.weight_input_path, arguments.input_file_path);
    if (arguments.mode == TRAIN)
        ocr_train(arguments.weight_input_path, arguments.weight_output_path);
}

int ocr_recognise(char *weight_input_path, char *input_file_path) {
    // TODO: CHECK FILE EXIST
    SDL_Surface *loadedImage = SDL_LoadBMP(input_file_path);
    binerize(loadedImage);


    Neural_Network *network = (Neural_Network *)malloc(sizeof(Neural_Network));
    Initialisation(network);
    // TODO: CHECK FILE EXIST
    if (!Load_Network(network, weight_input_path))
        exit(1);

    char *result = calloc(10000, sizeof(char));
    __extractpar(loadedImage, network, result);
    printf("Text:\n%s\n", result);
    free(result);

    Free_Network(network);

    return 0;
}

int ocr_train(char *weight_input_path , char *weight_output_path) {
    // TODO: CHECK FILE EXIST
    (void) weight_input_path; // TODO

    Neural_Network *network = (Neural_Network *)malloc(sizeof(Neural_Network));
    Initialisation(network);
    //TODO: LOAD NETWORK

    training(network, 50);

    // TODO: CREATE FILE
    Save_Network(network, weight_output_path);

    Free_Network(network);

    return 0;
}
