#ifndef HEADER_GUI
#define HEADER_GUI

/**
 * @file 
 * File containing the interface of guihandler module
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
#include <time.h>
#include <glade/glade.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <dirent.h>
#include "define.h"
#include "object.h"
#include "schemaGameUtility.h"
#include "bestroute.h"
#include "gameIOManager.h"
#include "schemaGameGenerator.h"


/* Start interface of guihandler module */

/** Glade GUI file name.*/
static const char GLADE_GUI_FILE_NAME[] = "gui.glade";
/** Main window name.*/
static const char MAIN_WINDOW_NAME[] = "mainwindow";
/** Draw area name.*/
static const char DRAW_AREA_NAME[] = "drawarea";
/** Down label name.*/
static const char DOWN_LABEL_NAME[] = "downlabel";


/**
 * Initialization of the gui creation of the main window, setting of the menues
 * (::createmenubar) and linking the window events (::delete_event,
 * ::destroy, ::configure_event, ::expose_event and ::keypressed).
*/
void gui( int argc,   /**< Corresponding of argc variable of main function*/
          char **argv /**< Corresponding of argv variables of main function*/
        );


/**
* Window close event, confirm' request on exit.
*/
gboolean delete_event( GtkWidget *window, /**< Reference to GtkWidget window*/
                       GdkEvent  *event,  /**< Reference to the event*/
                       gpointer  data     /**< Other data*/
                     );

/**
  * Create a new tournament, starting from level 1, and load the schema.
  * The schema name is obtained using ::get_schema_name function and the
  * game starts using the ::newgame and ::drawschema functions.
*/
void new_tournament( GtkWidget *window, /**< Reference to GtkWidget window*/
                     GtkWidget *widget, /**< Reference to GtkWidget widget*/
                     gpointer data      /**< Other data*/
                   );

/**
  * Print the schema in the drawing area, 
  * using ::color and ::draw_brush functions.
*/
void drawschema();

/**
 * Create a new backing pixmap of the appropriate size. 
*/
gboolean configure_event( GtkWidget *widget,        /**< Reference to 
                                                        GtkWidget widget*/
                          GdkEventConfigure *event, /**< Reference to 
                                                        the event*/
                          gpointer data             /**< Other data*/
                        );

/** 
  * Redraw the screen from the backing pixmap. 
*/
gboolean expose_event( GtkWidget *widget,  /**< Reference to GtkWidget widget*/
                       GdkEventExpose *event, /**< Reference to the event*/
                       gpointer data        /**< Other data*/
                      );

/**
  * Draw a rectangle in the drawing area. 
*/
void draw_brush( GtkWidget *widget,  /**< Reference to GtkWidget widget*/
                 gdouble x,          /**< x position of the rectangle*/
                 gdouble y,          /**< y position of the rectangle*/
                 gdouble width,      /**< width length of the rectangle*/
                 gdouble height,     /**< height length of the rectangle*/
                 char *col           /**< Colour*/
               );

/**
  * Check the key pressed by the player using ::checkKeyPressed function.
*/
void keypressed( GtkWidget *window,   /**< Reference to GtkWidget window*/
                 GdkEventKey *event,  /**< Reference to the key event*/
                 gpointer data        /**< Other data*/
               );


/**
  * Check the key value passed by ::keypressed function. If correct, invokes
    ::updateSchemaCell function to update the schema and draw the brush.
*/
void checkKeyPressed( int keyval,   /**< Int value of the key pressed*/
                      char *key,    /**< Output message*/
                      GtkWidget *drawarea,/**< Reference to the drawarea*/
                      unsigned int drawX, /**< Width length of the rectangle*/
                      unsigned int drawY  /**< Height length of the rectangle*/
                    );


/**
  * Updates schema values and invokes ::draw_brush function 
    to drawing the new brush.
*/
void updateSchemaCell(GtkWidget *drawarea,/**< Reference to the drawarea*/
                      unsigned int drawX, /**< Width length of the rectangle*/
                      unsigned int drawY  /**< Height length of the rectangle*/
                     );


