#include "environnement.h"
#include "enum.h"
#include <cmath>
#include "options.h"


Environnement::Environnement()
{
    p = this;
    //setStyleSheet("background-color: green");
    setFixedSize(1550,700);
    nombrePandas = 10;
    nombreBambous = 100;
    showOption = 0;
    int i;

    backGround = new QLabel(this);
    QPixmap * backG = new QPixmap("blanc.png");
    backGround->setPixmap(*backG);
    backGround->setGeometry(1300,0,250,700);

    simulation = new QPushButton("Lancer go omg",this);
    simulation->setGeometry(1350,10,150,20);
    QObject::connect(simulation,SIGNAL(clicked()),this,SLOT(simuler()));

    stop = new QPushButton("Stop",this);
    stop->setGeometry(1350,40,150,20);
    QObject::connect(stop,SIGNAL(clicked()),this,SLOT(stoper()));

    optionsBouton = new QPushButton("Options",this);
    optionsBouton->setGeometry(1350,70,150,20);
    QObject::connect(optionsBouton,SIGNAL(clicked()),this,SLOT(options()));

    testBouton = new QPushButton("Test",this);
    testBouton->setGeometry(1350,660,150,20);
    QObject::connect(testBouton,SIGNAL(clicked()),this,SLOT(options()));


    for(i=0;i<nombrePandas;i++)    // PLACEMENT DES PANDAS
    {

        Panda nouveauPanda(p);
        pandas.push_back(nouveauPanda);
        nouveauPanda.getLabel()->move(nouveauPanda.getX(),nouveauPanda.getY());

    }

    for(i=0;i<nombreBambous;i++)   // PLACEMENT DES BAMBOUS
    {
        Bambou nouveauBambou(p);
        bambous.push_back(nouveauBambou);
        reference = nouveauBambou.getLabel();
        nouveauBambou.getLabel()->move(nouveauBambou.getX(),nouveauBambou.getY());
    }


}


void Environnement::stoper()
{
    nombreTour = 1;
    cout << "On a stopé le processus" << endl;
}


void Environnement::simuler()
{
    nombreTour = 100;
    while(nombreTour != 0)
    {

    cout << "Tour " << nombreTour <<  endl;
    cout << "Nombre Pandas" << nombrePandas << endl;
    int i;
    int j;
    int conditionFin;
    int bouffe = 0;


    QTime time;
    time.start();
    int tempsPasse = time.elapsed();
    while(tempsPasse < 1000)     // PAUSE DANS LE PROGRAMME
    {tempsPasse = time.elapsed();}
    QCoreApplication::processEvents();

    for(i=0;i<nombrePandas;i++) // DEBUT FOR PANDAS
    {
     Panda tmpPand = pandas.first();
     conditionFin = 1;
     bouffe = 0;

     for(j=0;j<=nombreBambous;j++)  // DEBUT FOR BAMBOUS
     {

         Bambou tmpBamb = bambous.first();
         float distance = equationDistancePB(tmpPand,tmpBamb);


            if((tmpPand.getVision() > distance) && conditionFin != 0 && (tmpPand.getHp() < 90)) // CONDITION DE MANGAGE NOM NOM NOM NOM
            {
                tmpPand.setX(tmpBamb.getX());
                tmpPand.setY(tmpBamb.getY());
                tmpBamb.getLabel()->setGeometry(0,0,0,0);
                tmpPand.getLabel()->setGeometry(tmpBamb.getX(),tmpBamb.getY(),20,20);
                bambous.pop_front();
                conditionFin = 0;
                bouffe = 1;
                tmpPand.setHp(tmpPand.getHp() + 10);
            }
            if(conditionFin == 1)
            {
                bambous.pop_front();
                bambous.push_back(tmpBamb);
            }

     } // FIN FOR BAMBOUS
     if(bouffe == 1)
     {
         nombreBambous--;
     }
     else
     {
         tmpPand = changerCoordonneesY(tmpPand);
         tmpPand = changerCoordonneesX(tmpPand);
         tmpPand.getLabel()->setGeometry(tmpPand.getX(),tmpPand.getY(),20,20);
         tmpPand.setHp(tmpPand.getHp() - 5);
     }
     pandas.pop_front();
     pandas.push_back(tmpPand);
    } // FIN FOR PANDAS

    accouchement();
    testMortPanda();
    reproduction();
    respawnBambou(5);
    nombreTour--;




        }
} // FIN FONCTION




