#include "countdown.h"
#include <iostream>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>


Countdown::Countdown(QWidget* parent, int seconds)
    :QWidget(parent)
    ,seconds(seconds-1)
{
    QWidget * widget = new QWidget(this);
    widget->hide();
    widget->setFixedSize(100, 100);
    this->setFixedSize(100, 100);
    QHBoxLayout* layout = new QHBoxLayout(widget);
    timer = new QTimer(this);
    LCD = new QLCDNumber();
    LCD->setFixedSize(80,60);
    LCD->setStyleSheet(" background-color:rgb(60,60,60);color:rgb(0,150,0)");
    layout->addWidget(LCD,0,Qt::AlignRight);
    LCD->display(seconds);
    LCD->setDigitCount(2);
    QObject::connect(timer,  SIGNAL(timeout()),this,SLOT(setNumber()));

    //QObject::connect(this,  SIGNAL(stopped()),this,SLOT(close()));
    widget->show();
    timer->start(1000);
}

void Countdown::setNumber()
{

  if(seconds < 0)
  {
     timer->stop();
  }
  else
  {
      if(seconds==0)
      {
          emit stopped();
      }
      LCD->display(seconds--);
  }
}

void Countdown::stopClock()
{
    timer->stop();
}
