/****************************************************************************/
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
// Copyright (C) 2012-2026 German Aerospace Center (DLR) and others.
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0/
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License 2.0 are satisfied: GNU General Public License, version 2
// or later which is available at
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
/****************************************************************************/
/// @file    NBAlgorithms.h
/// @author  Daniel Krajzewicz
/// @author  Jakob Erdmann
/// @date    02. March 2012
///
// Algorithms for network computation
/****************************************************************************/
#pragma once
#include <config.h>

#include <map>
#include "NBEdgeCont.h"
#include "NBNodeCont.h"

// ===========================================================================
// class declarations
// ===========================================================================
class NBEdge;
class NBNode;

// ===========================================================================
// class definitions
// ===========================================================================
// ---------------------------------------------------------------------------
// NBTurningDirectionsComputer
// ---------------------------------------------------------------------------
/* @class NBTurningDirectionsComputer
 * @brief Computes turnaround destinations for all edges (if exist)
 */
class NBTurningDirectionsComputer {
public:
    /** @brief Computes turnaround destinations for all edges (if exist)
     * @param[in] nc The container of nodes to loop along
     * @param[in] warn Whether warnings shall be issued
     */
    static void computeTurnDirections(NBNodeCont& nc, bool warn = true);

    /** @brief Computes turnaround destinations for all incoming edges of the given nodes (if any)
     * @param[in] node The node for which to compute turnaround destinations
     * @param[in] warn Whether warnings shall be issued
     * @note: This is needed by netedit
     */
    static void computeTurnDirectionsForNode(NBNode* node, bool warn);

    /// @brief compute angle to junction at a point further away
    static double getFarAngleAtNode(const NBEdge* e, const NBNode* n, double dist = 50);

private:
    /** @struct Combination
     * @brief Stores the information about the angle between an incoming ("from") and an outgoing ("to") edge
     *
     * Note that the angle is increased by 360 if the edges connect the same two nodes in
     *  opposite direction.
     */
    struct Combination {
        NBEdge* from;
        NBEdge* to;
        double angle;
    };


    /** @class combination_by_angle_sorter
     * @brief Sorts "Combination"s by decreasing angle
     */
    class combination_by_angle_sorter {
    public:
        explicit combination_by_angle_sorter() { }
        int operator()(const Combination& c1, const Combination& c2) const {
            if (c1.angle != c2.angle) {
                return c1.angle > c2.angle;
            }
            if (c1.from != c2.from) {
                if (c1.to == c2.to && c1.from->getPermissions() != c2.from->getPermissions()) {
                    if (c1.from->getPermissions() == c1.to->getPermissions()) {
                        return true;
                    } else if (c2.from->getPermissions() == c1.to->getPermissions()) {
                        return false;
                    }
                }
                return c1.from->getID() < c2.from->getID();
            }
            if (c1.to->getPermissions() != c2.to->getPermissions()) {
                if (c1.to->getPermissions() == c1.from->getPermissions()) {
                    return true;
                } else if (c2.to->getPermissions() == c1.from->getPermissions()) {
                    return false;
                }
            }
            return c1.to->getID() < c2.to->getID();
        }
    };
};



// ---------------------------------------------------------------------------
// NBNodesEdgesSorter
// ---------------------------------------------------------------------------
/* @class NBNodesEdgesSorter
 * @brief Sorts a node's edges clockwise regarding driving direction
 */
class NBNodesEdgesSorter {
public:
    /** @brief Sorts a node's edges clockwise regarding driving direction
     * @param[in] nc The container of nodes to loop along
     * @param[in] useNodeShape Whether to sort based on the node shape (instead of only the edge angle)
     */
    static void sortNodesEdges(NBNodeCont& nc, bool useNodeShape = false);

    /** @class crossing_by_junction_angle_sorter
     * @brief Sorts crossings by minimum clockwise clockwise edge angle. Use the
     * ordering found in myAllEdges of the given node
     */
    class crossing_by_junction_angle_sorter {
    public:
        explicit crossing_by_junction_angle_sorter(const NBNode* node, const EdgeVector& ordering);

        int operator()(const std::unique_ptr<NBNode::Crossing>& c1, const std::unique_ptr<NBNode::Crossing>& c2) const {
            const int r1 = getMinRank(c1->edges);
            const int r2 = getMinRank(c2->edges);
            if (r1 == r2) {
                return c1->edges.size() > c2->edges.size();
            } else {
                return (int)(r1 < r2);
            }
        }

