#ifndef Model2D_H
#define Model2D_H

#include <functional>

#include "Array.h"
#include "Drawing.h"
#include "Point.h"
#include "TriangleList.h"
#include "Vec3.h"
#include "World2D.h"

namespace Framework
{
    class Texture2D;

    //! A polygon structure used by Model2D.
    //! Model2D class deletes the pointers
    struct Polygon2D
    {
        bool transparent;
        Text* name;
        Array<Vertex>* vertex;
        Array<Vertex>* tCoordinates;
        Vertex* center;
    };

    //! The data for a 2D model
    class Model2DData : public virtual ReferenceCounter
    {
    private:
        RCArray<Array<Point>> outList;

        //! Checks whether a point is inside the model
        //! \param p The point
        //! \param polygonId The ID of the polygon to check
        bool isPointInside(Vertex p, int polygonId = -1) const;
        //! Checks whether a line is inside the model
        //! \param a The start point of the line
        //! \param b The end point of the line
        bool isLineInside(Vertex a, Vertex b, int polygonId = -1) const;

    public:
        Array<Polygon2D>* polygons;
        RCArray<RCArray<TriangleList<Vertex>>>* vLists;
        Point minP, maxP;

        //! Constructor
        __declspec(dllexport) Model2DData();
        //! Destructor
        __declspec(dllexport) ~Model2DData();
        //! Creates the triangle lists from all given vertices of the
        //! polygons \param polygons An array of polygons \return always
        //! returns 1
        __declspec(dllexport) bool createModell(Array<Polygon2D>* polygons);
        //! Deletes the created triangle lists and vertices
        __declspec(dllexport) void removeModell();
        //! Returns the polygon with a specific name
        //! \param pos The support vector of the line
        //! \param dir The direction vector of the line
        //! \param polygonName The name of the polygon to calculate the
        //! hit point with \param hitPoint A reference to the variable
        //! where the hit point should be stored \param moveSpeed The
        //! movement speed resulting from the force \param rotSpeed The
        //! rotation speed resulting from the force
        //! \return 1 if a hit point exists
        __declspec(dllexport) bool calcHitPoint(Vertex pos,
            Vertex dir,
            const char* polygonName,
            Vertex& hitpoint,
            Vertex& moveSpeed,
            float& rotSpeed) const;
        //! Splits a specific polygon into two parts
        //! \param pos Start position of the split
        //! \param dir Start direction of the split
        //! \param polygonName The name of the polygon
        //! \param partA A pointer to a Model2DData object to store one
        //! half (output) \param partB A pointer to a Model2DData object to
        //! store the other half (output) \param posA The position of one new
        //! polygon (output) \param posB The position of the other new polygon
        //! (output) \param random A function that returns random values
        __declspec(dllexport) bool split(Vertex pos,
            Vertex dir,
            char* polygonName,
            Polygon2D& partA,
            Polygon2D& partB,
            Point& posA,
            Point& posB,
            std::function<double()> random) const;
        //! Returns the mass of the 2D model (sum of the areas of the
        //! non-transparent polygons)
        __declspec(dllexport) float getMasse() const;
    };

    class Model2DObject : public Object2D
    {
    private:
        Model2DData* rData;
        RCArray<Texture2D>* texture;

    public:
        //! Constructor
        __declspec(dllexport) Model2DObject();
        //! Destructor
        __declspec(dllexport) virtual ~Model2DObject();
        //! Sets the model data
        //! \param mdl The model data
        __declspec(dllexport) void setModel(Model2DData* mdl);
        //! Sets the texture
        //! \param t The image to use as texture
        __declspec(dllexport) void setTexture(Texture2D* t);
        //! Sets the texture
        //! \param t The image to use as texture
        //! \param polygonName The name of the polygon to receive the texture
        __declspec(dllexport) void setTexture(
            Texture2D* t, const char* polygonName);
        __declspec(dllexport) void impuls(
            Vertex start, Vertex speed, float strength = 1.f) override;
        //! Draws the drawing into a specific image
        //! \param zRObj The image to draw into
        __declspec(dllexport) void render(
            Mat3<float>& kamMat, Image& zRObj, const char* kamName) override;
        //! Returns whether a point is inside the model
        //! \param p The point
        //! \param ignoreTransparentFlag if 1, collisions with transparent
        //! polygons are also considered
        __declspec(dllexport) bool isPointInside(
            Vertex p, bool ignoreTransparent = 0) const override;
        //! Checks whether a line is inside the model
        //! \param a The start point of the line
        //! \param b The end point of the line
        //! \param ignoreTransparentFlag if 1, collisions with transparent
        //! polygons are also considered
        __declspec(dllexport) bool isLineInside(
            Vertex a, Vertex b, bool ignoreTransparent = 0) const override;
        //! Checks whether the object intersects with another
        //! \param zObj A pointer to the other object without increased
        //! reference counter \param sp A pointer to a point where the
        //! intersection point is stored \param end 0 if all corners of both
        //! objects should be checked. 1 if only the points of this model should
        //! be searched in the other \param ignoreTransparentFlag if 1,
        //! collisions with transparent polygons are also considered
        __declspec(dllexport) virtual bool isModelInside(const Object2D* zObj,
            Vertex* sp = 0,
            bool end = 0,
            bool ignoreTransparent = 0) const;
        __declspec(dllexport) Rect2<float> getBoundingBox() const override;
        //! Determines the hit point of a ray emitted from pos in the
        //! direction dir. \param pos The support vector of the line
        //! \param dir The direction vector of the line
        //! \param hitPoint A reference to the variable where the
        //! hit point should be stored \return 1 if a hit point exists
        __declspec(dllexport) bool calcHitPoint(
            Vertex pos, Vertex dir, Vertex& hitpoint) const override;
        __declspec(dllexport) float getAirResistance() const override;
        //! Returns the mass of the 2D model (sum of the areas of the
        //! non-transparent polygons)
        __declspec(dllexport) float getMass() const override;
        //! Returns the texture of the first polygon
        __declspec(dllexport) Texture2D* getTexture() const;
        //! Returns the texture of a polygon
        //! \param polygonName The name of the polygon
        __declspec(dllexport) Texture2D* getTexture(
            const char* polygonName) const;
        //! Returns the texture of the first polygon without increased
        //! reference counter
        __declspec(dllexport) Texture2D* zTexture() const;
        //! Returns the texture of a polygon without increased reference
        //! counter \param polygonName The name of the polygon
        __declspec(dllexport) Texture2D* zTexture(
            const char* polygonName) const;
        //! Returns the model data
        __declspec(dllexport) Model2DData* getModel() const;
        //! Returns the model data without increased reference counter
        __declspec(dllexport) Model2DData* zModel() const;
    };

