#include <plot.h>

GtkWidget *da_overview;

float coordinate_system_settings[5];
float teta;

float sun_x, sun_y, sun_z;
float moon_x, moon_y, moon_z;
float mw_x, mw_y, mw_z;

float array_sun_x[288], array_sun_y[288], array_sun_z[288];
float array_moon_x[288], array_moon_y[288], array_moon_z[288];
float array_mw_x[288], array_mw_y[288], array_mw_z[288];

void plot_set_basic_coordinate_system_settings (void){
  coordinate_system_settings[0] = -G_PI*3/5;
  coordinate_system_settings[1] = -G_PI*2/5;
  coordinate_system_settings[2] = 1;
  coordinate_system_settings[3] = 1;
  coordinate_system_settings[4] = 1.5;
}

void plot_adjust_teta (float value){
  teta = -value;
  gtk_widget_queue_draw (da_overview);
}

void plot_adjust_coordinate_system_settings (double s0, double s1, double s2, double s3, double s4){
  coordinate_system_settings[0] = s0;
  coordinate_system_settings[1] = s1;
  coordinate_system_settings[2] = s2;
  coordinate_system_settings[3] = s3;
  coordinate_system_settings[4] = s4;
}

double plot_calc_coordinate_x (double a1, double a2) {
  float alpha, beta;
  double x,a1_new, a2_new;

  if (!coordinate_system_settings[0]){
    plot_set_basic_coordinate_system_settings();
  }

  alpha = *coordinate_system_settings+G_PI;
  beta = *(coordinate_system_settings+1);
  a1_new = cos(teta)*a1 - sin(teta)*a2;
  a2_new = sin(teta)*a1 + cos(teta)*a2;
  a1_new *= coordinate_system_settings[2];
  a2_new *= coordinate_system_settings[3];
  x = (sin(alpha)*a1_new-sin(beta)*a2_new)/(cos(beta)*sin(alpha)-sin(beta)*cos(alpha));
  return x;
}

double plot_calc_coordinate_y (double a1, double a2, double a3) {
  float alpha, beta;
  double y, a1_new, a2_new;

  if (!coordinate_system_settings[0]){
    plot_set_basic_coordinate_system_settings();
  }

  alpha = *coordinate_system_settings+G_PI;
  beta = *(coordinate_system_settings+1);
  a1_new = cos(teta)*a1 - sin(teta)*a2;
  a2_new = sin(teta)*a1 + cos(teta)*a2;
  a1_new *= coordinate_system_settings[2];
  a2_new *= coordinate_system_settings[3];
  a3 *= coordinate_system_settings[4];
  y = (-cos(alpha)*a1_new+cos(beta)*a2_new)/(cos(beta)*sin(alpha)-sin(beta)*cos(alpha)) + a3;
  return y;
}

void plot_line_to (cairo_t *cr, double a1, double a2, double a3){
  cairo_line_to (cr, plot_calc_coordinate_x (a1, a2), plot_calc_coordinate_y (a1, a2, a3));
}

void plot_move_to (cairo_t *cr, double a1, double a2, double a3) {
  cairo_move_to (cr, plot_calc_coordinate_x (a1, a2), plot_calc_coordinate_y (a1, a2, a3));
}

void plot_vector_to (cairo_t *cr, double a1, double a2, double a3) {
  double current_x = 0;
  double current_y = 0;
  double *x_1 = &current_x;
  double *y_1 = &current_y;
  double x_2 = plot_calc_coordinate_x (a1, a2);
  double y_2 = plot_calc_coordinate_y (a1, a2, a3);

  cairo_get_current_point (cr, x_1, y_1);
  double direct_x = x_2-*x_1;
  double direct_y = y_2-*y_1;
  double norm = pow(pow(direct_x,2) + pow(direct_y,2), 0.5);
  direct_x /= norm;
  direct_y /= norm;
  /*g_print("direct_x: %f, direct_y: %f\n", direct_x, direct_y);*/

  float gamma = 0.5326;
  cairo_line_to (cr, x_2, y_2);
  cairo_move_to (cr, x_2+(-direct_x*cos(gamma)+direct_y*sin(gamma))*0.2, y_2+(-direct_y*cos(gamma)-direct_x*sin(gamma))*0.2);
  cairo_line_to (cr, x_2, y_2);
  cairo_line_to (cr, x_2+(-direct_x*cos(gamma)-direct_y*sin(gamma))*0.2, y_2+(-direct_y*cos(gamma)+direct_x*sin(gamma))*0.2);

}

