#pragma once
#include "SpacialHashing.h"
#include "Wire.h"
#include "Node.h"
#include <map>
#include <vector>

using namespace std;

class CRouter
{
private:
	CSpacialHashing *m_pSpaceHash;
	unsigned char *m_costBuffer;
	int m_maxX;
	int m_maxY;

	CNode *m_destNode;
	CNode *m_startNode;

	static const int m_CostFree = 0;
	static const int m_CostCorner = 20;
	static const int m_CostCross = 60;
	static const int m_CostParallel = 1000;
	static const int m_CostCrossCross = 1000;
	static const int m_CostSolid = 10000000;
private:
	int GetCost(CPoint point, int direction, int prevDirection);
	int GetAdjacent(CPoint point, CPoint prevPoint, vector<CPoint> &points, vector<int> &costs);
	static int EstimateCost(CPoint from, CPoint to);
	bool DoRoute(AnchorList &anchors, bool limitFrame, bool reverse);
	static bool CompareWires(CWire *w1, CWire *w2);
	bool OrderNodes(CNode **start, CNode **end);
public:
	CRouter(CSpacialHashing *spaceHash);
	bool Route(CNode *fromNode, CNode *toNode, AnchorList &anchors);
	CWire * RouteWire(CNode *from, CNode *to, AnchorList &anchors);
	void RerouteWires(vector<CWire *> wires);
};

class CRoutingNode
{
private:
	CPoint m_Point;
	CRoutingNode *m_pParentNode;
	int m_OrderValue;
	int m_Cost;
	int m_Heuristic;
public:
	CPoint GetPoint();
	int GetCost();
	CRoutingNode *GetParentNode();
	CRoutingNode(CPoint point, CRoutingNode *parent, int cost, int heuristic);
	static bool compare(CRoutingNode *left, CRoutingNode *right)
	{
		return (left->m_OrderValue > right->m_OrderValue);
	}

	//bool operator < (const CRoutingNode & right) const;
//	friend bool operator <(const CRoutingNode &left, const CRoutingNode &right);

	int id;
};