#include <QTimer>
#include <QStatusBar>
#include "Blinky.h"

Blinky::Blinky(QWidget * parent)
 : QWidget(parent),
   do_blink(false),
   new_blink_status(false),
   do_red(true),
   red_count(0),
   green_count(0) {

   ui.setupUi(this);
   ui.darkButton->raise();
   setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
   setMinimumSize(22,22);
   setMaximumSize(22,22);
   poll_worker = new QTimer(this);
   connect(poll_worker, SIGNAL(timeout()), this, SLOT(doPoll()));
   poll_worker->start(100); // every 1/10 sec
}

Blinky::~Blinky() {
   return;
}

void
Blinky::SetIncomingIR(bool getting_ir) {
   new_blink_status = getting_ir;
   red_count = 0;
   green_count = 0;
   if (new_blink_status) {
      do_blink = true;
   }
}

void
Blinky::doPoll() {
   if (do_blink) {
      if (do_red) {
         ui.redButton->raise();
         ++red_count;
      } else {
         ui.greenButton->raise();
         ++green_count;
      }
      do_red = ! do_red;
   }
   if (new_blink_status != do_blink) {
      if ((! new_blink_status) && (red_count > 3) && (green_count > 3)) {
         do_blink = false;
      }
   }
   if (! do_blink) {
      ui.darkButton->raise();
      red_count = 0;
      green_count = 0;
   }
}
