#include <stdio.h>
#include "OneButton.h"
#include "OneSample.h"
#include "RemoteTable.h"
#include "ButtonEditor.h"
#include "ButtonDifference.h"
#include <QIcon>

OneButton::OneButton()
 : QTableWidgetItem(QString("")),
   button_edit(NULL),
   max_sample_time(0),
   log_max_sample_time(0.0),
   difference(NULL) {

   int flags = ((int)Qt::ItemIsSelectable) |
               ((int)Qt::ItemIsEditable) |
               ((int)Qt::ItemIsEnabled);
   setFlags((Qt::ItemFlags)flags);
}

OneButton::~OneButton() {
   DeleteSamples();
   delete difference;
}

unsigned int
OneButton::NumSamples() const {
   return sample.size();
}

OneSample *
OneButton::operator[](unsigned int ndx) const {
   if (ndx < sample.size()) {
      return sample[ndx];
   } else {
      return NULL;
   }
}

void
OneButton::AppendSample(OneSample * new_sample) {
   delete difference;
   difference = NULL;
   sample.push_back(new_sample);
   unsigned long new_time = new_sample->TotalTime();
   if (new_time > max_sample_time) {
      max_sample_time = new_time;
   }
   double new_log_time = new_sample->LogTotalTime();
   if (new_log_time > log_max_sample_time) {
      log_max_sample_time = new_log_time;
   }
   UpdateButtonInfo();
   if (button_edit != NULL) {
      button_edit->doNewSample();
   }
}

void
OneButton::RemoveSample(int nth) {
   delete sample.at(nth);
   sample.erase(sample.begin()+nth);
   UpdateButtonInfo();
}

void
OneButton::UpdateButtonInfo() {
   RemoteTable * table = dynamic_cast<RemoteTable *>(tableWidget());
   if (table != NULL) {
      table->SetChangingRow(true);
      int myrow = row();
      QTableWidgetItem * status = table->item(myrow, 1);
      if (status == NULL) {
         status = new QTableWidgetItem(QString(""));
         status->setFlags(Qt::ItemIsEnabled);
         table->setItem(myrow, 1, status);
      }
      QTableWidgetItem * info   = table->item(myrow, 2);
      if (info == NULL) {
         info = new QTableWidgetItem(QString(""));
         info->setFlags(Qt::ItemIsEnabled);
         table->setItem(myrow, 2, info);
      }
      unsigned int num_samples = NumSamples();
      if (num_samples == 0) {
         status->setText("No Samples");
         status->setIcon(QIcon(table->RedQuestion()));
         info->setText("");
      } else if (num_samples == 1) {
         status->setText("1 Sample");
         status->setIcon(QIcon(table->GreenQuestion()));
         info->setText(QString("Sample length: %1").arg(sample[0]->Length()));
      } else {
         status->setText(QString("%1 Samples").arg(num_samples));
         if (num_samples > 6) {
            status->setIcon(QIcon(table->GreenCheck()));
         } else {
            status->setIcon(QIcon(table->GreenQuestion()));
         }
         unsigned int min = sample[0]->Length();
         unsigned int max = min;
         for (unsigned int i = 1; i < num_samples; ++i) {
            unsigned slen = sample[i]->Length();
            if (slen < min) min = slen;
            if (slen > max) max = slen;
         }
         if (min == max) {
            info->setText(
               QString("All samples length: %1").arg(min));
         } else {
            info->setText(
               QString("Sample lengths: %1 thru %2").arg(min).arg(max));
         }
      }
      table->SetChangingRow(false);
   }
}

void
OneButton::SetEditor(ButtonEditor * new_edit) {
   button_edit = new_edit;
}

ButtonDifference *
OneButton::GetButtonDifference() {
   if (difference == NULL) {
      difference = new ButtonDifference(this);
   }
   return difference;
}

void
OneButton::CopyData() {
   unsigned int len = sample.size();
   for (unsigned int i = 0; i < len; ++i) {
      sample_copy.push_back(new OneSample(*sample[i]));
   }
}

void
OneButton::RestoreCopy() {
   DeleteSamples();
   sample.clear();
   unsigned int len = sample_copy.size();
   for (unsigned int i = 0; i < len; ++i) {
      sample.push_back(sample_copy[i]);
   }
   sample_copy.clear();
}

void
OneButton::DeleteSamples() {
   unsigned int len = sample.size();
   for (unsigned int i = 0; i < len; ++i) {
      delete sample[i];
   }
}
