#ifndef TriangleList_H
#define TriangleList_H

#include "Array.h"
#include "Point.h"

namespace Framework
{
    template<typename T>
    //! A corner of a triangle
    struct TriangleVertex
    {
        T* point;
        Vec2<float>* texture;

        //! Constructor
        //! \param point The coordinate of the corner
        //! \param texture The coordinate in the texture
        TriangleVertex(T* point, Vec2<float>* texture)
        {
            this->point = point;
            this->texture = texture;
        }

        //! Destructor
        ~TriangleVertex()
        {
            delete point;
            delete texture;
        }
    };

    template<typename T>
    //! A list of triangles where the last two points of the previous triangle
    //! together with the next point form a new triangle
    class TriangleList : public virtual ReferenceCounter
    {
    private:
        Array<TriangleVertex<T>*>* points;

    public:
        //! Constructor
        TriangleList()
            : ReferenceCounter()
        {
            points = new Array<TriangleVertex<T>*>();
        }

        //! Destructor
        ~TriangleList()
        {
            int anz = points->getEntryCount();
            for (int i = 0; i < anz; i++)
                delete points->get(i);
            points->release();
        }

        //! Adds a point to the list
        //! \param p The coordinates of the point
        //! \param texture The coordinates in the texture
        void addPoint(T* p, Vec2<float>* texture)
        {
            points->add(new TriangleVertex<T>(p, texture));
        }

        //! Deletes the last point
        void removeLetztenPunkt()
        {
            int i = points->getEntryCount() - 1;
            if (!points->has(i)) return;
            delete points->get(i);
            points->remove(i);
        }

        //! Deletes all corners
        void lehren()
        {
            int anz = points->getEntryCount();
            for (int i = 0; i < anz; i++)
                delete points->get(i);
            points->clear();
        }

        //! Returns the number of triangles
        int getTriangleCount() const
        {
            return points->getEntryCount() - 2;
        }

        //! Returns whether a texture is used
        bool hasTexture() const
        {
            int anz = points->getEntryCount();
            bool ret = 1;
            for (int i = 0; i < anz; i++)
            {
                if (points->has(i)) ret &= points->get(i)->textur;
            }
            return ret;
        }

        //! Returns the list of points
        Array<TriangleVertex<T>*>* zListe() const
        {
            return points;
        }
    };
} // namespace Framework

#endif