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

#include "panel.h"

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

BEGIN_TEG_NAMESPACE();
BEGIN_NAMESPACE(gui);

Panel::Panel(int x, int y, int width, int height)
    : Control(x, y, width, height) {}

void Panel::Render() const {
  // Display the panel 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_ + 10);
  const float kRightX0 = static_cast<float>(x_ + width_ - 10);
  const float kRightX1 = static_cast<float>(x_ + width_);
  const float kTopY0 = static_cast<float>(-y_);
  const float kTopY1 = static_cast<float>(-y_ - 10);
  const float kBottomY0 = static_cast<float>(-y_ - height_ + 10);
  const float kBottomY1 = static_cast<float>(-y_ - height_);

  // Render the panel.
  glBegin(GL_QUADS);  
    // Top left corner of panel.
    glTexCoord2f(0.0625f, 0.5f);
    glVertex3f(kLeftX0, kTopY0, 0);
    glTexCoord2f(0.0625f, 0.421875f);
    glVertex3f(kLeftX0, kTopY1, 0);
    glTexCoord2f(0.140625f, 0.421875f);
    glVertex3f(kLeftX1, kTopY1, 0);
    glTexCoord2f(0.140625f, 0.5f);
    glVertex3f(kLeftX1, kTopY0, 0);

    // Top of panel.
    glTexCoord2f(0.140625f, 0.5f);
    glVertex3f(kLeftX1, kTopY0, 0);
    glTexCoord2f(0.140625f, 0.421875f);
    glVertex3f(kLeftX1, kTopY1, 0);
    glTexCoord2f(0.234375f, 0.421875f);
    glVertex3f(kRightX0, kTopY1, 0);
    glTexCoord2f(0.234375f, 0.5f);
    glVertex3f(kRightX0, kTopY0, 0);

    // Top right corder of panel.
    glTexCoord2f(0.234375f, 0.5f);
    glVertex3f(kRightX0, kTopY0, 0);
    glTexCoord2f(0.234375f, 0.421875f);
    glVertex3f(kRightX0, kTopY1, 0);
    glTexCoord2f(0.3125f, 0.421875f);
    glVertex3f(kRightX1, kTopY1, 0);
    glTexCoord2f(0.3125f, 0.5f);
    glVertex3f(kRightX1, kTopY0, 0);

    // Left side of panel.
    glTexCoord2f(0.0625f, 0.421875f);
    glVertex3f(kLeftX0, kTopY1, 0);
    glTexCoord2f(0.0625f, 0.328125f);
    glVertex3f(kLeftX0, kBottomY0, 0);
    glTexCoord2f(0.140625f, 0.328125f);
    glVertex3f(kLeftX1, kBottomY0, 0);
    glTexCoord2f(0.140625f, 0.421875f);
    glVertex3f(kLeftX1, kTopY1, 0);

    // Middle of panel.
    glTexCoord2f(0.140625f, 0.421875f);
    glVertex3f(kLeftX1, kTopY1, 0);
    glTexCoord2f(0.140625f, 0.328125f);
    glVertex3f(kLeftX1, kBottomY0, 0);
    glTexCoord2f(0.234375f, 0.328125f);
    glVertex3f(kRightX0, kBottomY0, 0);
    glTexCoord2f(0.234375f, 0.421875f);
    glVertex3f(kRightX0, kTopY1, 0);

    // Right side of panel.
    glTexCoord2f(0.234375f, 0.421875f);
    glVertex3f(kRightX0, kTopY1, 0);
    glTexCoord2f(0.234375f, 0.328125f);
    glVertex3f(kRightX0, kBottomY0, 0);
    glTexCoord2f(0.3125f, 0.328125f);
    glVertex3f(kRightX1, kBottomY0, 0);
    glTexCoord2f(0.3125f, 0.421875f);
    glVertex3f(kRightX1, kTopY1, 0);

    // Bottom left corner of panel.
    glTexCoord2f(0.0625f, 0.328125f);
    glVertex3f(kLeftX0, kBottomY0, 0);
    glTexCoord2f(0.0625f, 0.25f);
    glVertex3f(kLeftX0, kBottomY1, 0);
    glTexCoord2f(0.140625f, 0.25f);
    glVertex3f(kLeftX1, kBottomY1, 0);
    glTexCoord2f(0.140625f, 0.328125f);
    glVertex3f(kLeftX1, kBottomY0, 0);

    // Bottom middle of panel.
    glTexCoord2f(0.140625f, 0.328125f);
    glVertex3f(kLeftX1, kBottomY0, 0);
    glTexCoord2f(0.140625f, 0.25f);
    glVertex3f(kLeftX1, kBottomY1, 0);
    glTexCoord2f(0.234375f, 0.25f);
    glVertex3f(kRightX0, kBottomY1, 0);
    glTexCoord2f(0.234375f, 0.328125f);
    glVertex3f(kRightX0, kBottomY0, 0);

    // Bottom right corner of panel.
    glTexCoord2f(0.234375f, 0.328125f);
    glVertex3f(kRightX0, kBottomY0, 0);
    glTexCoord2f(0.234375f, 0.25f);
    glVertex3f(kRightX0, kBottomY1, 0);
    glTexCoord2f(0.3125f, 0.25f);
    glVertex3f(kRightX1, kBottomY1,0);
    glTexCoord2f(0.3125f, 0.328125f);
    glVertex3f(kRightX1, kBottomY0, 0);
  glEnd();
 
  glBindTexture(GL_TEXTURE_2D, 0);
  glDisable(GL_TEXTURE_2D);
}

END_NAMESPACE(gui);
END_TEG_NAMESPACE();
