#ifndef HEADER_GAMEIOMANAGER
#define HEADER_GAMEIOMANAGER

/**
 * @file 
 * File containing the interface of gameIOManager module
 */

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#include "define.h"
#include "schemaGameUtility.h"

/* START struct definition */

/** Data struct containing the game's records*/
struct gameRecordValues
{
    /** Player name*/
    char name[MAX_RECORDNAME_LEN + 1];

    /** Collected points*/
    unsigned int points;

    /** Reached level*/
    unsigned int level;
}
 gameRecord[MAX_RECORD_SHOW];

/* END struct definition */


/* Start interface of gameIOManager module */

/** This function manages dinamic data loading processing from the 
    configuration file. It ignores the comments ( COMMENT ) and lines that 
    contain less then one characters ( successives ENDLINE ). It allocates 
    just the same memory of 'maxrow' and 'maxcol' (array of strings), 
    because it also implements lenght controls.
    An addittive control, that verify if all the rows have the same
    number of columns, should be activate setting 'diffcontrol' to TRUE.  
    Note: It isn't necessary to modify this function, although if the need 
    arises to change the structure of the file.
    It invokes ::allocResetVariable, ::skipCharacters, ::readCharacters and
    ::checkDifferentColLen functions.

    @return A pointer to the array of strings created with the function, 
    containing useful data of the file (char **).
*/
char **readfile( char namefile[],             /**< file name*/
                 bool *schema_ok,             /**< error control */
                 unsigned int *mapCurrentRow, /**< row position */
                 unsigned int *mapCurrentCol, /**< column position */
                 unsigned int mapMaxRow,      /**< max row length */
                 unsigned int mapMaxCol,      /**< max column length */
                 bool differentColLen         /**< activate different columns 
                                                  length control */
               );


/**
  * Allocs and reset the variable that will contains file content.
  * @return double char pointer to the new variable.
*/
char **allocResetVariable( unsigned int mapMaxRow,/**< max row length */
                           unsigned int mapMaxCol /**< max column length */
                        );


/**
  * Skips all the EOF, ENDLINE and COMMENT characters.
*/
void skipCharacters(char *fileChar, /**< Readed character */
                    int *checkchar, /**< Integer value of the readed 
                                         character*/
                    FILE *file2read /**< pointer to the opened file to read*/
                    );

/**
  * If there aren't structural form error in file and while the file not end,
  * reads all the characters and puts it into variable contentFile.
*/
bool readCharacters(unsigned int *mapCurrentCol,    /**< column position */
                    unsigned int mapMaxCol,         /**< max column length */
                    const unsigned int *mapCurrentRow, /**< row position */
                    unsigned int mapMaxRow,         /**< max row length */
                    char ** contentFile,            /**< content of the file*/
                    FILE *file2read                 /**< pointer to the opened
                                                    file to read*/
                    );


/**
  * Check if all the rows in the file has the same number of columns. 
*/
bool checkDifferentColLen(  const unsigned int *mapCurrentRow,/**< row 
                                                                   position */
                            const unsigned int *mapCurrentCol,/**< column 
                                                                   position */
                            unsigned int *checkCol     /**< Reference number 
                                                            of columns*/
                           );


/**
    Save the game record list into the record data structure, checking 
    characters using function ::isallowedchar.
    @return Positive value (true) if occurs error/s. 
           Negative value (false) otherwise.
*/
bool setGameRecordStructValue( char **filecontent, /**< Content of the 
                                                              file*/
                               unsigned int numrow,      /**< Number of rows*/
                               unsigned int numcol     /**< Number of columns*/
                             );

/**
  * Read from record file the record list and save it in the record data struct,
    invoking the functions ::readfile and ::setGameRecordStructValue. 
    @return Positive value (true) if no errors occurs. 
            Negative value (false) otherwise.
*/
bool readSetGameRecord( unsigned int *readrows /**< Number of record read */
                      );

/**
  * Save the new record in file, invoking the function ::readSetGameRecord.
  * Warning: this function work properly with inputs composed only by
             letters, digits, COMMENT and ENDLINE.
*/
void save_record( const char *name,         /**< Player name*/
                  unsigned int totalpoints, /**< Total won points*/
                  unsigned int currentlevel /**< Reached level*/
                );

/**
  * Simple function that print on standard output the content of a matrix with 
  * the dimensions row x column in textual format (char).
  * It works only in debug modality.
  * Warning: this function doesn't implement any sort of control on input data;
             make sure that are correct!
*/
void printschema();

/* End interface of gameIOManager module */

#endif
