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

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

BEGIN_TEG_NAMESPACE();
BEGIN_NAMESPACE(gui);

CheckBox::CheckBox(int x, int y, bool is_checked)
    : Control(x, y, kCheckBoxSize, kCheckBoxSize), is_checked_(is_checked) {}

CheckBox::~CheckBox() {}

void CheckBox::Render() const {
  // Display the check box if it's visible.
  if(!is_visible_) return;

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

  const float kCheckBoxX0 = static_cast<float> (x_);
  const float kCheckBoxX1 = static_cast<float> (x_ + kCheckBoxSize);
  const float kCheckBoxY0 = static_cast<float> (-y_);
  const float kCheckBoxY1 = static_cast<float> (-y_ - kCheckBoxSize);

  // Render diferent texture if the control is checked
  glBegin(GL_QUADS);
    if(is_checked_) {
      glTexCoord2f(0.6875f, 0.75f);
      glVertex3f(kCheckBoxX0, kCheckBoxY0, 0);
      glTexCoord2f(0.6875f, 0.625f);
      glVertex3f(kCheckBoxX0, kCheckBoxY1, 0);
      glTexCoord2f(0.8125f, 0.625f);
      glVertex3f(kCheckBoxX1, kCheckBoxY1, 0);
      glTexCoord2f(0.8125f, 0.75f);
      glVertex3f(kCheckBoxX1, kCheckBoxY0, 0);
    } else {
      glTexCoord2f(0.5625f, 0.75f);
      glVertex3f(kCheckBoxX0, kCheckBoxY0, 0);
      glTexCoord2f(0.5625f, 0.625f);
      glVertex3f(kCheckBoxX0, kCheckBoxY1, 0);
      glTexCoord2f(0.6875f, 0.625f);
      glVertex3f(kCheckBoxX1, kCheckBoxY1, 0);
      glTexCoord2f(0.6875f, 0.75f);
      glVertex3f(kCheckBoxX1, kCheckBoxY0, 0); 
    }
  glEnd();

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

END_NAMESPACE(gui);
END_TEG_NAMESPACE();
