/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#include <iostream>
#include <boost/lexical_cast.hpp>
#include "boost/date_time/posix_time/posix_time.hpp" //include all types plus i/o

#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WDialog>
#include <Wt/WGridLayout>
#include <Wt/WVBoxLayout>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WTimer>

using namespace Wt;
using namespace boost::posix_time;
using namespace std;
bool ShowClock = true;

class TestApp : public WApplication {
public:
  TestApp( const WEnvironment& env ) :
    WApplication( env ) {
    
    WPushButton *b = new WPushButton("Click", root());
    b->clicked().connect(this, &TestApp::doit);

    currentTimeDisplay_ = new WText("-----", root() );
    timer_ = new WTimer();
    if (ShowClock) {
	    timer_->setInterval(1000);
	    timer_->timeout().connect(SLOT( this, TestApp::showCurrentTime) );
	    timer_->start();
    }
    
  }
void  showCurrentTime() {
	  ptime t(second_clock::universal_time() );
	  string time = to_simple_string(t);
	  currentTimeDisplay_->setText( time.c_str() );
  }

  void doit()
  {
    WDialog dialog;

    dialog.setWindowTitle( "Edit Processor Parameters" );
    WVBoxLayout  *contentsLayout = new WVBoxLayout();
    dialog.contents()->setLayout( contentsLayout );
    dialog.contents()->resize(WLength(), 100);

    WGridLayout *grid = new WGridLayout();
    contentsLayout->addLayout( grid, 1  /* ,Wt::AlignTop | Wt::AlignJustify */ );
    int row = 0;
    WText *label = new WText( "Name" );
    grid->addWidget( label, row, 0 );
    
    ++row;

    WPushButton *ok = new WPushButton( "OK" );
    WPushButton *cancel = new WPushButton( "Cancel" );
    grid->addWidget( ok, row, 0 );
    grid->addWidget( cancel, row, 1 );
    grid->addWidget( new WPushButton ("Push me!" ), row, 2);

    ok->clicked().connect( SLOT(&dialog, WDialog::accept) );
    cancel->clicked().connect( SLOT(&dialog, WDialog::reject) );

    if (dialog.exec() == WDialog::Accepted) {
      std::cerr << "Accepted" << std::endl;
    }
  }
private:
	WTimer *timer_;
        WText *currentTimeDisplay_;
};

WApplication *createApplication(const WEnvironment& env)
{
  WApplication *app = new TestApp(env);
  return app;
}

int main(int argc, char **argv)
{
  string opt = argv[1];
  if (opt == "--noclock") {
	ShowClock = false;
	++argv;--argc;
  } 
  return WRun(argc, argv, &createApplication);
}
