#include "Boss.h" 
#include "GunBullet.h"     
#include "PlayerShip.h"
#include "Smoke.h"
#include <windows.h>
#include <gl/glu.h>

int RandomNumber(int minvalue,int maxvalue);

extern CPlayerShip playerShip;
extern CGunBullet gunBullets;
extern CGunMissle gunMissles;
extern CSmoke smoke;

GLUquadricObj *targetOrb;
      
sVector directionFromAngles(sVector rotationVector);

CBoss::CBoss()
{
     sVector pos, scale, rotation;
     
     pos.x = 0;
     pos.y = 2000;
     pos.z = 500;
     
     scale.x = 40;
     scale.y = 40;
     scale.z = 40;
     
     rotation.x = -20;
     rotation.y = 90;
     rotation.z = 0;
     
     model.LoadObjFile("./models/Boss.obj");
     model.setScale(scale);
     model.setPos(pos);
     model.setRotation(rotation);
     
     state = DROPPING;
     
     targetOrb = gluNewQuadric();
     
     timer = 0;
     missleDelay = 0;
     injured = 0;
     health = 30;
}

CBoss::~CBoss()
{
}

sVector CBoss::getPos()
{
        return model.getPos();
}

void CBoss::update()
{
     sVector pos = model.getPos();
     sVector rotation = model.getRotation();
     
     if(state == DROPPING)
     {
              pos.y *= 0.99;
              if(pos.y < 80)
              {
                       sVector dest;
                       dest.z = 2000;
                       dest.y = 100;
                       dest.x = RandomNumber(-300,300);
                     
                       direction = dest.Difference(&pos);
                       direction.Normalize();      
                       state = FLYINGAWAY;
              }
     }
     else if(state == FLYINGAWAY)
     {
          rotation.y -= 4;
          if(rotation.y < -90)
                        rotation.y = -90;
          
          pos.x += direction.x * 10;
          pos.z += direction.z * 10;
          
          if(pos.z > 2000)
          {
             sVector dest;
             dest.z = 500;
             dest.y = 100;
             dest.x = RandomNumber(-600,600);
             
             direction = dest.Difference(&pos);
             direction.Normalize();      
             state = FLYINGTOWARDS;
          }
     }
     else if(state == FLYINGTOWARDS)
     {
          rotation.y += 10;
          if(rotation.y > 90)
                        rotation.y = 90;
          
          if(timer > 5 && missleDelay > 10)
          {
             sVector misslePos;
             misslePos.y = 150;
             misslePos.x = pos.x + RandomNumber(-1,1) * 200 - 100;
             misslePos.z = pos.z;         
             gunMissles.addMissle(misslePos);   
             missleDelay = 0;          
          }        
          
          missleDelay++;
          timer++;
          
          pos.x += direction.x * 15;
          pos.z += direction.z * 15;
          
          if(pos.z < 500)
          {
             sVector dest;
             dest.z = 2000;
             dest.y = 100;
             dest.x = RandomNumber(-600,600);
             
             direction = dest.Difference(&pos); 
             direction.Normalize();       
             state = FLYINGAWAY;
          }
          
          sVector testpos = pos;
          testpos.y += 50;
          
          if(gunBullets.hitTest(testpos) && (injured == 0))
          {
               injured = 50;
               health -= 11;   
               if(health < 0)
                  state = DEAD;        
          }  
     }
     else if(state == DEAD)
     {
          rotation.y += 10;
          
          for(int i = 0; i < 2; i++)
          {
            sVector newSmokePos = this->getPos();
            newSmokePos.x += RandomNumber(-160, 160);
            newSmokePos.z += RandomNumber(-160, 160);
            newSmokePos.y += RandomNumber(0, 200);
            smoke.addSmoke(newSmokePos);
          }
          
     }
     
     if(injured > 0)
     {
                pos.x += RandomNumber(-10,10);
                pos.z += RandomNumber(-10,10);
                injured--;
     }
     
     gunMissles.update();
     model.setPos(pos);
     model.setRotation(rotation);
}

void CBoss::Render(float alpha)
{
    static float orbBrightness = 0.0; 
    orbBrightness+=0.01;
    if(orbBrightness >= 6.7)
        orbBrightness = 0; 
    
    GLdouble color;
     
    glColor4f(0.3 + (injured * 0.1), 0.3, 0.3, alpha);
    model.Render();
    
    sVector pos = model.getPos();
    sVector scale = model.getScale();
    sVector rotation = model.getRotation();
    
    glPushMatrix();
    glTranslatef(pos.x, pos.y, pos.z);
    glScalef(scale.x, scale.y, scale.z);
   
    glRotatef(rotation.z, 0, 0, 1.0);
    glRotatef(rotation.x, 1.0, 0, 0);
    glRotatef(rotation.y, 0, 1.0, 0);
    
    glTranslatef(1, 1, 0);
    
    //render his "Orb of Power" ^^
    glColor4f(1.0, sin(orbBrightness), 0.5, 1.0);
    gluSphere(targetOrb, 0.5, 5, 5);
    glColor4f(0.3, 0.3, 0.3, 0.5);
    
    glPopMatrix();
}

int CBoss::getStatus()
{
	/* TODO (#1#): Implement CBoss::getStatus() */
}
