#include <math.h>
#include "SampleGraph.h"
#include "OneButton.h"
#include "OneSample.h"
#include "ButtonEditor.h"
#include "ButtonDifference.h"
#include "RawIR.h"
#include <QPainter>
#include <QBrush>
#include <QColor>

SampleGraph::SampleGraph(
   ButtonEditor * init_editor,
   unsigned int init_index,
   QWidget * parent)
 : QWidget(parent),
   editor(init_editor),
   index(init_index) {

   QSizePolicy pol = sizePolicy();
   pol.setHorizontalPolicy(QSizePolicy::Ignored);
   pol.setVerticalPolicy(QSizePolicy::Ignored);
   setSizePolicy(pol);
}

SampleGraph::~SampleGraph() {
}

void
SampleGraph::paintEvent(QPaintEvent * /* event */) {
   if (editor->DrawDiffGraph()) {
      PaintDiffs();
   } else {
      PaintSamples();
   }
}

void
SampleGraph::PaintSamples() {
   OneButton * button = editor->CurrentButton();
   double logical_width;
   logical_width = (double)button->MaxSampleTime();
   double pixel_width = (double)width();
   int pixel_height = height();
   OneSample * sample = (*button)[index];
   int cur_x = 0;
   unsigned int sample_count = sample->Length();
   QPainter p(this);
   p.eraseRect(0,0,width(),height());
   QBrush blue_brush(QColor("blue"));
   QBrush green_brush(QColor("green"));
   int half_height = (pixel_height - 2)/2;
   int blue_y = 1;
   int green_y = 1 + half_height;
   double tot_logical_blip = 0.0;
   for (unsigned int s = 0; s < sample_count; ++s) {
      unsigned short data = (*sample)[s];
      double logical_blip = (double)(BGIR_DURATION(data));
      tot_logical_blip += logical_blip;
      int tot_blip_pixels = (int)
         (((tot_logical_blip / logical_width) * pixel_width) + 0.5);
      int blip_pixels = (int)
         (((logical_blip / logical_width) * pixel_width) + 0.5);
      if ((cur_x + blip_pixels) > tot_blip_pixels) {
         --blip_pixels;
      } else if ((cur_x + blip_pixels) < tot_blip_pixels) {
         ++blip_pixels;
      }
      if (BGIR_IS_MARK(data)) {
         p.fillRect(cur_x, blue_y, blip_pixels, half_height, blue_brush);
      } else {
         p.fillRect(cur_x, green_y, blip_pixels, half_height, green_brush);
      }
      cur_x += blip_pixels;
   }
}

void
SampleGraph::PaintDiffs() {
   OneButton * button = editor->CurrentButton();
   unsigned int max_height = button->NumSamples() - 1;
   ButtonDifference * diff = button->GetButtonDifference();
   double logical_width;
   logical_width = (double)diff->MaxDiffTime();
   double pixel_width = (double)width();
   SampleDifference * sample = (*diff)[index];
   int cur_x = 0;
   unsigned int sample_count = sample->Length();
   QPainter p(this);
   p.eraseRect(0,0,width(),height());
   QBrush red_brush(QColor("red"));
   double tot_logical_blip = 0.0;
   double height_scale = ((double)(height() - 2))/(double)(max_height);
   int red_y = height() - 1;
   for (unsigned int s = 0; s < sample_count; ++s) {
      unsigned short data = (*sample)[s].length;
      unsigned int logical_height = (*sample)[s].count;
      int pixel_height = (int)
         ((((double)logical_height) * height_scale) + 0.5);
      double logical_blip = (double)data;
      tot_logical_blip += logical_blip;
      int tot_blip_pixels = (int)
         (((tot_logical_blip / logical_width) * pixel_width) + 0.5);
      int blip_pixels = (int)
         (((logical_blip / logical_width) * pixel_width) + 0.5);
      if ((cur_x + blip_pixels) > tot_blip_pixels) {
         --blip_pixels;
      } else if ((cur_x + blip_pixels) < tot_blip_pixels) {
         ++blip_pixels;
      }
      p.fillRect(cur_x, red_y - pixel_height, blip_pixels, pixel_height,
                 red_brush);
      cur_x += blip_pixels;
   }
}
