/**

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.

*/

#ifndef PHYSICAUTOMATISM_H
#define PHYSICAUTOMATISM_H

#include "GDL/Automatism.h"
#include "GDL/Object.h"
#include "ScenePathDatas.h"
#include <boost/weak_ptr.hpp>
#include <iostream>
#include <map>
#include <set>
#include "GDL/RuntimeScene.h"
class TiXmlElement;
class Scene;
class PathAutomatismEditor;

/**
 * Automatism that position object on a predefined path.
 */
class GD_EXTENSION_API PathAutomatism : public Automatism
{
    friend class PathAutomatismEditor;

    public:
        PathAutomatism(std::string automatismTypeName);
        virtual ~PathAutomatism();
        virtual boost::shared_ptr<Automatism> Clone() { return boost::shared_ptr<Automatism>(new PathAutomatism(*this));}

        #if defined(GD_IDE_ONLY)
        /**
         * Save Automatism to XML
         */
        virtual void SaveToXml(TiXmlElement * elem) const;
        #endif

        /**
         * Load Automatism from XML
         */
        virtual void LoadFromXml(const TiXmlElement * elem);

        #if defined(GD_IDE_ONLY)
        /**
         * Called when user wants to edit the automatism.
         */
        virtual void EditAutomatism( wxWindow* parent, Game & game_, Scene * scene, MainEditorCommand & mainEditorCommand_ );
        #endif

        void Reset();
        void EnterSegment(unsigned int segmentNumber);

        static std::map < const Scene* , ScenePathDatas > scenesPathDatas; ///< Static map associating scene to datas

        bool CondReverseAtEnd( RuntimeScene & scene, ObjectsConcerned & objectsConcerned, const Instruction & action ) { return reverseAtEnd; };
        bool CondStopAtEnd( RuntimeScene & scene, ObjectsConcerned & objectsConcerned, const Instruction & action ) { return stopAtEnd; };
        bool ActReverseAtEnd( RuntimeScene & scene, ObjectsConcerned & objectsConcerned, const Instruction & action ) { reverseAtEnd = action.GetParameter(2).GetAsBool(); return true;};
        bool ActStopAtEnd( RuntimeScene & scene, ObjectsConcerned & objectsConcerned, const Instruction & action ) { stopAtEnd = action.GetParameter(2).GetAsBool(); return true; };

    private:

        virtual void DoStepPreEvents(RuntimeScene & scene);

        //sf::Vector2f has been choosen too represent position, but any simple vector2 class would do the job.
        std::vector<sf::Vector2f> path;
        sf::Vector2f offset;
        float speed;
        float timeOnSegment;
        float totalSegmentTime;
        unsigned int currentSegment;

        bool stopAtEnd;
        bool reverseAtEnd;

        boost::shared_ptr<RuntimeScenePathDatas> runtimeScenesPathDatas;
};

#endif // PHYSICAUTOMATISM_H
