#include "classicflyinganimationscenario.h"
#include "graphics/classicnotegraphics.h"

void ClassicFlyingAnimationScenario::launch(const std::shared_ptr<ClassicNoteGraphics> sprite, const kku::microsec& time_begin, const kku::microsec &time_end)
{
    _sprite = sprite;
    _time_begin = time_begin;
    _time_end = time_end;

    _percentage = ((_time_end - _time_begin) * 0.01);
}

float ClassicFlyingAnimationScenario::getPoint(const kku::Point& point, float perc) const
{
    return point.x + ((point.y - point.x) * perc);
}

void ClassicFlyingAnimationScenario::update(const kku::microsec& music_offset)
{
    const auto crd = _sprite->getPosition();
    auto update_time = music_offset - _time_begin;
    float i = update_time / _percentage * 0.01;

    float xa = getPoint(kku::Point{crd.x + 20, crd.x + 90}, i);
    float ya = getPoint(kku::Point{crd.y - 600, crd.y - 150}, i);
    float xb = getPoint(kku::Point{crd.x + 90, crd.x}, i);
    float yb = getPoint(kku::Point{crd.y - 150, crd.y}, i);

    _sprite->setTrailPosition(kku::Point(getPoint( kku::Point{xa, xb}, i ), getPoint( kku::Point{ya, yb}, i )));

    bool pastPerfectScore = (i >= 1);

    if (pastPerfectScore)
        fadeTrailSprite();
}

bool ClassicFlyingAnimationScenario::isDone() const
{
    return false;
}

void ClassicFlyingAnimationScenario::fadeTrailSprite() const
{
    auto fill_color = _sprite->getTrailColor();

    if (fill_color.alpha == 0)
        return;

    auto new_alpha = (int(fill_color.alpha) - 15) < 0 ? 0 : int(fill_color.alpha) - 15;
    fill_color.alpha = new_alpha;

    _sprite->setTrailColor(fill_color);
}
