#include "Fighter.h" 
#include "GunBullet.h"     
#include "GunMissle.h"
#include "PlayerShip.h"
#include "Smoke.h"
#include <windows.h>

int RandomNumber(int minvalue,int maxvalue);

extern CPlayerShip playerShip;
extern CGunBullet gunBullets;
extern CGunMissle gunMissles;
extern CSmoke smoke;
      
sVector directionFromAngles(sVector rotationVector);

CFighter::CFighter()
{
     sVector scale, rotation;
     
     scale.x = 10;
     scale.y = 10;
     scale.z = 10;
     
     rotation.x = 0;
     rotation.y = 180;
     rotation.z = 0;
     
     model.LoadObjFile("./models/Fighter.obj");
     model.setScale(scale);
     model.setRotation(rotation);
     
     shouldFire = false;
}

CFighter::~CFighter()
{
}

void CFighter::reset()
{
     while(fighterList.GetNumItems() != 0)
     {
        delete (sFighter *)fighterList.GetItem(0);
        fighterList.DeleteItem(0);
     }
}

void CFighter::update()
{
     
     if((RandomNumber(0, 1000) > 995) && generate)
     {
             sVector pos;
             pos.x = RandomNumber(-600,600);
             pos.y = RandomNumber(150,300);;
             pos.z = 4000;
             
             addFighter(pos);
     }
     
     int numFighters = fighterList.GetNumItems();
     
     for(int i = 0; i < numFighters; i++)
     {
             sFighter *fighter = (sFighter *)fighterList.GetItem(i);
             if(fighter->state == HIT)
             {
                 fighter->pos.x += fighter->direction.x * 10;
                 fighter->pos.y += fighter->direction.y - fighter->rotation.x;
                 fighter->pos.z += fighter->direction.z * 10;
                 fighter->rotation.z += 10.2;
                 fighter->rotation.x += 0.2;
                 fighter->rotation.y += 0.2;
                 
                 sVector newSmokePos = fighter->pos;
                 newSmokePos.x += RandomNumber(-10, 10);
                 newSmokePos.z += RandomNumber(-10, 10);
                 smoke.addSmoke(newSmokePos);
             }
             else
             {
                 fighter->pos.x += fighter->direction.x * 10;
                 fighter->pos.y += fighter->direction.y * 10;
                 fighter->pos.z += fighter->direction.z * 10;
                 fighter->rotation.y += 0.1;
                 //do collision detection for bullets
                 
                 if(gunBullets.hitTest(fighter->pos))
                 {
                          fighter->state = HIT;                   
                 }  
                 
                 if(shouldFire && (RandomNumber(0, 200) > 199))
                 {
                          gunMissles.addMissle(fighter->pos);               
                 }
             } 

             sVector distance = playerShip.getPos().Difference(&fighter->pos);
             if(distance.x < 0)
                distance.x = -distance.x;
             if(distance.y < 0)
                distance.y = -distance.y;
             if(distance.z < 0)
                distance.z = -distance.z;
                
             if(distance.x < 60)
                if(distance.y < 60)
                   if(distance.z < 60)
                       playerShip.Hit();
                       
                       
             if(fighter->pos.z < -100 || fighter->pos.y < 0)
             {
                      delete fighter;
                      fighterList.DeleteItem(i);
                      numFighters = fighterList.GetNumItems();
                      i--;
             }
     }  
}

void CFighter::setFire(bool shouldFire)
{
     this->shouldFire = shouldFire;
}

void CFighter::setGenerate(bool generate)
{
     this->generate = generate;
}

void CFighter::Render()
{
     int numFighters = fighterList.GetNumItems();
     
     for(int i = 0; i < numFighters; i++)
     {
             sFighter *fighter = (sFighter *)fighterList.GetItem(i);
     
             model.setPos(fighter->pos);
             model.setRotation(fighter->rotation);
             model.Render();
     }
     
     gunMissles.Render();
}

void CFighter::addFighter(sVector pos)
{
     if(fighterList.GetNumItems() > 2000)
       return;
      
     sFighter *fighter = new sFighter();
     if(fighter == NULL)
      return;
      
     sVector rotation;
     
     rotation.x = 0;
     rotation.y = 180 + RandomNumber(-20,20);
     rotation.z = rotation.y - 180; 
     fighter->pos = pos;
     fighter->rotation = rotation;
     fighter->direction = directionFromAngles(rotation);
     fighter->state = ALIVE;
          
     fighterList.AppendItem(fighter);
}