double plot_rotate_coordinate (double x, double y, double z, int coordinate_index, float rot, int rot_index){
  /* coordniate_index is used to specify if the coordinate is x, y or z component
   * rot_index is used to specify if the rotation is around the x, y or z achsis */
  if (rot_index == 0){
    if (coordinate_index == 0) { return x; }
    else if (coordinate_index == 1){ return cos(rot)*y-sin(rot)*z; }
    else{ return sin(rot)*y+cos(rot)*z; }
  }
  else if (rot_index == 1){
    if (coordinate_index == 0){ return cos(rot)*x+sin(rot)*z; }
    else if (coordinate_index == 1){ return y; }
    else { return -sin(rot)*x+cos(rot)*z; }
  }
  else {
    if (coordinate_index == 0){ return cos(rot)*x-sin(rot)*y; }
    else if (coordinate_index == 1){ return sin(rot)*x+cos(rot)*y; }
    else { return z; }
  }
}

void plot_circle (cairo_t *cr, double radius, float rot1, float rot2, double render){
  double x = radius;
  double y = 0;
  double z = 0;
  double x_plot, y_plot, z_plot;

  x_plot = plot_rotate_coordinate (x,y,z,0,rot1,0);
  y_plot = plot_rotate_coordinate (x,y,z,1,rot1,0);
  z_plot = plot_rotate_coordinate (x,y,z,2,rot1,0);
  x = x_plot; y = y_plot; z = z_plot;
  x_plot = plot_rotate_coordinate (x,y,z,0,rot2,1);
  y_plot = plot_rotate_coordinate (x,y,z,1,rot2,1);
  z_plot = plot_rotate_coordinate (x,y,z,2,rot2,1);

  plot_move_to (cr, x_plot, y_plot, z_plot);
  for (double ang = 2*G_PI/render; ang<=2*G_PI; ang += 2*G_PI/render){
    x = radius * cos(ang);
    y = radius * sin(ang);
    z = 0;

    x_plot = plot_rotate_coordinate (x,y,z,0,rot1,0);
    y_plot = plot_rotate_coordinate (x,y,z,1,rot1,0);
    z_plot = plot_rotate_coordinate (x,y,z,2,rot1,0);
    x = x_plot; y = y_plot; z = z_plot;
    x_plot = plot_rotate_coordinate (x,y,z,0,rot2,1);
    y_plot = plot_rotate_coordinate (x,y,z,1,rot2,1);
    z_plot = plot_rotate_coordinate (x,y,z,2,rot2,1);

    plot_line_to (cr, x_plot, y_plot, z_plot);
  }
  x = radius;
  y = 0;
  z = 0;

  x_plot = plot_rotate_coordinate (x,y,z,0,rot1,0);
  y_plot = plot_rotate_coordinate (x,y,z,1,rot1,0);
  z_plot = plot_rotate_coordinate (x,y,z,2,rot1,0);
  x = x_plot; y = y_plot; z = z_plot;
  x_plot = plot_rotate_coordinate (x,y,z,0,rot2,1);
  y_plot = plot_rotate_coordinate (x,y,z,1,rot2,1);
  z_plot = plot_rotate_coordinate (x,y,z,2,rot2,1);

  plot_line_to (cr, x_plot, y_plot, z_plot);
}

void plot_overview (GtkWidget *box){
  g_print("plots_overview\n");
  da_overview = gtk_drawing_area_new ();
  gtk_widget_set_size_request (da_overview, 200, 100);
  gtk_widget_set_hexpand (da_overview, TRUE);
  gtk_widget_set_vexpand (da_overview, TRUE);

  gtk_container_add (GTK_CONTAINER (box), da_overview);
  gtk_widget_show_all (box);

  g_signal_connect (da_overview, "draw", G_CALLBACK (on_draw_overview), NULL);
}

void plot_overview_gl (GtkWidget *box){
  g_print("plot_overview_gl\n");
  da_overview = gtk_gl_area_new ();
  gtk_widget_set_size_request (da_overview, 200, 100);
  gtk_widget_set_hexpand (da_overview, TRUE);
  gtk_widget_set_vexpand (da_overview, TRUE);

  gtk_container_add (GTK_CONTAINER (box), da_overview);
  gtk_widget_show_all (box);

  g_signal_connect (da_overview, "draw", G_CALLBACK (on_draw_overview_gl), NULL);
}

