#include <gtk/gtk.h>
#include <iostream>

#include "Naptar.h"
#include "evfordulo.h"

Naptar np;

void markDay(GtkCalendar * calendar){
  gtk_calendar_clear_marks (calendar);
  int ev,honap;
  gtk_calendar_get_date(calendar,(guint*)&ev,(guint*)&honap,NULL);
  for(int x=1; x < 32;++x){
    if(np.foglalt(ev, honap+1, x))
      gtk_calendar_mark_day(calendar, x);
  }
}

void daySelected(GtkCalendar * calendar,GtkLabel* label){
  int ev,honap,nap;
  gtk_calendar_get_date(calendar,(guint*)&ev,(guint*)&honap,(guint*)&nap);
  Datum day(ev,honap+1,nap);
  if(np.foglalt(day))
    for(Naptar::iterator it = np.begin();it != np.end();++it){

      if(*it == day)
	gtk_label_set_text(label,it->getMegjegyzes());
    }
  else
    gtk_label_set_text(label,"Erre a napra nincs bejegyzés!");
}

void popupAbout(){
  GtkWidget* pa = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size((GtkWindow*)pa,200,50);
  gtk_window_set_title((GtkWindow*) pa, "About");
  gtk_window_set_resizable((GtkWindow*) pa, false);
  gtk_window_set_default_icon_name("x-office-calendar");
  gtk_window_set_position((GtkWindow*)pa, GTK_WIN_POS_CENTER);
  gtk_container_set_border_width (GTK_CONTAINER (pa), 10);
  GtkWidget* text = gtk_label_new("Keszitette: Toldi Bala'zs");

  gtk_container_add(GTK_CONTAINER(pa), text);
  gtk_widget_show(text);
  g_signal_connect(pa, "destroy", G_CALLBACK(gtk_widget_destroy),pa);
  gtk_widget_show(pa);
}

void addToCalendar(GtkWidget* button,GtkBuilder * builder){
  GtkCalendar* cal = (GtkCalendar*) gtk_builder_get_object(builder, "cal");
  int ev,honap,nap;
  gtk_calendar_get_date(cal,(guint*)&ev,(guint*)&honap,(guint*)&nap);
    
  if(!np.foglalt(ev,honap+1,nap)){
    GtkEntry* entry = (GtkEntry*) gtk_builder_get_object(builder, "note");
    GtkEntryBuffer* buf = gtk_entry_get_buffer(entry);
    np.hozzaad(new Datum(ev, honap+1, nap,gtk_entry_buffer_get_text(buf)));
  }else{
    GtkWidget* pa = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size((GtkWindow*)pa,200,50);
    gtk_window_set_title((GtkWindow*) pa, "Hiba");
    gtk_window_set_resizable((GtkWindow*) pa, false);
    gtk_window_set_default_icon_name("x-office-calendar");
    gtk_window_set_position((GtkWindow*)pa, GTK_WIN_POS_CENTER);
    gtk_container_set_border_width (GTK_CONTAINER (pa), 10);
    GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL,2);
    gtk_widget_show(box);
    GtkWidget* text = gtk_label_new("Hiba: A kiválasztott nap már foglalt!");
    GtkWidget* button = gtk_button_new_with_label("OK");
  
    gtk_container_add(GTK_CONTAINER(box), text);
    gtk_container_add(GTK_CONTAINER(box), button);
   
    gtk_widget_show(button);
    gtk_widget_show(text);
   
    gtk_container_add(GTK_CONTAINER(pa), box);

    g_signal_connect (button, "clicked", G_CALLBACK(gtk_window_close),(GtkWindow*) pa);
    g_signal_connect(pa, "destroy", G_CALLBACK(gtk_widget_destroy),pa);
    gtk_widget_show(pa);
  }
}

void addDate(){
  GtkBuilder *builder;
  GObject *window;
  GError *error = NULL;
  GtkWidget* button;
    
  gtk_init (NULL, NULL);
    
  builder = gtk_builder_new ();
  if (gtk_builder_add_from_file (builder, "adder.ui", &error) == 0)
    {
      g_printerr ("Error loading file: %s\n", error->message);
      g_clear_error (&error);
      return;
    }
    
    
  window = gtk_builder_get_object (builder, "window");
  g_signal_connect (window, "destroy",G_CALLBACK(gtk_widget_destroy),window);
    
  button = (GtkWidget*)gtk_builder_get_object (builder, "cancel");
  g_signal_connect (button, "clicked", G_CALLBACK(gtk_window_close),(GtkWindow*)window);

  button = (GtkWidget*)gtk_builder_get_object (builder, "apply");
    
  g_signal_connect (button, "clicked", G_CALLBACK(addToCalendar),builder);
  g_signal_connect (button, "clicked", G_CALLBACK(gtk_window_close),(GtkWindow*) window);

  gtk_widget_show((GtkWidget*) window);
}

int main ()
{
    
  GtkBuilder *builder;
  GObject *window;
  GtkCalendar *c;
  GtkMenuItem* menuButton;
  GError *error = NULL;
  GtkLabel* label;
  gtk_init (NULL, NULL);
    
  builder = gtk_builder_new ();
  if (gtk_builder_add_from_file (builder, "builder.ui", &error) == 0)
    {
      g_printerr ("Error loading file: %s\n", error->message);
      g_clear_error (&error);
      return 1;
    }
        
    
  window = gtk_builder_get_object (builder, "window");
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  menuButton = (GtkMenuItem*) gtk_builder_get_object (builder, "quit");
  g_signal_connect (menuButton, "activate", G_CALLBACK (gtk_main_quit), NULL);

  menuButton = (GtkMenuItem*) gtk_builder_get_object (builder, "about");
  g_signal_connect (menuButton, "activate", G_CALLBACK (popupAbout), NULL);

  menuButton = (GtkMenuItem*) gtk_builder_get_object (builder, "add-date");
  g_signal_connect (menuButton, "activate", G_CALLBACK (addDate), NULL);
    
  c = (GtkCalendar*)gtk_builder_get_object (builder, "cal");
  np.hozzaad(new Datum(2020,5,19,"Prog2 NZH2"));
    
  np.hozzaad(new Evfordulo(2017,6,16,"Zsazs<3"));
  if(np.foglalt(Datum(2020,6,16)))
  
    g_signal_connect (c, "month-changed", G_CALLBACK (markDay), NULL);
  markDay(c);

  label = (GtkLabel*) gtk_builder_get_object(builder,"label");
  daySelected(c, label);
  g_signal_connect (c, "day-selected", G_CALLBACK (daySelected), label);
  gtk_main ();
  np.urit();
  return 0;
}