float equationDistancePB(Panda a, Bambou b)
{
    float distance = sqrt(((a.getX() - b.getX())*(a.getX() - b.getX())) + ((a.getY() - b.getY())*((a.getY() - b.getY()))));
    return distance;
}

float equationDistancePP(Panda a, Panda b)
{
    float distance = sqrt(((a.getX() - b.getX())*(a.getX() - b.getX())) + ((a.getY() - b.getY())*((a.getY() - b.getY()))));
    return distance;
}

Panda changerCoordonneesX(Panda a)
{
    float tmp = a.getX();
    float tmp2 = tmp + rand()%40 - 19;
          while(tmp2>=1280 || tmp2<=0)
          {
              tmp2 = tmp;
              tmp2 = tmp + rand()%40 - 19;
          }
    a.setX(tmp2);
    return a;
}

Panda changerCoordonneesY(Panda a)
{
    float tmp = a.getY();
    float tmp2 = tmp + rand()%40 - 19;
          while(tmp2>=680 || tmp2<=0)
          {
              tmp2 = tmp;
              tmp2 = tmp + rand()%40 - 19;
          }
    a.setY(tmp2);
    return a;
}


void Environnement::setNombrePandas(int p)
{
    nombrePandas = p;
}


int Environnement::getPanda()
{
    return nombrePandas;
}

void Environnement::addPanda(QMainWindow *p)
{
    Panda newPanda(p);
    pandas.push_back(newPanda);
}

QVector<Panda> Environnement::getPandas()
{
    return pandas;
}


void Environnement::respawnBambou(int nbRespawn)
{
    int k;
    for(k=0;k<nbRespawn;k++)
    {
        Bambou nouveauBambou(p);
        bambous.push_back(nouveauBambou);
        nouveauBambou.getLabel()->setGeometry(nouveauBambou.getX(),nouveauBambou.getY(),20,20);
        nouveauBambou.getLabel()->show();
        nombreBambous++;
        QCoreApplication::processEvents();
    }
}


void Environnement::testMortPanda()
{
    int i;
    for(i=0;i<nombrePandas;i++)
    {
     Panda tmpPand = pandas.first();
         if(tmpPand.getHp() <= 0)
         {
             pandas.pop_front();
             tmpPand.getLabel()->hide();
             nombrePandas--;
         }
         else
         {
             pandas.pop_front();
             pandas.push_back(tmpPand);
         }
    }
}

void Environnement::reproduction()
{
    int i;
    for(i=0;i<nombrePandas;i++)
    {
       Panda tmpPand = pandas.first();
       pandas.pop_front();
       int j;
       for(j=0;j<nombrePandas-1;j++)
       {
           Panda tmpPand2 = pandas.first();
           if ((equationDistancePP(tmpPand,tmpPand2) <= tmpPand.getVision()) && (tmpPand2.getEnceinte() == 0) && (tmpPand2.getSexe() == FEMININ))
           {
                tmpPand.setX(tmpPand2.getX()+20);
                tmpPand2.setEnceinte(1);
                tmpPand2.setGestation(10);
                cout << "ENCEINTE" << endl;
           }
           pandas.pop_front();
           pandas.push_back(tmpPand2);

       }
       pandas.push_back(tmpPand);
    }
}


void Environnement::accouchement()
{
    int i;
    int nouveauBebe = 0;
    for(i=0;i<nombrePandas;i++)
    {
        Panda tmpPand = pandas.first();
           if(tmpPand.getGestation() == 1)
           {
               tmpPand.setGestation(0);
               tmpPand.setEnceinte(0);
               Panda bebe(p);
               bebe.setX(tmpPand.getX()+20);
               bebe.setY(tmpPand.getY());
               bebe.getLabel()->show();
               pandas.push_back(bebe);
               nouveauBebe++;
           }
           else if(tmpPand.getGestation() > 1)
           {
               tmpPand.setGestation(tmpPand.getGestation() - 1);
           }
        pandas.pop_front();
        pandas.push_back(tmpPand);
    }
    nombrePandas+=nouveauBebe;
}


void Environnement::options()
{
    cout << "Ferme toi pd de fenetre" << endl;
    opts->show();
}







