#include "Hero.h"


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

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

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

#define TILE_WIDTH 30
#define TILE_HEIGHT 30

Hero::Hero(Kraken* kraken, float pozX, float pozY)
{
        m_speedX=0;
        m_speedY=0;
        m_pozX=pozX;
        m_pozY=pozY;
        m_attacking=false;
        m_start.x=pozX;
        m_start.y=pozY;
        m_statut=1;
        m_kraken=kraken;
        hidden=false;
        circle.setRadius(0);
        circle.setPosition(m_pozX,m_pozY);
        test=0;
        m_dead=false;
        m_clock.restart();
        hasAttacked=false;


    //ctor
}

void Hero::die()
{
    m_dead=true;


}

bool Hero::attack()
{
    sf::Time time;
    time=m_clock.getElapsedTime();

    if(time.asSeconds()<=0.3)
    {
        time=m_clock.getElapsedTime();
    int coinH(0), coinV(0);
    if(m_statut==0){m_speedX=0.5;}
    if(m_statut==1){m_speedX=-0.5;}
    if(m_statut==2){m_speedY=-0.5;}
    if(m_statut==3){m_speedY=0.5;}


    if(m_speedX>0){coinH=m_pozX+TILE_WIDTH+m_speedX;}
    else if (m_speedX<0) {coinH=m_pozX+m_speedX; }
    else if(m_speedX==0)    {coinH=m_pozX+TILE_WIDTH/2;}

    if(m_speedY>0){coinV=m_pozY+TILE_HEIGHT+m_speedY;}
    else if (m_speedY<0) {coinV=m_pozY+m_speedY;}
    else if (m_speedY==0)   {coinV=m_pozY+2*TILE_HEIGHT/3;}

    if (next(coinH,coinV)==3 || next(coinH,coinV)==5 ) {die();}
    justMove();
    if(m_pozX>=m_kraken->Gethead().getPosition().x - TILE_WIDTH && m_pozX<=m_kraken->Gethead().getPosition().x +3*TILE_WIDTH && m_pozY<=m_kraken->Gethead().getPosition().y+3*TILE_HEIGHT && m_pozY>=m_kraken->Gethead().getPosition().y && hasAttacked==false){m_kraken->Damaged(); hasAttacked=true;}
    }
    else {m_speedX=0;m_speedY=0;
    m_attacking=false;hasAttacked=false;}
    
    return false;
}


void Hero::Settexture(std::string name)
{
    if(!m_texture.loadFromFile(name.c_str())){std::cout<<"Echec chargement texture Hero"<<std::endl;}
    m_sprite.setTexture(m_texture);
}

void Hero::draw()
{

    ///Chosing the picture
    if(m_speedX>0 && m_speedY==0){m_statut=0;}
    if(m_speedX<0 && m_speedY==0){m_statut=1;}
    if(m_speedY>0 && m_speedX==0){m_statut=3;}
    if(m_speedY<0 && m_speedX==0){m_statut=2;}
    ///

    ///Draw the hero
    if(hidden==false)
    {
        m_sprite.setTextureRect(sf::IntRect(6*(TILE_WIDTH+1)+m_statut*(TILE_WIDTH+1),0,TILE_WIDTH,TILE_HEIGHT));
    m_sprite.setPosition(m_pozX,m_pozY);
    m_kraken->Getlevel()->Getcase()->Getwindow()->draw(m_sprite);
    }

    ///LIFE
    if (m_color==2){m_sprite.setColor(sf::Color::Green);}
    else if (m_color==3){m_sprite.setColor(sf::Color::Red);}

    ///DEAD
    if (m_dead)
    {
//        m_kraken->restart();
        //circle.setFillColor(sf::Color::Red);
        test=0;
        sf::Texture Texplosion;
        if(!Texplosion.loadFromFile("explosion.jpg")){std::cout<<"Error loading texture explosion"<<std::endl;};
        sf::Sprite explosion;
        explosion.setTexture(Texplosion);
        circle.setTexture(&Texplosion);
    for(int i(0);i<200;i++)
    {
        test+=0.25;
        circle.setPosition(m_pozX-0.25*i,m_pozY-0.25*i);
        circle.setRadius(test);
        SetspeedX(0); SetspeedY(0);
        m_kraken->Getlevel()->Getcase()->Getwindow()->draw(circle);
        m_kraken->Getlevel()->Getcase()->Getwindow()->display();
       // for(int j(0);j<1000;j+=0.1){}
    }
    SetpozX(m_start.x);
    SetpozY(m_start.y);
    m_attacking=false;
    m_dead=false;
    m_speedX=0;
    m_speedY=0;
    }

}

void Hero::justMove()
{
    if(next(m_pozX+TILE_WIDTH+m_speedX,m_pozY+2*TILE_HEIGHT/3+m_speedY)==5){m_speedX=0;}    ///RIGHT
    if(next(m_pozX+m_speedX,m_pozY+2*TILE_HEIGHT/3+m_speedY)==5){m_speedX=0;}               ///LEFT
    if(next(m_pozX+m_speedX+TILE_WIDTH/2, m_pozY+2*TILE_HEIGHT/3+m_speedY)==5){m_speedY=0;} ///UP & DOWN

    if(m_kraken->Getlevel()->getCoord(convertCoordX(m_pozX+TILE_WIDTH/2),convertCoordY(m_pozY+2*TILE_HEIGHT/3))=='4')
    {



        for(int i(0);i<100;i++)
            {
                m_speedX=0; m_speedY=0;
                m_statut=i%4;
                m_sprite.setTextureRect(sf::IntRect(6*(TILE_WIDTH+1)+m_statut*(TILE_WIDTH+1),0,TILE_WIDTH,TILE_HEIGHT));
                m_kraken->Getlevel()->Getcase()->Getwindow()->draw(m_sprite);
                m_kraken->Getlevel()->Getcase()->Getwindow()->display();}




        SetpozY(m_kraken->Getlevel()->findAnother2('4').first*TILE_WIDTH);
        SetpozX(m_kraken->Getlevel()->findAnother2('4').second*TILE_HEIGHT);
}

    m_pozX+=m_speedX;
    m_pozY+=m_speedY;

}

void Hero::update()
{
    if (m_attacking && m_dead==false){attack();}
    else if(m_attacking==false){justMove();}

    if(m_kraken->getcounter()==2 && hidden==false){if(m_kraken->isThereCollision(convertCoordX(m_pozX+TILE_WIDTH/2),convertCoordY(m_pozY+TILE_HEIGHT/2))){
            m_dead=true; m_kraken->setVector(std::vector<int>(10,10));
    }}

    if(next(m_pozX+TILE_WIDTH/2,m_pozY+TILE_HEIGHT/2)==3){
            m_sprite.setTextureRect(sf::IntRect(11*(TILE_WIDTH+1),0,TILE_WIDTH,TILE_HEIGHT));
            m_sprite.setPosition(m_pozX,m_pozY);
            m_kraken->Getlevel()->Getcase()->draw(0,6,convertCoordX(m_pozX+TILE_WIDTH/2),convertCoordY(m_pozY+TILE_HEIGHT/2));
           hidden=true;}
    else if (next(m_pozX,m_pozY)!=3) {draw();hidden=false;}

}

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