#include "info_view.h"
#include "l3d.h"
#include "player.h"
#include "world_view.h"
#include "ship.h"

InfoView::InfoView(void) : View() {
  SetTransparency(true);

  float size[2];
  GetSize(size);

  info1 = new Gui::Label("Some star stuff.");
  Add(info1, 40, size[1]-40);
}

void InfoView::UpdateInfo(void) {
  char buf[512];
  std::string nfo;
  const ShipType& stype = L3D::player->GetShipType();
  nfo = "SHIP INFORMATION: "+std::string(stype.name);

  Equip::Type e = L3D::player->m_equipment.Get(Equip::SLOT_ENGINE);
  nfo += std::string("\n\nDrive system:   ")+EquipType::types[e].name;

  const shipstats_t* stats;
  stats = L3D::player->CalcStats();
  snprintf(buf, sizeof(buf), "n\nCapacity:    %dt\n"
                             "Free:   %dt\n"
                             "Used:   %dt\n"
                             "All-up weight:    %dt", stats->max_capacity,
                             stats->free_capacity, stats->used_capacity,
                             stats->total_mass);
  nfo += std::string(buf);

  e = L3D::player->m_equipment.Get(Equip::SLOT_LASER, 0);
  nfo += std::string("\n\nFront weapon:   ")+EquipType::types[e].name;
  e = L3D::player->m_equipment.Get(Equip::SLOT_LASER, 1);
  nfo += std::string("\nRear weapon:    ")+EquipType::types[e].name;

  snprintf(buf, sizeof(buf), "\n\nHyperspace range: %.2f light years.", stats->hyperspace_range);
  nfo += std::string(buf);

  info1->SetText(nfo);
}

static ObjParams params = {
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f },

  /* pColor[3] */
  {
    { { 1.0f, 0.0f, 1.0f }, { 0, 0, 0 }, { 0, 0, 0 }, 0 },
    { { 0.8f, 0.6f, 0.5f }, { 0, 0, 0 }, { 0, 0, 0 }, 0 },
    { { 0.5f, 0.5f, 0.5f }, { 0, 0, 0 }, { 0, 0, 0 }, 0 }
  },

  /* pText[3][256] */
  { "IR-L33T", "ME TOO" },
};

void InfoView::Draw3D(void) {
  static float rot1, rot2;
  rot1 += .5*L3D::GetFrameTime();
  rot2 += L3D::GetFrameTime();
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-L3D::GetScrWidth() *.5, L3D::GetScrWidth()* .5,
            -L3D::GetScrHeight()*.5, L3D::GetScrHeight()*.5,
             L3D::GetScrWidth()* .5, 100000);
  glDepthRange(-10, -100000);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glClearColor(0, .2, .4, 0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  float lightCol[] = { 1, 1, 1 };
  float lightDir[] = { 1, 0, 0 };

  glPushAttrib(GL_ALL_ATTRIB_BITS);
  sbreSetDirLight(lightCol, lightDir);
  sbreSetViewport(L3D::GetScrWidth(), L3D::GetScrHeight(), 
                  L3D::GetScrWidth()*0.5, 5.0f, 100000.0f, 0.0f, 1.0f);
  /* Psht, go away sbre. I want my own viewport! */
  glViewport(L3D::GetScrWidth()/4, 0, L3D::GetScrWidth(), L3D::GetScrHeight());

  Vector p; p.x = 0; p.y = 0; p.z = 100;
  matrix4x4d rot = matrix4x4d::RotateXMatrix(rot1);
  rot.RotateY(rot2);
  Matrix m;
  m.x1 =  rot[0]; m.x2 =  rot[4]; m.x3 = -rot[ 8];
  m.y1 =  rot[1]; m.y2 =  rot[5]; m.y3 = -rot[ 9];
  m.z1 = -rot[2]; m.z2 = -rot[6]; m.z3 =  rot[10];
  const ShipType& stype = L3D::player->GetShipType();

  sbreSetDepthRange(L3D::GetScrWidth()*0.5f, 0.0f, 1.0f);
  sbreRenderModel(&p, &m, stype.sbreModel, &params);
  glPopAttrib();
}

void InfoView::Update(void) {

}