#include <stdlib.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gprintf.h>

/* EXO 3.1 */

int main(int argc, char* argv[]){
  g_printf(" === EXO 3.1  === \n");

  GString* tabstr[6];
  tabstr[0]= g_string_new("Programmation");
  tabstr[1]= g_string_new("génial");
  tabstr[2]= g_string_new("GLib");
  tabstr[3]= g_string_new("La");
  tabstr[4]= g_string_new("avec");
  tabstr[5]= g_string_new("c'est");

  
  GIOChannel *channel = g_io_channel_unix_new (STDIN_FILENO);
  gchar *str;
    
  g_printf (" [0] - %s \n", tabstr[0]->str);
  g_printf (" [1] - %s \n", tabstr[1]->str);
  g_printf (" [2] - %s \n", tabstr[2]->str);
  g_printf (" [3] - %s \n", tabstr[3]->str);
  g_printf (" [4] - %s \n", tabstr[4]->str);
  g_printf (" [5] - %s \n", tabstr[5]->str);

  g_printf ("Saisir le numéro de mot : \n");

  GString* strres = g_string_sized_new (100);
  for(guint8 i = 0; i < 6 ; i++){
    g_io_channel_read_line (channel, &str, NULL, NULL, NULL);

    gint64 val = g_ascii_strtoll(str, NULL, 10);
    gint8  newval = CLAMP(val,0,5);
    g_string_append (strres, tabstr[newval]->str);
    g_string_append (strres, " ");
  }

  
  g_printf ("Résultat : %s \n", strres->str);

  if(g_string_equal(strres, g_string_new("La Programmation avec GLib c'est génial ")))
    g_printf ("Bravo !\n");
  else
    g_printf ("Dommage vous avez perdu !\n");
  return EXIT_SUCCESS;
}
