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

#include "radio_button.h"

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

BEGIN_TEG_NAMESPACE();
BEGIN_NAMESPACE(gui);

RadioButton::RadioButton(int x, int y, bool is_checked, int group)
    : CheckBox(x, y, is_checked), group_(group) {}

void RadioButton::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 kRadioButtonX0 = static_cast<float> (x_);
  const float kRadioButtonX1 = static_cast<float> (x_ + kCheckBoxSize);
  const float kRadioButtonY0 = static_cast<float> (-y_);
  const float kRadioButtonY1 = static_cast<float> (-y_ - kCheckBoxSize);

  // Render diferent texture if the control is checked.
  glBegin(GL_QUADS);
    if(is_checked_) {
      glTexCoord2f(0.6875f, 0.625f);
      glVertex3f(kRadioButtonX0, kRadioButtonY0, 0);
      glTexCoord2f(0.6875f, 0.5f);
      glVertex3f(kRadioButtonX0, kRadioButtonY1, 0);
      glTexCoord2f(0.8125f, 0.5f);
      glVertex3f(kRadioButtonX1, kRadioButtonY1, 0);
      glTexCoord2f(0.8125f, 0.625f);
      glVertex3f(kRadioButtonX1, kRadioButtonY0, 0);
    } else {
      glTexCoord2f(0.5625f, 0.625f);
      glVertex3f(kRadioButtonX0, kRadioButtonY0, 0);
      glTexCoord2f(0.5625f, 0.5f);
      glVertex3f(kRadioButtonX0, kRadioButtonY1, 0);
      glTexCoord2f(0.6875f, 0.5f);
      glVertex3f(kRadioButtonX1, kRadioButtonY1, 0);
      glTexCoord2f(0.6875f, 0.625f);
      glVertex3f(kRadioButtonX1, kRadioButtonY0, 0);
    }
  glEnd();

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

END_NAMESPACE(gui);
END_TEG_NAMESPACE();
