#include "zombiesimple.h"
#include "proyectil.h"
#include <math.h>
#include <QDebug>

zombieSimple::zombieSimple(QPixmap pixmap)
{
    trajectoryPos = 1;
    m_x = 0;
    m_y = 0;
    setPixmap(pixmap);
    yAnimation = new QPropertyAnimation(this,"y",this);
    xAnimation = new QPropertyAnimation(this,"x",this);
    xAnimationFinished = false;
    yAnimationFinished = false;
    connect(xAnimation,&QPropertyAnimation::finished,this,[=](){
        xAnimation->stop();
        xAnimationFinished = true;
        emit checkAnimationFinished();
    });
    connect(yAnimation,&QPropertyAnimation::finished,this,[=](){
        yAnimation->stop();
        yAnimationFinished = true;
        emit checkAnimationFinished();
    });
    hitProjectileTimer = new QTimer(this);
    connect(hitProjectileTimer,&QTimer::timeout,this,[=](){
        startTrajectory();
        setPixmap(QPixmap(":/ImagenesDeProyecto/Zombie1erAno.png"));
        hitProjectileTimer->stop();
    });
    connect(this,&zombieSimple::nextTrajectoryPoint,this,&zombieSimple::startTrajectory);
    connect(this,&zombieSimple::checkAnimationFinished,this,&zombieSimple::animationFinished);
}

zombieSimple::~zombieSimple()
{
    for(int i=0; i<trajectory.size(); i++){
        trajectory.removeLast();
    }
    delete xAnimation;
    delete yAnimation;
    delete hitProjectileTimer;
}

qreal zombieSimple::x() const
{
    return m_x;
}

void zombieSimple::setX(qreal newX)
{
    moveBy(newX-m_x,0);
    m_x = newX;
    emit xChanged();
    emit checkMove();
}

qreal zombieSimple::y() const
{
    return m_y;
}

void zombieSimple::setY(qreal newY)
{
    moveBy(0,newY-m_y);
    m_y = newY;
    emit yChanged();
    emit checkMove();
}

void zombieSimple::addTrajectoryPoint(QPointF newPoint)
{
    trajectory.append(newPoint);
}

void zombieSimple::startTrajectory()
{
    //qDebug() << trajectoryPos;
    if(trajectory.at(trajectoryPos).y() != scenePos().y()){
        yAnimation->setStartValue(scenePos().y());
        yAnimation->setEndValue(trajectory.at(trajectoryPos).y());
        yAnimation->setEasingCurve(QEasingCurve::InOutQuad);
        yAnimation->setDuration(60.0*pow(abs(trajectory.at(trajectoryPos).y()-
                                             scenePos().y()),1.0/3.0)+1);
        yAnimation->start();
    }
    else{
        yAnimation->stop();
        yAnimationFinished = true;
        emit checkAnimationFinished();
    }

    if(trajectory.at(trajectoryPos).x() != scenePos().x()){
        //qDebug() << scenePos().x() << trajectory.at(trajectoryPos).x();
        xAnimation->setStartValue(scenePos().x());
        xAnimation->setEndValue(trajectory.at(trajectoryPos).x());
        xAnimation->setEasingCurve(QEasingCurve::Linear);
        xAnimation->setDuration(10*abs(trajectory.at(trajectoryPos).x()
                                       -scenePos().x()));
        xAnimation->start();
    }
    else{
        xAnimation->stop();
        xAnimationFinished = true;
        emit checkAnimationFinished();
    }
}

void zombieSimple::animationFinished()
{
    if(xAnimationFinished && yAnimationFinished)
    {
        xAnimationFinished = false;
        yAnimationFinished = false;
        trajectoryPos = (trajectoryPos+1)%trajectory.size();
        emit nextTrajectoryPoint();
    }
}

void zombieSimple::checkProyectil()
{
    QList<QGraphicsItem*> collidingItems = this->collidingItems();
    foreach(QGraphicsItem *item, collidingItems){
        proyectil * bala = dynamic_cast<proyectil*>(item);
        if(bala){
            xAnimation->stop();
            yAnimation->stop();
            setPixmap(QPixmap(":/ImagenesDeProyecto/Zombie1erAnoSano.png"));
            hitProjectileTimer->start(1000);
        }
    }
}
