/* LICENSE:
  =========================================================================
    CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
    Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
    School of Computer Science, Carnegie Mellon University
    All rights reserved.
  ========================================================================= */

#include <FL/Fl_Double_Window.H>

#include "../DataInterface.h"
#include "RLEViewer.h"

//====================================================================

extern sem_t GuiDataSemaphore;
extern DataInterface *DataControl;

static const ulong color_vals[]={
  0x00000000, // 10 black 
  0x00ff8000, // 11 orange 
  0x00008000, // 12 green 
  0x00ff0080, // 13 pink 
  0x0000ffff, // 14 cyan 
  0x00ffff00, // 15 yellow 
  0x000000ff, // 16 blue 
  0x00ff0000, // 17 red 
  0x00ffffff, // 18 white 
  0x00808080 // 19 gray 
};

//====================================================================

RLEViewer::RLEViewer(int width,int height) :
  Fl_Double_Window(0,0,width,height,"RLE")
{
  resizable(this);
}

RLEViewer::~RLEViewer()
{
}

//=========================================================================
void RLEViewer::resize(int x,int y,int w, int h)
{
  Fl_Double_Window::resize(x,y,w,h);
}

//=========================================================================
void RLEViewer::draw()
{
  uchar * rgb_image = NULL;

  int robot_idx=0;

  int pix_width=1;
  int pix_height=1;

  int pix_offset_x=0;
  int pix_offset_y=0;

  sem_wait(&GuiDataSemaphore);

  // Find the active robot_id and draw it
  while((!DataControl->allRobotViewData->robotsActive[robot_idx]) &&
        (robot_idx < RVDMaxRobots)){
    robot_idx++;
  }

  if(DataControl->allRobotViewData->robotsActive[robot_idx]){
    rgb_image = DataControl->allRobotViewData->robots[robot_idx].rleImg;

    fl_color(128,128,128);
    fl_rectf(0,0,w(),h());

    int rle_width  = DataControl->allRobotViewData->robots[robot_idx].rleImageXSize;
    int rle_height = DataControl->allRobotViewData->robots[robot_idx].rleImageYSize;

    if(rle_width!=0 && rle_height!=0){
      // Compute the stretch in the x
      if(w() > rle_width) {
        pix_width = w() / rle_width;
        if(pix_width < 1) { pix_width=1; }
        pix_offset_x = (w() - pix_width*rle_width)/2;
      }

      // Compute the stretch in the y
      if(h() > rle_height) {
        pix_height = h() / rle_height;
        if(pix_height < 1) { pix_height=1; }
        pix_offset_y = (h() - pix_height*rle_height)/2;
      }

      // Each of the pixels in this image have to be set manually
      // as they are just color IDs
      for(int y=0;y<rle_height;y++) {
        for(int x=0;x<rle_width;x++) {
          int rgb_idx = rgb_image[y*rle_width+x];
          if(0 <= rgb_idx && rgb_idx < 10) {
            ulong c = color_vals[rgb_idx];
            uchar r = 0xff&(c>>16);
            uchar g = 0xff&(c>>8);
            uchar b = 0xff&(c);

            fl_color(r,g,b);
            fl_rectf(pix_offset_x + x*pix_width,
                     pix_offset_y + y*pix_height,
                     pix_width,
                     pix_height);
          }
        }
      }
    }
  }
  sem_post(&GuiDataSemaphore);
}

//=========================================================================
int RLEViewer::handle(int event)
{
  switch(event){
    case FL_KEYDOWN:
      {
        int key = Fl::event_key();
        switch(key){
        }
      }
      return 0;
      break;
    case FL_PUSH:
      {
        int xs,ys;

        xs = Fl::event_x();
        ys = Fl::event_y();
      }
      return 0;
      break;
  }

  return 0;
}