    private:
        /// @brief retrieves the minimum index in myAllEdges
        int getMinRank(const EdgeVector& e) const {
            int result = (int)myOrdering.size();
            for (EdgeVector::const_iterator it = e.begin(); it != e.end(); ++it) {
                int rank = (int)std::distance(myOrdering.begin(), std::find(myOrdering.begin(), myOrdering.end(), *it));
                result = MIN2(result, rank);
            }
            return result;
        }

    private:
        EdgeVector myOrdering;
    };

    /** @brief Assures correct order for same-angle opposite-direction edges
     * @param[in] n The currently processed node
     * @param[in] i1 Pointer to first edge
     * @param[in] i2 Pointer to second edge
     */
    static void swapWhenReversed(const NBNode* const n,
                                 const std::vector<NBEdge*>::iterator& i1,
                                 const std::vector<NBEdge*>::iterator& i2);


    /** @class edge_by_junction_angle_sorter
     * @brief Sorts incoming and outgoing edges clockwise around the given node
     */
    class edge_by_junction_angle_sorter {
    public:
        explicit edge_by_junction_angle_sorter(NBNode* n) : myNode(n) {}
        int operator()(NBEdge* e1, NBEdge* e2) const {
            const double a1 = e1->getAngleAtNodeNormalized(myNode);
            const double a2 = e2->getAngleAtNodeNormalized(myNode);
            return a1 < a2 || (a1 == a2 && e1->getNumericalID() < e2->getNumericalID());
        }

    private:
        /// @brief The node to compute the relative angle of
        NBNode* myNode;

    };

};



// ---------------------------------------------------------------------------
// NBNodeTypeComputer
// ---------------------------------------------------------------------------
/* @class NBNodeTypeComputer
 * @brief Computes node types
 */
class NBNodeTypeComputer {
public:
    /** @brief Computes node types
     * @param[in] nc The container of nodes to loop along
     */
    static void computeNodeTypes(NBNodeCont& nc, NBTrafficLightLogicCont& tlc);

    /** @brief Checks rail_crossing for validity
     * @param[in] nc The container of nodes to loop along
     */
    static void validateRailCrossings(NBNodeCont& nc, NBTrafficLightLogicCont& tlc);

    /// @brief whether the given node only has rail edges
    static bool isRailwayNode(const NBNode* n);
};



// ---------------------------------------------------------------------------
// NBEdgePriorityComputer
// ---------------------------------------------------------------------------
/* @class NBEdgePriorityComputer
 * @brief Computes edge priorities within a node
 */
class NBEdgePriorityComputer {
public:
    /** @brief Computes edge priorities within a node
     * @param[in] nc The container of nodes to loop along
     */
    static void computeEdgePriorities(NBNodeCont& nc);

private:
    /** @brief Sets the priorites in case of a priority junction
     * @param[in] n The node to set edges' priorities
     */
    static void setPriorityJunctionPriorities(NBNode& n, bool forceStraight = false);

    /// @brief score pair of edges for multi-criteria evaluatoin of angle, priority, laneNumber and speed
    static double getScore(const NBEdge* e1, const NBEdge* e2, int minPrio, int maxPrio, int maxNumLanes, double maxSpeed, SVCPermissions permissions);

    /// @brief set priority for edges that are parallel to the best edges
    static void markBestParallel(const NBNode& n, NBEdge* bestFirst, NBEdge* bestSecond);

    /** @brief Sets the priorites in case of a priority junction
     * @param[in] n The node to set edges' priorities
     * @param[in] s The vector of edges to get and mark the first from
     * @param[in] prio The priority to assign
     * @return The vector's first edge
     */
    static NBEdge* extractAndMarkFirst(NBNode& n, std::vector<NBEdge*>& s, int prio = 1);

    /** @brief Returns whether both edges have the same priority
     * @param[in] e1 The first edge
     * @param[in] e2 The second edge
     * Whether both edges have the same priority
     */
    static bool samePriority(const NBEdge* const e1, const NBEdge* const e2, SVCPermissions permissions);

    /// @brief return whether the priorite attribute can be used to distinguish the edges
    static bool hasDifferentPriorities(const EdgeVector& edges, const NBEdge* excluded);

};
