#include "Kraken.h"

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

#include "Case.h"
#include "Level.h"
#include "Kraken.h"
#include "Hero.h"

#include <string>
#include <vector>
#include <iostream>

#define TILE_WIDTH 30
#define TILE_HEIGHT 30

Kraken::Kraken(Level* level,int width, int height, int numberTenta)
{
    m_level=level;
    m_clock.restart();
    m_tentacleClock.restart();
    m_maxX=width;
    m_maxY=height;
    pozX=rand()%(m_maxX-3)*TILE_WIDTH+1;
    pozY=rand()%(m_maxY-6)*TILE_HEIGHT+1;
    m_head.setPosition(pozX,pozY);
    m_counter=0;
    m_life=5;
    m_damaged=false;
    m_dead=false;
    m_numberTenta=numberTenta;
    m_coords=std::vector<int>(10,0);
    m_temp.restart();
    rapidity=1;


}


void Kraken::Settexture(std::string name)
{
    if(!m_texture.loadFromFile(name)){std::cout<<"Echec de l'ouverture texture Kraken" << std::endl;}
    m_head.setTexture(m_texture);
    m_tentacle.setTexture(m_texture);

    ///Head

}

void Kraken::drawTheHead(int a)
{
    m_head.setTextureRect(sf::IntRect(a*2*(1+TILE_WIDTH),TILE_HEIGHT,TILE_WIDTH*2, TILE_HEIGHT*3));
   // m_head.setPosition(x*TILE_WIDTH,y*TILE_HEIGHT);
    m_level->Getcase()->Getwindow()->draw(m_head);
    int x= int(m_head.getPosition().x/TILE_WIDTH);
    int y=int(m_head.getPosition().y/TILE_HEIGHT);

    for (int i(x-1);i<=x+2;i++)
    {
        for(int j(y-1);j<=y+3;j++)
        {
            m_level->setCoordCase(i,j,'5');
        }
    }
    m_level->setCoordCase(x,y+3,'2');
    m_level->setCoordCase(x+1,y+3,'2');

    m_level->setCoordCase(x-2,y+4,'1');
    m_level->setCoordCase(x-1,y+4,'1');
    m_level->setCoordCase(x,y+4,'1');
    m_level->setCoordCase(x+1,y+4,'1');
    m_level->setCoordCase(x+2,y+4,'1');
    m_level->setCoordCase(x+3,y+4,'1');



}

void Kraken::placeTheHeadAnywhere()
{
     {
            pozX=rand()%(m_maxX-3)*TILE_WIDTH+3;
            pozY=rand()%(m_maxY-6)*TILE_HEIGHT+1;
            m_head.setPosition(pozX,pozY);}
}


void Kraken::drawATentacle()
{


    m_tentacleTime=m_tentacleClock.getElapsedTime();
    int counteur;
    bool pick(false), ended(true);
    ///Go
    if(m_tentacleTime.asMilliseconds()%2000<=50 && m_tentacleTime.asMilliseconds()%2000>=25){pick=true; ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=100*rapidity && m_tentacleTime.asMilliseconds()%2000>=50){counteur=0;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=200*rapidity){counteur=1;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=300*rapidity){counteur=2;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=400*rapidity){counteur=3;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=500*rapidity){counteur=4;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=600*rapidity){counteur=5;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=700*rapidity){counteur=6;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=800*rapidity){counteur=7;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=900*rapidity){counteur=8;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1000*rapidity){counteur=9;ended=false;}
    ///Back
    else if(m_tentacleTime.asMilliseconds()%2000<=1100*rapidity){counteur=8;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1200*rapidity){counteur=7;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1300*rapidity){counteur=6;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1400*rapidity){counteur=5;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1500*rapidity){counteur=4;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1600*rapidity){counteur=3;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1700*rapidity){counteur=2;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1800*rapidity){counteur=1;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1900*rapidity){counteur=0;ended=false;}
    else if(m_tentacleTime.asMilliseconds()%2000<=1999*rapidity){counteur=0;ended=true;}
    else {m_tentacleClock.restart();}

    if(pick){

        m_coords.erase(m_coords.begin(),m_coords.end());
        setNumberTenta(rand()%6+1);

       for(int i(0);i<2*m_numberTenta-1;i+=2){


                                    m_coords.push_back(abs(pickX()));
                                    m_coords.push_back(abs(pickY()));
                                    }
    }



    m_tentacle.setTextureRect(sf::IntRect(counteur*(TILE_WIDTH+1),4*(TILE_HEIGHT+2),TILE_WIDTH, TILE_HEIGHT*2));
    for(int i(0);i<m_numberTenta*2;i+=2){
                            m_tentacle.setPosition(m_coords[i]*TILE_WIDTH,m_coords[i+1]*TILE_HEIGHT);
                            m_level->Getcase()->Getwindow()->draw(m_tentacle);}

if (ended){for(size_t i(0); i<m_coords.size();i++){m_coords[i]=0;}}


}




bool Kraken::isThereCollision(int x, int y)
 {

     bool temp(false);
     for(size_t i(0);i<m_coords.size()-2;i+=2){

            if(m_coords[i]==x && m_coords[i+1]==y)
                {std::cout<<" touche "; temp=true;}
                                        }
    return temp;

 }



void Kraken::update()
{
    ///HEAD
    m_time=m_clock.getElapsedTime();

    if(m_time.asSeconds()<5*rapidity){m_counter=0;}
    else if(m_time.asMilliseconds()<=8000*rapidity){m_counter=1;}
    else if(m_time.asMilliseconds()<=10000*rapidity){m_counter=2;}
    else {m_clock.restart();}
    if(m_life==1){m_counter=2;}
    ///

    ///DAMAGED
    if(isDamaged()){

    sf::Time test;
    test=m_temp.getElapsedTime();
    m_time=m_clock.getElapsedTime();

    if (test.asMilliseconds()%4000<=2000){m_counter=3;}

    else if (test.asMilliseconds()%4000>2000) {m_counter=3;m_damaged=false;    m_life-=1; if(m_life<=0){m_counter=4;}  placeTheHeadAnywhere(); m_damaged=false; }
    //rapidity-=0.17;




    }
    ///

    ///DEAD
    if(isDead()){m_counter=4;}
    ///

    if(m_counter==2){drawATentacle();}


    drawTheHead(m_counter);

    ///TENTACLE

}




Kraken::~Kraken()
{
    //dtor
}
