#include <gtkmm.h>
#include <iostream>
#include "memtrace.h"
#include "Naptar.h"
#include "evfordulo.h"

class AboutPage : public Gtk::Window {
  Gtk::Label text;
public:
  AboutPage() : Gtk::Window(),text("Készítette Toldi Balázs Ádám"){
    this->set_default_size(100, 100);
    this->set_title("About");
    this->add(text);
    this->show_all_children();
  }
  virtual ~AboutPage() {}
};

class MainWindow : public Gtk::Window {
  void markDay(){
    cal->clear_marks();
    int ev,honap,nap;
    cal->get_date((guint&)ev,(guint&)honap,(guint&)nap);
    for(int x=1; x < 32;++x){
      if(np.foglalt(ev, honap+1, x))
	cal->mark_day(x);
    }
  }
  void daySelected(){
    int ev,honap,nap;
    cal->get_date((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)
	  label->set_label(it->getMegjegyzes());
      }
    else
      label->set_label("Erre a napra nincs bejegyzés!");
  }
  void aboutWinClose(){
    aboutw = NULL;
  }
  void about(){
    if(aboutw != NULL)
        return;

    aboutw = new AboutPage;
    aboutw->signal_hide().connect(sigc::mem_fun(*this, &MainWindow::aboutWinClose));
    aboutw->show();
  }
public:
  MainWindow(BaseObjectType* b,const Glib::RefPtr<Gtk::Builder>& refGlade) : Gtk::Window(b), np(),builder(refGlade),aboutw(NULL){
    builder->get_widget("cal",cal);
    builder->get_widget("label",label);
    cal->signal_day_selected().connect(sigc::mem_fun(*this,&MainWindow::daySelected) );
    cal->signal_prev_month().connect(sigc::mem_fun(*this,&MainWindow::markDay) );
    cal->signal_next_month().connect(sigc::mem_fun(*this,&MainWindow::markDay) );
    cal->signal_prev_year().connect(sigc::mem_fun(*this,&MainWindow::markDay) );
    cal->signal_next_year().connect(sigc::mem_fun(*this,&MainWindow::markDay) );

    builder->get_widget("quit",mitem);
    mitem->signal_activate().connect(sigc::mem_fun(*this,&Gtk::Window::close));

    builder->get_widget("about",mitem);
    mitem->signal_activate().connect(sigc::mem_fun(*this,&MainWindow::about));
     
    daySelected();
    markDay();
  }
  virtual ~MainWindow() {
    delete cal;
    delete label;
  }
protected:
  Naptar np;
  Glib::RefPtr<Gtk::Builder> builder;
  Gtk::Calendar* cal;
  Gtk::Label* label;
  Gtk::MenuItem* mitem;
  AboutPage* aboutw;
};

int main(int argc, char *argv[])
{
  auto app =
    Gtk::Application::create(argc, argv,
			     "org.gtkmm.examples.base");

  
  
  std::cout << "Hali" << std::endl;
  auto builder = Gtk::Builder::create();
  try
    {
      builder->add_from_file("builder.ui");
    }
  catch(const Glib::FileError& ex)
    {
      std::cerr << "FileError: " << ex.what() << std::endl;
      return 1;
    }
  catch(const Glib::MarkupError& ex)
    {
      std::cerr << "MarkupError: " << ex.what() << std::endl;
      return 1;
    }
  catch(const Gtk::BuilderError& ex)
    {
      std::cerr << "BuilderError: " << ex.what() << std::endl;
      return 1;
    } 
  MainWindow* mw;
  builder->get_widget_derived("window",mw);
  app->run(*mw);
  delete mw;
  return 0;
}
