#pragma once

#include <QLineF>
#include <QPointF>
#include <QPolygonF>
#include <QtMath>
#include <QLineF>

#include "PolygonCalculus.h"

class Circle;

namespace PlanimetryCalculus {

    enum IntersectType{InsideNoIntersection, InsideTouching, InsideIntersection,
                       OutsideIntersection, OutsideTouching, OutsideNoIntersection,
                       CirclesEqual, //Circle Circle intersection

                       Tangent, Secant, // Circle Line Intersetion

                       EdgeCornerIntersection, EdgeEdgeIntersection, CornerCornerIntersection,
                       LinesParallel, LinesEqual, // Line Line intersection

                       Interior, // Polygon contains

                       NoIntersection, Error // general
                       };

    typedef QList<QPair<int, int>> NeighbourList;
    typedef QList<QPointF> QPointFList;
    typedef QList<IntersectType> IntersectList;

    void rotateReference(QPointF &point, double alpha);
    void rotateReference(QPointFList &points, double alpha);
    void rotateReference(QLineF &line, double alpha);
    //void rotateReference(QPolygonF &polygon, double alpha);

    QPointF     rotateReturn(QPointF point, double alpha);
    QPointFList rotateReturn(QPointFList points, double alpha);
    QLineF      rotateReturn(QLineF line, double alpha);
    //QPolygonF       rotateReturn(QPolygonF &polygon, double alpha);

    bool intersects(const Circle &circle1, const Circle &circle2);
    bool intersects(const Circle &circle1, const Circle &circle2, IntersectType &type);
    bool intersects(const Circle &circle1, const Circle &circle2, QPointFList &intersectionPoints);
    bool intersects(const Circle &circle1, const Circle &circle2, QPointFList &intersectionPoints, IntersectType &type);

    bool intersects(const Circle &circle, const QLineF &line);
    bool intersects(const Circle &circle, const QLineF &line, IntersectType &type);
    bool intersects(const Circle &circle, const QLineF &line, QPointFList &intersectionPoints);
    bool intersects(const Circle &circle, const QLineF &line, QPointFList &intersectionPoints, IntersectType &type);

    bool intersects(const Circle &circle, const QPolygonF &polygon);
    bool intersects(const Circle &circle, const QPolygonF &polygon, QList<QPointFList> &intersectionPoints);
    bool intersects(const Circle &circle, const QPolygonF &polygon, QList<QPointFList> &intersectionPoints, IntersectList &typeList);
    bool intersects(const Circle &circle, const QPolygonF &polygon, QList<QPointFList> &intersectionPoints, NeighbourList &neighbourList);
    bool intersects(const Circle &circle, const QPolygonF &polygon, QList<QPointFList> &intersectionPoints, NeighbourList &neighbourList, IntersectList &typeList);

    bool intersects(const QLineF &line1, const QLineF &line2);
    bool intersects(const QLineF &line1, const QLineF &line2, QPointF &intersectionPt);
    bool intersects(const QLineF &line1, const QLineF &line2, QPointF &intersectionPt, IntersectType &type);
    bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList);
    bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList,  IntersectList &typeList);
    bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList,  NeighbourList &neighbourList);
    bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList,  NeighbourList &neighbourList, IntersectList &typeList);

    bool contains(const QLineF &line, const QPointF &point);
    bool contains(const QLineF &line, const QPointF &point, IntersectType &type);
    bool contains(const QPolygonF &polygon, const QPointF &point);
    bool contains(const QPolygonF &polygon, const QPointF &point, IntersectType &type);


    double distance(const QPointF &p1, const QPointF p2);
    double norm(double x, double y);
    double norm(const QPointF &p);
    double angle(const QPointF &p1, const QPointF p2);
    double angle(const QPointF &p1);
    double angle(const QLineF &line);
    double angleDegree(const QPointF &p1, const QPointF p2);
    double truncateAngle(double angle);
    double truncateAngleDegree(double angle);

    /*!
     * \fntemplate <typename T> int signum(T val)
     * Returns the signum of a value \a val.
     *
     * \sa QPair, QList
     */
    template <typename T> int signum(T val)
    {
        return (T(0) < val) - (val < T(0));
    }
}



