/**

Game Develop - Path Automatism Extension
Copyright (c) 2010-2011 Florian Rival (Florian.Rival@gmail.com)

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not
    claim that you wrote the original software. If you use this software
    in a product, an acknowledgment in the product documentation would be
    appreciated but is not required.

    2. Altered source versions must be plainly marked as such, and must not be
    misrepresented as being the original software.

    3. This notice may not be removed or altered from any source
    distribution.

*/

#include "PathAutomatism.h"
#include "PathAutomatismEditor.h"
#include "GDL/Scene.h"
#include "GDL/tinyxml.h"
#include "GDL/XmlMacros.h"

PathAutomatism::PathAutomatism(std::string automatismTypeName) :
    Automatism(automatismTypeName),
    speed(200),
    timeOnSegment(0),
    totalSegmentTime(0),
    currentSegment(0),
    stopAtEnd(false),
    reverseAtEnd(true)
{
    path.push_back(sf::Vector2f(50,50));
    path.push_back(sf::Vector2f(150,50));
    path.push_back(sf::Vector2f(110,25));
    path.push_back(sf::Vector2f(190,85));

    Reset();
}

void PathAutomatism::Reset()
{
    EnterSegment(0);
}

void PathAutomatism::EnterSegment(unsigned int segmentNumber)
{
    currentSegment = segmentNumber;
    if (!path.empty() && currentSegment < path.size()-1)
    {
        sf::Vector2f newPath = (path[currentSegment + 1] - path[currentSegment]);
        totalSegmentTime = sqrtf(newPath.x*newPath.x+newPath.y*newPath.y);
        timeOnSegment = 0;
    }
}

PathAutomatism::~PathAutomatism()
{
}

#if defined(GD_IDE_ONLY)
void PathAutomatism::EditAutomatism( wxWindow* parent, Game & game_, Scene * scene, MainEditorCommand & mainEditorCommand_ )
{
    PathAutomatismEditor editor(parent, game_, scene, *this, mainEditorCommand_);
    editor.ShowModal();
}
#endif

/**
 * Called at each frame before events :
 * Position the object on the path
 */
void PathAutomatism::DoStepPreEvents(RuntimeScene & scene)
{
    //  add to the current time along the path
    timeOnSegment += scene.GetElapsedTime()*speed;

    //  if I reached the end of this segment, move to a new segment
    if (timeOnSegment >= totalSegmentTime && currentSegment < path.size())
        EnterSegment(currentSegment + 1);

    //Position object on the segment
    sf::Vector2f newPos;
    if ( !path.empty() && currentSegment < path.size()-1 )
        newPos = offset + path[currentSegment] + (path[currentSegment + 1] - path[currentSegment]) * (timeOnSegment / totalSegmentTime);
    else
    {
        if ( stopAtEnd && !path.empty()) newPos = path.back();
        else if (reverseAtEnd)
        {
            std::reverse(path.begin(), path.end());
            EnterSegment(0);
            if (!path.empty()) newPos = path.front();
        }
        else
        {
            EnterSegment(0);
            if (!path.empty()) newPos = path.front();
        }
    }

    object->SetX(newPos.x);
    object->SetY(newPos.y);

    return;
}

#if defined(GD_IDE_ONLY)
void PathAutomatism::SaveToXml(TiXmlElement * elem) const
{
}
#endif

void PathAutomatism::LoadFromXml(const TiXmlElement * elem)
{
}