    //! A drawing of a model
    class Model2D : public Drawable
    {
    public:
        class Style : public Drawable::Style
        {
        public:
            static const __int64 Texture
                = 0x8; //! If this flag is set, a texture is used when drawing
            static const __int64 Border
                = 0x10; //! If this flag is set, the polygon borders are drawn
            static const __int64 Alpha = 0x40; //! If this flag is set, alpha
                                               //! blending is used when drawing
            static const __int64 Mesh
                = 0x20; //! If this flag is set, the triangle borders are drawn
        };

    private:
        Model2DData* rData;
        float rotation;
        float size;
        int color;
        RCArray<Texture2D>* textur;

    public:
        //! Constructor
        __declspec(dllexport) Model2D();
        //! Destructor
        __declspec(dllexport) virtual ~Model2D();
        //! Sets the model data
        //! \param mdl The model data
        __declspec(dllexport) void setModel(Model2DData* mdl);
        //! Sets the counter-clockwise rotation of the model
        //! \param rotation The angle in radians
        __declspec(dllexport) void setRotation(float rotation);
        //! Adds to the current rotation angle
        //! \param rotation The angle in radians to add
        __declspec(dllexport) void addRotation(float rotation);
        //! Sets the scaling of the model
        //! \param size The scaling factor
        __declspec(dllexport) void setSize(float size);
        //! Adds a value to the scaling
        //! \param size The value to add to the scaling
        __declspec(dllexport) void addSize(float size);
        //! Sets the texture
        //! \param t The image to use as texture
        __declspec(dllexport) void setTexture(Texture2D* t);
        //! Sets the texture
        //! \param t The image to use as texture
        //! \param polygonName The name of the polygon to receive the texture
        __declspec(dllexport) void setTexture(
            Texture2D* t, const char* polygonName);
        //! Sets the color
        //! \param f The color in A8R8G8B8 format
        __declspec(dllexport) void setColor(int f);
        //! Processes the time elapsed since the last call of this function
        //! \param tickVal The elapsed time in seconds
        __declspec(dllexport) bool tick(double tickVal) override;
        //! Draws the drawing into a specific image
        //! \param zRObj The image to draw into
        __declspec(dllexport) void render(Image& zRObj) override;
        //! Returns the rotation of the model
        __declspec(dllexport) float getRotation() const;
        //! Returns the scaling factor
        __declspec(dllexport) float getSize() const;
        //! Returns whether a point is inside the model
        //! \param p The point
        __declspec(dllexport) bool isPointInside(int x, int y) const override;
        //! Returns whether a point is inside the model
        //! \param p The point
        __declspec(dllexport) bool isPointInside(Vertex p) const;
        //! Checks whether a line is inside the model
        //! \param a The start point of the line
        //! \param b The end point of the line
        __declspec(dllexport) bool isLineInside(Vertex a, Vertex b) const;
        //! Checks whether the model intersects with another
        //! \param zMdl A pointer to the other model without increased reference
        //! counter \param end 0 if all corners of both models should be
        //! checked. 1 if only the points of this model should be searched in
        //! the other
        __declspec(dllexport) bool isModelInside(
            const Model2D* zMdl, bool end = 0) const;
        //! Returns the model data
        __declspec(dllexport) Model2DData* getModel() const;
        //! Returns the model data without increased reference counter
        __declspec(dllexport) Model2DData* zModel() const;
    };
} // namespace Framework
#endif