void course_sun_overview (float *array_az_sun, float *array_el_sun){
  for (int i=0; i<288; i++){
    array_sun_x[i] = cos(M_PI/180*array_az_sun[i+2])*cos(M_PI/180*array_el_sun[i+2]);
    array_sun_y[i] = sin(M_PI/180*array_az_sun[i+2])*cos(M_PI/180*array_el_sun[i+2]);
    array_sun_z[i] = -sin(M_PI/180*array_el_sun[i+2]);
  }
}

void course_moon_overview (float *array_az_moon, float *array_el_moon){
  for (int i=0; i<288; i++){
    array_moon_x[i] = cos(M_PI/180*array_az_moon[i+2])*cos(M_PI/180*array_el_moon[i+2]);
    array_moon_y[i] = sin(M_PI/180*array_az_moon[i+2])*cos(M_PI/180*array_el_moon[i+2]);
    array_moon_z[i] = -sin(M_PI/180*array_el_moon[i+2]);
  }
}

void course_mw_overview (float *array_az_mw, float *array_el_mw){
  for (int i=0; i<288; i++){
    array_mw_x[i] = cos(M_PI/180*array_az_mw[i+2])*cos(M_PI/180*array_el_mw[i+2]);
    array_mw_y[i] = sin(M_PI/180*array_az_mw[i+2])*cos(M_PI/180*array_el_mw[i+2]);
    array_mw_z[i] = -sin(M_PI/180*array_el_mw[i+2]);
  }
}

void update_overview (float sun_az, float sun_el, float moon_az, float moon_el, float mw_az, float mw_el){

  sun_x = cos(M_PI/180*sun_az)*cos(M_PI/180*sun_el);
  sun_y = sin(M_PI/180*sun_az)*cos(M_PI/180*sun_el);
  sun_z = -sin(M_PI/180*sun_el);

  moon_x = cos(M_PI/180*moon_az)*cos(M_PI/180*moon_el);
  moon_y = sin(M_PI/180*moon_az)*cos(M_PI/180*moon_el);
  moon_z = -sin(M_PI/180*moon_el);

  mw_x = cos(M_PI/180*mw_az)*cos(M_PI/180*mw_el);
  mw_y = sin(M_PI/180*mw_az)*cos(M_PI/180*mw_el);
  mw_z = -sin(M_PI/180*mw_el);

  gtk_widget_queue_draw(da_overview);
}

