/*
 * Game.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Game.h"
#include "Collision.h"
#include "Level.h"
#include "Shader.h"
#include "ShadowVolume.h"
#include "Triangle.h"

#include "gl.h"

#include <algorithm>

namespace Zoom {

const float Game::PLAYER_SPEED = 3;
const float Game::PLAYER_RADIUS = 0.3;

Game::Game() : playerPos(vmml::vec3f::ZERO), playerRotY(vmml::mat4f::IDENTITY), playerRotX(0),
    input(0), lightPos(0) {
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_STENCIL_TEST);

  glEnable(GL_BLEND);

  glEnable(GL_MULTISAMPLE_ARB);

  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vmml::vec4f(0.05, 0.05, 0.05, 1).array);

  glLightfv(GL_LIGHT0, GL_AMBIENT, vmml::vec4f::ZERO.array);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, vmml::vec4f::ONE.array);
  glLightfv(GL_LIGHT0, GL_SPECULAR, vmml::vec4f::ONE.array);
  glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.2);

  glEnable(GL_COLOR_MATERIAL);
  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vmml::vec4f::ONE.array);
  glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 128);

  glEnable(GL_CULL_FACE);
  glFrontFace(GL_CCW);

  glEnable(GL_STENCIL_TEST);

  loadLevel("level.xml");
  triangles.insert(triangles.end(), level->getRooms().front().walls.begin(), level->getRooms().front().walls.end());
  std::sort(triangles.begin(), triangles.end(), Renderer::TextureSorter());
}

bool Game::loadLevel(const std::string &name) {
  level = Level::loadLevel(name);

  return level;
}

void Game::resize(int width, int height) {
  if(height == 0) {
    height = 1;
  }


  glMatrixMode(GL_PROJECTION);
  glLoadMatrixf(MathUtil::perspective(50.0f, (float)width/(float)height, 0.1).array);

  glMatrixMode(GL_MODELVIEW);

  glViewport(0, 0, width, height);
}

void Game::turn(float x, float y) {
  playerRotY.rotate_y(-x*M_PI/180/10);

  playerRotX -= y*M_PI/180/10;
  if(playerRotX > M_PI_2)
    playerRotX = M_PI_2;
  else if(playerRotX < -M_PI_2)
    playerRotX = -M_PI_2;
}

bool Game::doesCollide(const vmml::vec3f &playerMove) const {
  for(std::vector<TriangleRecord>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
    if(Collision::test(t->getTriangle(), playerPos, PLAYER_RADIUS-MathUtil::EPSILON, playerMove))
      return true;
  }

  return false;
}

void Game::run(int delta) {
  lightPos += delta*0.5;
  lightPos = std::fmod(lightPos, 26000);

  vmml::vec3f playerMove(vmml::vec3f::ZERO);

  if(input & FORWARD) {
    playerMove -= playerRotY*vmml::vec3f::UNIT_Z;
  }
  if(input & BACKWARD) {
    playerMove += playerRotY*vmml::vec3f::UNIT_Z;
  }
  if(input & LEFT) {
    playerMove -= playerRotY*vmml::vec3f::UNIT_X;
  }
  if(input & RIGHT) {
    playerMove += playerRotY*vmml::vec3f::UNIT_X;
  }

  if(playerMove == vmml::vec3f::ZERO)
    return;

  playerMove.normalize();
  playerMove *= PLAYER_SPEED*delta/1000;

  vmml::vec3f origMove = playerMove;

  bool collision = true;

  while(collision) {
    collision = false;

    MathUtil::Plane nearestPlane;
    float nearestDistance;

    for(std::vector<TriangleRecord>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
      if(Collision::test(t->getTriangle(), playerPos, PLAYER_RADIUS, playerMove)) {
        vmml::vec3f normal = t->getTriangle().computeNormal();
        if(normal.dot(playerMove) >= 0)
          continue;

        MathUtil::Plane p(normal, vmml::dot(normal, t->getTriangle().getVertex(0)+PLAYER_RADIUS*normal));
        if(p.isInFront(playerPos) || p.contains(playerPos)) {
          vmml::vec3f intersection = p.intersection(MathUtil::Ray(playerPos, playerMove));
          float distance = intersection.squared_distance(playerPos);

          if(!collision || distance < nearestDistance) {
            collision = true;

            nearestPlane = p;
            nearestDistance = distance;
          }
        }
        else {
          // TODO Edge collision

        }
      }
    }

    if(collision) {
      vmml::vec3f move;

      if(playerMove.dot(nearestPlane.getNormal()) == 0)
        move = playerMove;
      else
        move = nearestPlane.intersection(MathUtil::Ray(playerPos, playerMove)) - playerPos;

      if(move.dot(origMove) <= 0 && move.squared_length() > MathUtil::EPSILON) {
        return;
      }

      if(doesCollide(move))
        return;

      playerPos += move;

      vmml::vec3f restMove = playerMove - move;
      playerMove = restMove - nearestPlane.getNormal() * (nearestPlane.getNormal().dot(restMove));
    }
  }

  if(!doesCollide(playerMove))
    playerPos += playerMove;
}

void Game::render() {
  int i;
  vmml::vec3f light(vmml::vec3f::ZERO);

  if(lightPos < 13000) i = lightPos;
  else i = 26000 - lightPos;

  if(i < 500) {
    light.x() = 0.0;
    light.z() = 0.0;
  }
  else if(i < 4500) {
    light.x() = 0.0;
    light.z() = -(i-500) * 0.001;
  }
  else if(i < 8500) {
    light.x() = (i-4500) * 0.001;
    light.z() = -4.0;
  }
  else if(i < 12500) {
    light.x() = 4.0;
    light.z() = -4.0 - (i-8500) * 0.001;
  }
  else {
    light.x() = 4.0;
    light.z() = -8.0;
  }

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  vmml::mat4f transform(playerRotY), inverse;
  transform.rotate_x(playerRotX);
  transform.set_translation(playerPos);
  transform.inverse(inverse);

  glLoadMatrixf(inverse.array);

  renderer.render(triangles, light);

  Shader::disable();
  glDepthFunc(GL_LEQUAL);
  glStencilFunc(GL_ALWAYS, 0, std::numeric_limits<GLuint>::max());
  glBlendFunc(GL_ONE, GL_ZERO);

  glDisable(GL_TEXTURE_2D);
  glColor3f(1, 1, 1);

  vmml::vec3f lightVec = playerPos-light;

  vmml::vec3f axis1 = lightVec.cross(vmml::vec3f(0, 1, 0)), axis2 = lightVec.cross(axis1);
  axis1.normalize();
  axis2.normalize();

  glBegin(GL_POLYGON);
  for(int i = 0; i < 32; ++i) {
    vmml::vec3f pos = light + axis1*std::cos(M_PI*i/16.0)*0.05 + axis2*std::sin(M_PI*i/16.0)*0.05;

    glVertex3fv(pos.array);
  }
  glEnd();

  glEnable(GL_TEXTURE_2D);
}

}
