#include <gtk/gtk.h>
#include <math.h>
typedef struct{
  GtkWidget *win;
  GtkWidget *vbox;
  GtkWidget *bt_quit;
  GtkWidget *draw_area;
}UserData;


gboolean fct_rappel(gpointer data){
  UserData* uwData = data;
  gtk_widget_queue_draw(uwData->draw_area);
  return TRUE;
}


gboolean refresh_draw (GtkWidget *widget, cairo_t *cr, gpointer dataColor){
  guint width, height;
  gdouble m_radius = 0.42;
  gdouble m_line_width = 0.05;

  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);


  cairo_scale(cr, width, height); //mise à l'échelle
  cairo_translate(cr, 0.5, 0.5);
  cairo_set_line_width(cr, m_line_width);

  cairo_save(cr);
  cairo_set_source_rgba(cr, 0.337, 0.612, 0.117, 0.9);   // green
  cairo_paint(cr);
  cairo_restore(cr);
  cairo_arc(cr,0, 0, m_radius, 0, 2 * M_PI);
  cairo_save(cr);
  cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.8);
  cairo_fill_preserve(cr);
  cairo_restore(cr);
  cairo_stroke_preserve(cr);
  cairo_clip(cr);

  //clock ticks
  for (int i = 0; i < 12; i++)
  {
    double inset = 0.05;

    cairo_save(cr);
    cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND);

    if(i % 3 != 0)
    {
      inset *= 0.8;
      cairo_set_line_width(cr,0.03);
    }

    cairo_move_to(cr,
      (m_radius - inset) * cos (i * M_PI / 6),
      (m_radius - inset) * sin (i * M_PI / 6));
    cairo_line_to (cr,
      m_radius * cos (i * M_PI / 6),
      m_radius * sin (i * M_PI / 6));
    cairo_stroke(cr);
    cairo_restore(cr); /* stack-pen-size */
  }

  // store the current time
  time_t rawtime;
  time(&rawtime);
  struct tm * timeinfo = localtime (&rawtime);

  // compute the angles of the indicators of our clock
  double minutes = timeinfo->tm_min * M_PI / 30;
  double hours = timeinfo->tm_hour * M_PI / 6;
  double seconds= timeinfo->tm_sec * M_PI / 30;

  cairo_save(cr);
  cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);

  // draw the seconds hand
  cairo_save(cr);
  cairo_set_line_width(cr, m_line_width / 3);
  cairo_set_source_rgba(cr, 0.7, 0.7, 0.7, 0.8); // gray
  cairo_move_to(cr, 0, 0);
  cairo_line_to(cr, sin(seconds) * (m_radius * 0.9),
    -cos(seconds) * (m_radius * 0.9));
  cairo_stroke(cr);
  cairo_restore(cr);

  // draw the minutes hand
  cairo_set_source_rgba(cr, 0.117, 0.337, 0.612, 0.9);   // blue
  cairo_move_to(cr, 0, 0);
  cairo_line_to(cr, sin(minutes + seconds / 60) * (m_radius * 0.8),
    -cos(minutes + seconds / 60) * (m_radius * 0.8));
  cairo_stroke(cr);

  // draw the hours hand
  cairo_set_source_rgba(cr, 0.337, 0.612, 0.117, 0.9);   // green
  cairo_move_to(cr, 0, 0);
  cairo_line_to(cr, sin(hours + minutes / 12.0) * (m_radius * 0.5),
    -cos(hours + minutes / 12.0) * (m_radius * 0.5));
  cairo_stroke(cr);
  cairo_restore(cr);

  // draw a little dot in the middle
  cairo_arc(cr, 0, 0, m_line_width / 3.0, 0, 2 * M_PI);

  cairo_fill (cr);

  
}


int main(int argc, char* argv[]){
  UserData uData;
   
  gtk_init(&argc, &argv);
  //Création de la window
  uData.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (uData.win), 10);
  g_signal_connect (G_OBJECT(uData.win),"delete_event",G_CALLBACK(gtk_main_quit) ,NULL);
  //box verticale 
  uData.vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,10);
  gtk_container_add(GTK_CONTAINER(uData.win),uData.vbox);


  
  //Bouton quit
  uData.bt_quit = gtk_button_new_with_label("Fermer");
  gtk_box_pack_start (GTK_BOX(uData.vbox), uData.bt_quit, FALSE, FALSE, 5);
  g_signal_connect (G_OBJECT (uData.bt_quit), "clicked", G_CALLBACK (gtk_main_quit), &uData);

 
  //Zone couleur
  uData.draw_area = gtk_drawing_area_new ();
  gtk_widget_set_size_request (uData.draw_area, 400, 400);
  gtk_box_pack_start (GTK_BOX(uData.vbox), uData.draw_area, TRUE, TRUE, 5);

  g_signal_connect (G_OBJECT (uData.draw_area), "draw", G_CALLBACK (refresh_draw), NULL);


  //timer
  g_timeout_add(1000, fct_rappel, &uData);

  
  gtk_widget_show_all(uData.win);
  gtk_main();
  return 0;
}