gboolean on_draw_overview (GtkWidget *widget, cairo_t *cr, gpointer gpoint){

  GdkWindow *drawing_window;
  cairo_region_t *cairo_region;
  GdkDrawingContext *drawing_context;
  GdkRectangle da;
  gdouble dx, dy;
  gdouble clip_x1, clip_y1, clip_x2, clip_y2;

  drawing_window = gtk_widget_get_window (widget);
  cairo_region = cairo_region_create();
  drawing_context = gdk_window_begin_draw_frame (drawing_window,cairo_region);
  cr = gdk_drawing_context_get_cairo_context (drawing_context);

  gdk_window_get_geometry (drawing_window, &da.x, &da.y, &da.width, &da.height);
  cairo_translate (cr, da.width*0.5, da.height*0.5);
  cairo_scale (cr, 100, 100);
  dx = da.width, dy = da.height;
  clip_x1 = 0.0, clip_y1 = 0.0, clip_x2 = 0.0, clip_y2 = 0.0;
  cairo_device_to_user_distance (cr, &dx, &dy);
  cairo_clip_extents (cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);

  plot_set_basic_coordinate_system_settings();

  cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
  cairo_paint (cr);

  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_set_line_width (cr, 0.02);
  plot_move_to (cr, 0, 0, 0);
  plot_circle (cr, 1, 0, 0, 50);
  cairo_fill (cr);
  cairo_stroke (cr);

  cairo_set_line_width (cr, 0.05);

  if (sun_z<0){
    cairo_set_source_rgb (cr, 1, 0, 0);
    plot_move_to (cr, 0, 0, 0);
    plot_vector_to (cr, sun_x, sun_y, sun_z);
    cairo_stroke (cr);
  }

  if (moon_z<0){
    cairo_set_source_rgb (cr, 0, 1, 0);
    plot_move_to (cr, 0, 0, 0);
    plot_vector_to (cr, moon_x, moon_y, moon_z);
    cairo_stroke (cr);
  }
  if (mw_z<0){
    cairo_set_source_rgb (cr, 0, 0, 1);
    plot_move_to (cr, 0, 0, 0);
    plot_vector_to (cr, mw_x, mw_y, mw_z);
    cairo_stroke (cr);
  }

  cairo_set_source_rgb (cr, 1, 0.9, 0);
  plot_move_to (cr, array_sun_x[0], array_sun_y[0], array_sun_z[0]);
  for (int i=1; i<288; i++){
    if (array_sun_z[i]<0){
      plot_line_to (cr, array_sun_x[i], array_sun_y[i], array_sun_z[i]);
    }
    else{
      plot_move_to (cr, array_sun_x[i], array_sun_y[i], array_sun_z[i]);

    }
  }
  cairo_stroke (cr);

  cairo_set_source_rgb (cr, 0.1, 0.9, 1);
  plot_move_to (cr, array_moon_x[0], array_moon_y[0], array_moon_z[0]);
  for (int i=0; i<288; i++){
    if (array_moon_z[i]<0){
      plot_line_to (cr, array_moon_x[i], array_moon_y[i], array_moon_z[i]);
    }
    else{
      plot_move_to (cr, array_moon_x[i], array_moon_y[i], array_moon_z[i]);
    }
  }
  cairo_stroke (cr);

  cairo_set_source_rgb (cr, 0.9, 0.9, 0.9);
  plot_move_to (cr, array_mw_x[0], array_mw_y[0], array_mw_z[0]);
  for (int i=0; i<288; i++){
    if (array_mw_z[i]<0){
      plot_line_to (cr, array_mw_x[i], array_mw_y[i], array_mw_z[i]);
    }
    else{
      plot_move_to (cr, array_mw_x[i], array_mw_y[i], array_mw_z[i]);
    }
  }
  cairo_stroke (cr);

  cairo_surface_t *cairo_surface = cairo_get_target (cr);
  unsigned char *data = cairo_image_surface_get_data(cairo_surface);


  gdk_window_end_draw_frame(drawing_window,drawing_context);
  cairo_region_destroy(cairo_region);

  return FALSE;
}

gboolean on_draw_overview_gl (GtkWidget *widget, cairo_t *cr, gpointer gpoint){

  GdkWindow *drawing_window;

  drawing_window = gtk_widget_get_window (widget);


  // 1. Plot circle

  if (sun_z<0){
    cairo_set_source_rgb (cr, 1, 0, 0);
    plot_move_to (cr, 0, 0, 0);
    plot_vector_to (cr, sun_x, sun_y, sun_z);
    cairo_stroke (cr);
  }

  if (moon_z<0){
    cairo_set_source_rgb (cr, 0, 1, 0);
    plot_move_to (cr, 0, 0, 0);
    plot_vector_to (cr, moon_x, moon_y, moon_z);
    cairo_stroke (cr);
  }
  if (mw_z<0){
    cairo_set_source_rgb (cr, 0, 0, 1);
    plot_move_to (cr, 0, 0, 0);
    plot_vector_to (cr, mw_x, mw_y, mw_z);
    cairo_stroke (cr);
  }

  cairo_set_source_rgb (cr, 1, 0.9, 0);
  plot_move_to (cr, array_sun_x[0], array_sun_y[0], array_sun_z[0]);
  for (int i=1; i<288; i++){
    if (array_sun_z[i]<0){
      plot_line_to (cr, array_sun_x[i], array_sun_y[i], array_sun_z[i]);
    }
    else{
      plot_move_to (cr, array_sun_x[i], array_sun_y[i], array_sun_z[i]);

    }
  }
  cairo_stroke (cr);

  cairo_set_source_rgb (cr, 0.1, 0.9, 1);
  plot_move_to (cr, array_moon_x[0], array_moon_y[0], array_moon_z[0]);
  for (int i=0; i<288; i++){
    if (array_moon_z[i]<0){
      plot_line_to (cr, array_moon_x[i], array_moon_y[i], array_moon_z[i]);
    }
    else{
      plot_move_to (cr, array_moon_x[i], array_moon_y[i], array_moon_z[i]);
    }
  }
  cairo_stroke (cr);

  cairo_set_source_rgb (cr, 0.9, 0.9, 0.9);
  plot_move_to (cr, array_mw_x[0], array_mw_y[0], array_mw_z[0]);
  for (int i=0; i<288; i++){
    if (array_mw_z[i]<0){
      plot_line_to (cr, array_mw_x[i], array_mw_y[i], array_mw_z[i]);
    }
    else{
      plot_move_to (cr, array_mw_x[i], array_mw_y[i], array_mw_z[i]);
    }
  }
  cairo_stroke (cr);

  cairo_surface_t *cairo_surface = cairo_get_target (cr);
  unsigned char *data = cairo_image_surface_get_data(cairo_surface);


  gdk_window_end_draw_frame(drawing_window,drawing_context);
  cairo_region_destroy(cairo_region);

  return FALSE;
}

