// Author: Juan Carlos De Abreu (jcabreur@gmail.com)

#include "button.h"

#include "window_management.h"
#include "../Includes/OpenGL.h"

BEGIN_TEG_NAMESPACE();
BEGIN_NAMESPACE(gui);

Button::Button(int x, int y, int width, int height, const std::tstring& caption) 
    : Control(x, y, width, height), caption_(caption) {}

Button::~Button() {}

// Method to render the button
void Button::Render() const {
  // Display the radio button if it's visible.
  if(!is_visible_) return;

  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, GUI_TEXTURE);

  const float kLeftX0 = static_cast<float>(x_);
  const float kLeftX1 = static_cast<float>(x_ + 6);
  const float kRightX0 = static_cast<float>(x_ + width_ - 6);
  const float kRightX1 = static_cast<float>(x_ + width_);
  const float kY0 = static_cast<float>(-y_);
  const float kY1 = static_cast<float>(-y_ - height_);

  if(is_pressed_) {	// si el boton esta presionado
    glBegin(GL_QUADS);
      // left side
      glTexCoord2f(0.445312f, 0.742187f);
      glVertex3f(kLeftX0, kY0, 0);
      glTexCoord2f(0.445312f, 0.546875f);
      glVertex3f(kLeftX0, kY1, 0);
      glTexCoord2f(0.484375f, 0.546875f);
      glVertex3f(kLeftX1, kY1, 0);
      glTexCoord2f(0.484375f, 0.742187f);
      glVertex3f(kLeftX1, kY0, 0);

      // middle
      glTexCoord2f(0.484375f, 0.742187f);
      glVertex3f(kLeftX1, kY0, 0);
      glTexCoord2f(0.484375f, 0.546875f);
      glVertex3f(kLeftX1, kY1, 0);
      glTexCoord2f(0.515625f, 0.546875f);
      glVertex3f(kRightX0, kY1, 0);
      glTexCoord2f(0.515625f, 0.742187f);
      glVertex3f(kRightX0, kY0, 0);

      // right side
      glTexCoord2f(0.515625f, 0.742187f);
      glVertex3f(kRightX0, kY0, 0);
      glTexCoord2f(0.515625f, 0.546875f);
      glVertex3f(kRightX0, kY1, 0);
      glTexCoord2f(0.554687f, 0.546875f);
      glVertex3f(kRightX1, kY1, 0);
      glTexCoord2f(0.554687f, 0.742187f);
      glVertex3f(kRightX1, kY0, 0);
    glEnd();

    // se pinta el contenido del boton
    //parent->wManagement->glWrite(x + 1 + (width-strlen(caption)*6) / 2, -y-21, caption);
    GL_WRITE(x_ + 1 + (width_ - caption_.size() * 6) / 2, y_ + 21, caption_.c_str());
  } else {	// si el boton no esta presionado
    glBegin(GL_QUADS);
      // left side
      glTexCoord2f(0.320312f, 0.742187f);
      glVertex2f(kLeftX0, kY0);
      glTexCoord2f(0.320312f, 0.546875f);
      glVertex2f(kLeftX0, kY1);
      glTexCoord2f(0.359375f, 0.546875f);
      glVertex2f(kLeftX1, kY1);
      glTexCoord2f(0.359375f, 0.742187f);
      glVertex2f(kLeftX1, kY0);

      // middle
      glTexCoord2f(0.359375f, 0.742187f);
      glVertex2f(kLeftX1, kY0);
      glTexCoord2f(0.359375f, 0.546875f);
      glVertex2f(kLeftX1, kY1);
      glTexCoord2f(0.390625f, 0.546875f);
      glVertex2f(kRightX0, kY1);
      glTexCoord2f(0.390625f, 0.742187f);
      glVertex2f(kRightX0, kY0);

      // right side
      glTexCoord2f(0.390625f, 0.742187f);
      glVertex2f(kRightX0, kY0);
      glTexCoord2f(0.390625f, 0.546875f);
      glVertex2f(kRightX0, kY1);
      glTexCoord2f(0.429687f, 0.546875f);
      glVertex2f(kRightX1, kY1);
      glTexCoord2f(0.429687f, 0.742187f);
      glVertex2f(kRightX1, kY0);
    glEnd();

    //se pinta el contenido del boton
    //		parent->wManagement->glWrite(x + (width-strlen(caption)*6) / 2, -y-20, caption);
    GL_WRITE(x_ + 1 + (width_ - caption_.size() * 6) / 2, y_ + 20, caption_.c_str());
  }

  glBindTexture(GL_TEXTURE_2D, 0);
  glDisable(GL_TEXTURE_2D);
}

END_NAMESPACE(gui);
END_TEG_NAMESPACE();
