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

#include "frame.h"

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

BEGIN_TEG_NAMESPACE();
BEGIN_NAMESPACE(gui);

Frame::Frame(int x, int y, int width, int height)
    : Control(x, y, width, height) {

  contain_a_title_ = true;
  contain_a_close_button_ = true;
}

Frame::~Frame() {}

void Frame::Render() const {
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, GUI_TEXTURE);

  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_DEPTH_TEST);
  glColor4f(1,1,1, 1);

  glPushMatrix();
    glTranslatef(x_ + 0.0f, height_ - y_ + 0.0f, 0);

    glBegin(GL_QUADS);
      const float anchoMarco = 0.046563f;
      const float OneMinusanchoMarco = 1.0f - anchoMarco;

      const float nPixelMarco = 7.0f;
      const float nPixelTitle = 20.0f;

      const float leftMarco = 0.05321508f;
      const float rightMarco = 1.0f - leftMarco;

      const float kLeftX0 = static_cast<float>(0);
      const float kLeftX1 = static_cast<float>(7);
      const float kRightX0 = static_cast<float>(width_ - 7);
      const float kRightX1 = static_cast<float>(width_);
      const float kTopY0 = static_cast<float>(0);
      const float kTopY1 = static_cast<float>(-7);
      const float kBottomY0 = static_cast<float>(-height_ + 7);
      const float kBottomY1 = static_cast<float>(-height_);

      // Top of window.
      glTexCoord2f(0.0f, 1.0f);	
      glVertex3f(kLeftX0, kTopY0, 0.0f);
      glTexCoord2f(0.0f, OneMinusanchoMarco);
      glVertex3f(kLeftX0, kTopY1, 0.0f);
      glTexCoord2f(1.0f, OneMinusanchoMarco);
      glVertex3f(kRightX1, kTopY1, 0.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f(kRightX1, kTopY0, 0.0f);

      // Bottom of window.
      glTexCoord2f(0.0f, anchoMarco);
      glVertex3f(kLeftX0, kBottomY0, 0.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(kLeftX0, kBottomY1, 0.0f);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f(kRightX1, kBottomY1, 0.0f);
      glTexCoord2f(1.0f, anchoMarco);
      glVertex3f(kRightX1, kBottomY0, 0.0f);

      // Left side of window.
      glTexCoord2f(0.0f, 4.5f);
      glVertex3f(kLeftX0, kTopY1, 0.0f);
      glTexCoord2f(0.0f, 4.5f);
      glVertex3f(kLeftX0, kBottomY0, 0.0f);
      glTexCoord2f(leftMarco, 4.5f);
      glVertex3f(kLeftX1, kBottomY0, 0.0f);
      glTexCoord2f(leftMarco, 4.5f);
      glVertex3f(kLeftX1, kTopY1, 0.0f);

      // Right side of window.
      glTexCoord2f(rightMarco, 4.5f);
      glVertex3f(kRightX0, kTopY1, 0.0f);
      glTexCoord2f(rightMarco, 4.5f);
      glVertex3f(kRightX0, kBottomY0, 0.0f);
      glTexCoord2f(1.0f, 4.5f);
      glVertex3f(kRightX1, kBottomY0, 0.0f);
      glTexCoord2f(1.0f, 4.5f);
      glVertex3f(kRightX1, kTopY1, 0.0f);

      // Draw the main body of the window
      glTexCoord2f(0.070312f, 0.75f);
      glVertex3f(kLeftX1, kTopY1, 0.0f);
      glTexCoord2f(0.070312f, 0.5f);
      glVertex3f(kLeftX1, kBottomY0, 0.0f);
      glTexCoord2f(0.304687f, 0.5f);
      glVertex3f(kRightX0, kBottomY0, 0.0f);
      glTexCoord2f(0.304687f, 0.75f);
      glVertex3f(kRightX0, kTopY1, 0.0f);

      const float leftTitle = 0.319290f;
      const float rightTitle = 0.935698f;
      const float topTitle = 1 - 0.048780f;
      const float bottomTitle = 1 - 0.208426f;

      if (contain_a_title_) {
        // top of window.
        glTexCoord2f(leftTitle, topTitle);
        glVertex3f(nPixelMarco, -nPixelMarco, 0.0f);
        glTexCoord2f(leftTitle, bottomTitle);
        glVertex3f(nPixelMarco, -nPixelMarco - nPixelTitle, 0.0f);
        glTexCoord2f(rightTitle, bottomTitle);
        glVertex3f(width_ - nPixelMarco, -nPixelMarco - nPixelTitle, 0.0f);
        glTexCoord2f(rightTitle, topTitle);
        glVertex3f(width_ - nPixelMarco, -nPixelMarco, 0.0f);

        // OpenGL logo
        // top left corner of window.
        const float leftLogo = 0.055432f;
        const float rightLogo = 0.310421f;
        const float topLogo = 1.0f - 0.070953f;
        const float bottomLogo = 1.0f - 0.177384f;

        glTexCoord2f(leftLogo, topLogo);
        glVertex3f(10, -9, 0.f);
        glTexCoord2f(leftLogo, bottomLogo);
        glVertex3f(10, - 23, 0.f);
        glTexCoord2f(rightLogo, bottomLogo);
        glVertex3f(42, - 23, 0.f);
        glTexCoord2f(rightLogo, topLogo);
        glVertex3f(42, -9, 0.f);
      }

      if (contain_a_close_button_) {
        // window close button
        glTexCoord2f(0.8125f, 0.75f);
        glVertex3f(width_ - 22.f, - 8.f, 0.0f);
        glTexCoord2f(0.8125f, 0.625f);
        glVertex3f(width_ - 22.f, - 24.f, 0.0f);
        glTexCoord2f(0.9375f, 0.625f);
        glVertex3f(width_ - 6.f, - 24.f, 0.0f);
        glTexCoord2f(0.9375f, 0.75f);
        glVertex3f(width_ - 6.f, - 8.f, 0.0f);
      }
    glEnd();
  glPopMatrix();

  glDisable(GL_BLEND);

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

END_NAMESPACE(gui);
END_TEG_NAMESPACE();