void plot_elevation_mw (float *elevations, GtkWidget *box){
  g_print("plots_mw_elevation\n");
  GtkWidget *da; /* Drawing Area */

  da = gtk_drawing_area_new ();
  gtk_widget_set_size_request (da, 200, 100);
  gtk_widget_set_hexpand (da, TRUE);
  gtk_widget_set_vexpand (da, TRUE);

  gtk_container_add (GTK_CONTAINER (box), da);
  gtk_widget_show_all (box);

  static float stat_elevation[290];
  for (int i = 0; i<290; i++){
    stat_elevation[i]=elevations[i];
  }
  /* I found this by trail and error... Why do I need a static array? A non static arry is not able to pass data through the g_signal_connect function...? */

  g_signal_connect (da, "draw", G_CALLBACK (on_draw_elevation_mw), stat_elevation);
}

gboolean on_draw_elevation_mw (GtkWidget *widget, cairo_t *cr, gpointer gpoint){
  float *elevation = gpoint;

  GdkWindow *drawing_window;
  cairo_region_t *cairo_region;
  GdkDrawingContext *drawing_context;
  GdkRectangle da;

  drawing_window = gtk_widget_get_window (widget);

  cairo_region = cairo_region_create();
  drawing_context = gdk_window_begin_draw_frame (drawing_window,cairo_region);
  cr = gdk_drawing_context_get_cairo_context (drawing_context);

  gdk_window_get_geometry (drawing_window, &da.x, &da.y, &da.width, &da.height);
  cairo_translate (cr, 0, da.height*0.4);
  cairo_scale (cr, 100, 100);
  gdouble dx = da.width, dy = da.height*0.4;
  gdouble clip_x1 = 0.0, clip_y1 = 0.0, clip_x2 = 0.0, clip_y2 = 0.0;
  cairo_device_to_user_distance (cr, &dx, &dy);
  cairo_clip_extents (cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
  cairo_set_line_width (cr, 0.05);

  cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
  cairo_paint (cr);
  cairo_move_to (cr, 0, 0);
  cairo_line_to (cr, dx, 0);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_stroke (cr);

  float extr = 0;
  if (fabs(*elevation)>*(elevation+1)){
    extr=fabs(*elevation);
  }
  else{
    extr = fabs(*(elevation+1));
  }

  for (int i = 0; i<25; i++){
    float x = (float)i/24.*dx*0.9+0.05*dx;

    cairo_move_to (cr, x, -dy);
    cairo_set_line_width (cr, 0.02);
    cairo_set_source_rgb (cr, 0.95, 0.95, 0.95);
    cairo_line_to (cr, x, dy);

    if (i%2==0){
      cairo_move_to (cr, x-0.1, dy*1.4);
      static char label[3];
      sprintf (label, "%02d", i);
      cairo_set_font_size (cr, 0.15);
      cairo_select_font_face (cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
      cairo_show_text (cr, label);
    }
    cairo_stroke (cr);
  }

  cairo_set_line_width (cr, 0.05);
  cairo_move_to (cr, 0, 0);
  cairo_set_source_rgb (cr, 0, 0, 0);
  for (int i = 0; i<288; i++){
    float x = (float)i/288.*dx*0.9+0.05*dx;
    float y = -*(elevation+i+2)/extr*dy*0.95;
    /*g_print("Elevation: %f\n", elevation[i+2]);*/
    if (i==0){
      cairo_move_to (cr, x, y);
    }
    else{
      cairo_line_to (cr, x, y);
    }

  }

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_stroke (cr);

  gdk_window_end_draw_frame(drawing_window,drawing_context);
  cairo_region_destroy(cairo_region);

  return FALSE;
}

void plot_elevation_sun (float *elevations, GtkWidget *box){
  g_print("plots_sun_elevation\n");
  GtkWidget *da; /* Drawing Area */

  da = gtk_drawing_area_new ();
  gtk_widget_set_size_request (GTK_WIDGET(da), 200, 100);
  gtk_widget_set_hexpand (GTK_WIDGET (da), TRUE);
  gtk_widget_set_vexpand (GTK_WIDGET (da), TRUE);

  gtk_container_add (GTK_CONTAINER (box), da);
  gtk_widget_show_all (box);

  /*g_print("Elevation 1: %f\n",*(elevations+2));*/

  static float stat_elevation[290];
  for (int i = 0; i<290; i++){
    stat_elevation[i]=elevations[i];
  }
  /* I found this by trail and error... Why do I need a static array? A non static arry is not able to pass data through the g_signal_connect function...? */
  /*g_print("Elevation 1.1: %f\n",stat_elevation[2]);*/

  g_signal_connect (da, "draw", G_CALLBACK (on_draw_elevation_sun), stat_elevation);
}

gboolean on_draw_elevation_sun (GtkWidget *widget, cairo_t *cr, gpointer gpoint){
  float *elevation = gpoint;
  /*g_print("on_draw_elevation_sun\n");*/

  GdkWindow *drawing_window;
  cairo_region_t *cairo_region;
  GdkDrawingContext *drawing_context;
  GdkRectangle da;

  drawing_window = gtk_widget_get_window (widget);

  cairo_region = cairo_region_create();
  drawing_context = gdk_window_begin_draw_frame (drawing_window,cairo_region);
  cr = gdk_drawing_context_get_cairo_context (drawing_context);

  gdk_window_get_geometry (drawing_window, &da.x, &da.y, &da.width, &da.height);
  cairo_translate (cr, 0, da.height*0.4);
  cairo_scale (cr, 100, 100);
  gdouble dx = da.width, dy = da.height*0.4;
  gdouble clip_x1 = 0.0, clip_y1 = 0.0, clip_x2 = 0.0, clip_y2 = 0.0;
  cairo_device_to_user_distance (cr, &dx, &dy);
  cairo_clip_extents (cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
  cairo_set_line_width (cr, 0.05);

  cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
  cairo_paint (cr);
  cairo_move_to (cr, 0, 0);
  cairo_line_to (cr, dx, 0);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_stroke (cr);

  float extr = 0;
  if (fabs(*elevation)>*(elevation+1)){
    extr=fabs(*elevation);
  }
  else{
    extr = *(elevation+1);
  }

  for (int i = 0; i<25; i++){
    float x = (float)i/24.*dx*0.9+0.05*dx;

    cairo_move_to (cr, x, -dy);
    cairo_set_line_width (cr, 0.02);
    cairo_set_source_rgb (cr, 0.95, 0.95, 0.95);
    cairo_line_to (cr, x, dy);

    if (i%2==0){
    cairo_move_to (cr, x-0.1, dy*1.4);
    static char label[3];
    sprintf (label, "%02d", i);
    cairo_set_font_size (cr, 0.15);
    cairo_select_font_face (cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
    cairo_show_text (cr, label);
    }
    cairo_stroke (cr);
  }

  cairo_set_line_width (cr, 0.05);
  cairo_move_to (cr, 0, 0);
  cairo_set_source_rgb (cr, 0, 0, 0);
  for (int i = 0; i<288; i++){
    float x = (float)i/288.*dx*0.9+0.05*dx;
    float y = -*(elevation+i+2)/extr*dy*0.95;
    /*g_print("Elevation: %f\n", elevation[i+2]);*/
    if (i==0){
      cairo_move_to (cr, x, y);
    }
    else{
      cairo_line_to (cr, x, y);
    }
    /*
    int hour=(int)(i*5/60);
    int min=i*5-(int)(i*5/60)*60;
    if (elevation[i+2]==elevation[1]){
      cairo_stroke (cr);

      static char label[30];
      sprintf (label, "%02d:%02d @ %.1f \u00B0", hour,min,elevation[i+2]);
      cairo_move_to (cr, x-0.7, y-0.08);

      cairo_set_font_size (cr, 0.2);
      cairo_select_font_face (cr, "Georgia", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
      cairo_show_text (cr, label);
      cairo_move_to (cr, x, y);
    } */

  }
  /*cairo_line_to (cr, 160./288.*dx, -35./60.*dy);*/

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_stroke (cr);

  gdk_window_end_draw_frame(drawing_window,drawing_context);
  cairo_region_destroy(cairo_region);

  return FALSE;
}

void plot_elevation_moon (float *elevations, GtkWidget *box){
  GtkWidget *da;

  da = gtk_drawing_area_new ();
  gtk_widget_set_size_request (GTK_WIDGET(da), 200, 100);
  gtk_widget_set_hexpand (GTK_WIDGET (da), TRUE);
  gtk_widget_set_vexpand (GTK_WIDGET (da), TRUE);

  gtk_container_add (GTK_CONTAINER (box), da);
  gtk_widget_show_all (box);

  /*g_print("Elevation 1: %f\n",*(elevations+2));*/

  static float stat_elevation[290];
  for (int i = 0; i<290; i++){
    stat_elevation[i]=elevations[i];
  }
  /* I found this by trail and error... Why do I need a static array? A non static arry is not able to pass data through the g_signal_connect function...? */
  /*g_print("Elevation 1.1: %f\n",stat_elevation[2]);*/

  g_signal_connect (da, "draw", G_CALLBACK (on_draw_elevation_moon), stat_elevation);
}

gboolean on_draw_elevation_moon (GtkWidget *widget, cairo_t *cr, gpointer gpoint){
  float *elevation = gpoint;

  GdkWindow *drawing_window;
  cairo_region_t *cairo_region;
  GdkDrawingContext *drawing_context;
  GdkRectangle da;

  drawing_window = gtk_widget_get_window (widget);

  cairo_region = cairo_region_create();
  drawing_context = gdk_window_begin_draw_frame (drawing_window,cairo_region);
  cr = gdk_drawing_context_get_cairo_context (drawing_context);

  gdk_window_get_geometry (drawing_window, &da.x, &da.y, &da.width, &da.height);
  cairo_translate (cr, 0, da.height*0.4);
  cairo_scale (cr, 100, 100);
  gdouble dx = da.width, dy = da.height*0.4;
  gdouble clip_x1 = 0.0, clip_y1 = 0.0, clip_x2 = 0.0, clip_y2 = 0.0;
  cairo_device_to_user_distance (cr, &dx, &dy);
  cairo_clip_extents (cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
  cairo_set_line_width (cr, 0.05);

  cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
  cairo_paint (cr);
  cairo_move_to (cr, 0, 0);
  cairo_line_to (cr, dx, 0);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_stroke (cr);

  float extr = 0;
  if (fabs(*elevation)>*(elevation+1)){
    extr=fabs(*elevation);
  }
  else{
    extr = *(elevation+1);
  }

  for (int i = 0; i<25; i++){
    float x = (float)i/24.*dx*0.9+0.05*dx;

    cairo_move_to (cr, x, -dy);
    cairo_set_line_width (cr, 0.02);
    cairo_set_source_rgb (cr, 0.95, 0.95, 0.95);
    cairo_line_to (cr, x, dy);

    if (i%2==0){
    cairo_move_to (cr, x-0.1, dy*1.4);
    static char label[3];
    sprintf (label, "%02d", i);
    cairo_set_font_size (cr, 0.15);
    cairo_select_font_face (cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
    cairo_show_text (cr, label);
    }
    cairo_stroke (cr);
  }

  cairo_set_line_width (cr, 0.05);
  cairo_move_to (cr, 0, 0);
  cairo_set_source_rgb (cr, 0, 0, 0);
  for (int i = 0; i<288; i++){
    float x = (float)i/288.*dx*0.9+0.05*dx;
    float y = -*(elevation+i+2)/extr*dy*0.95;
    /*g_print("Elevation: %f\n", elevation[i+2]);*/
    if (i==0){
      cairo_move_to (cr, x, y);
    }
    else{
      cairo_line_to (cr, x, y);
    }
  }

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_stroke (cr);

  gdk_window_end_draw_frame(drawing_window,drawing_context);
  cairo_region_destroy(cairo_region);

  return FALSE;
}