/** Show the summary of the game, after a schema is won. 
  * The schema results are calculated with ::calcSchemaResults function.
  * If tournament isn't finished, start a new one using the function
  * ::new_game, otherwise it saves and shows the record invoking the 
  * functions ::get_player_name, ::save_record and ::show_records.
*/
void win( );


/**
  * Show records in a dialog window using the function ::read_record.
*/
void show_records( GtkWidget *widget,   /**< Reference to GtkWidget widget*/ 
                   GtkWindow *window    /**< Reference to GtkWidget window*/
                 );

/**
  * Get player name from a dialog, to save the new record, 
  * call ::checkResponseNameError function to check the 
  * inserted characters.
  * If the name is wrong and user 'says yes', asks again the name.
  * @return true if function return a valid name, false otherwise.
*/
bool get_player_name( GtkWidget *window, /**< Reference to GtkWidget window*/
                      gchar *input_text  /**< Player name */
                    );

/**
  * Check if the input text is correct, using ::isallowedchar function.
  * If it isn't correct, show a dialog with the error message.
  * @return true if input text is correct, false otherwise.
*/
bool checkResponseNameError( GtkWidget *window, 
                             char *input_text
                            );

/**
  * Calls the function ::drawschema, writes informations
  * about elapsed time into downlabel and sets values of schema
  * game informations.
*/
void drawingAndSetSchemaInfo( double loadsuccess /**< Time to load and
                                                      validate schema*/
                            );

/**
  * Open the selected game by player,
  * using the ::newgame and ::drawschema functions.
*/
void open_game( GtkWidget *window,  /**< Reference to GtkWidget window*/
                GtkWidget *widget,  /**< Reference to GtkWidget widget*/
                gpointer data       /**< Other data*/
              );


/**
  * Generate a new game, saving it in the file selected by the player, and run 
  * it. The parameters inserted by the player are checked with the function 
  * ::request_generate_parameters, and the schema is generated with the 
  * function ::generateSchema. The schema is drawn using ::drawschema function.
*/
void generate_game( GtkWidget *widget,   /**< Reference to GtkWidget widget*/
                    GtkWindow *window    /**< Reference to GtkWidget window*/
                  );


/** Create a new dialog window for input the necessary
  * parameters to generate a new schema and check the inserted values
  * with ::isallowedchar function.
  * The function becomes recursively if data are wrong and user 'says yes'.
  * @return true if parameters are ok, false otherwise.
 */
bool request_generate_parameters( GtkWindow *window,   /**< Reference to 
                                                           GtkWidget window*/
                                  unsigned int *rows,  /**< Number of rows*/
                                  unsigned int *cols,  /**< Number of columns*/
                                  unsigned int *difficult /**< Level*/
                                );


/**
  * Check if the input text is correct, using ::isallowedchar function.
  * If it isn't correct, show a dialog with the error message.
  * @return true if input text is correct, false otherwise.
*/
bool checkResponseErrorGenerateParameters(GtkWindow *window,/**< Reference to 
                                                             GtkWidget window*/
                                          char *rows_text, /**< Input text for 
                                                             row field */
                                          char *cols_text, /**< Input text for 
                                                             columns field */
                                          char *difficult_text,/**< Input text 
                                                             for difficult 
                                                             field */
                                          unsigned int *rows,/**< Number of 
                                                             rows */
                                          unsigned int *cols,/**< Number of
                                                             columns */
                                          unsigned int *difficult/**< Difficult
                                                             level */
                                         );

/** Set the text of widget (GTK_ENTRY) with the
    number passed as parameter.
    @return the number converted to string
*/
char *set_gtk_entry_text_from_num( unsigned int number,/**< Number to convert*/
                                    GtkWidget *entry   /**< Entry to set*/
                                 );

/**
*  Show HELP informations, in a dialog window.
*/
void show_help( GtkWidget *widget,    /**< Reference to GtkWidget widget*/ 
                GtkWidget *window,    /**< Reference to GtkWidget window*/
                gpointer data         /**< Other data*/
              );

/**
*  Show all ABOUT informations in a dialog window.
*/
void show_about( GtkWidget *widget, /**< Reference to GtkWidget widget*/
                 GtkWidget *window  /**< Reference to GtkWidget window*/
               );


/* End interface of guihandler module */

#endif
