using UnityEngine;
using System.Collections;

public class DeikstraSystem : MonoBehaviour {

	public DeikstraType Type;
	public Waypoint[] Waypoints;
	public Edge[] Edges;

	
	private int t;
	private float min=0;
	// Use this for initialization
	void Start () {
		
		for (int i = 0;i<Waypoints.Length;i++) {
			CreateWaypoint(i);
		}
		
		for (int n = 0;n<Edges.Length;n++) {
			CreateEdge(n);
		}	
		
		
	}
	
	// Update is called once per frame
	void Update () {
	}
	
	
	
	
	
	
	void CreateWaypoint(int number) {
		Waypoints[number].mesh.parent = Waypoints[number].mesh.transform;
	}
	
	
	void CreateEdge(int number) {
			Edges[number].beginV = Waypoints[Edges[number].begin].mesh.position;
			Edges[number].endV = Waypoints[Edges[number].end].mesh.position;
			Edges[number].dist = Vector3.Distance(Edges[number].beginV,Edges[number].endV);

	}
	
	void DebugEdges() {
		for (int n = 0;n<Edges.Length;n++) {
			Debug.DrawLine(Edges[n].beginV,Edges[n].endV);	
		}
	}
	
	void DebugWaypoints() {
		for (int i = 0;i<Waypoints.Length;i++) {
			Vector3 scr = Camera.mainCamera.WorldToScreenPoint(Waypoints[i].mesh.position);
			GUI.Label(new Rect(scr.x,scr.y,100,100),""+Waypoints[i].id);
		}
	}
	
	
	public int Nearest(Vector3 pos) {
		int needed=0; 
		float lastDist=999999;
		for (int i = 0;i<Waypoints.Length;i++) {
				if (Vector3.Distance(pos,Waypoints[i].mesh.position)<lastDist) {lastDist =Vector3.Distance(pos,Waypoints[i].mesh.position); needed = i;}
		}
		return needed;
		 needed=0; 
		 lastDist=999999;
	}
	
	public int[] SmartMoveTo(Vector3 start, Vector3 stop) {
		Restart();
		t=0;
		int begin = Nearest(start);
		int end = Nearest(stop);
		int WayP = begin;
		Waypoints[WayP].weight = 0;
		while (t!=-1) {
		for (int y=0;y<Waypoints.Length;y++) 
			{
				for(int x=0;x<Edges.Length;x++) 
				{
					if ((Edges[x].begin==WayP || Edges[x].end == WayP) & (Edges[x].begin==Waypoints[y].id || Edges[x].end == Waypoints[y].id ) & (y!=WayP) & (Waypoints[y].weight>Edges[x].dist + Waypoints[WayP].weight)) 
					{
							Waypoints[y].weight=Edges[x].dist + Waypoints[WayP].weight;
							Waypoints[y].Predok = WayP;
					}
				}
			}
		Waypoints[WayP].IsCheck  =true;
		min=30000;
		t=-1;
		for (int b=0;b<Waypoints.Length;b++) 
			{
			if (Waypoints[b].weight<min & Waypoints[b].IsCheck==false) 
				{
					min = Waypoints[b].weight;
					t=b;
					WayP = b;
				}
			}
		}
	int LastWayp=end;
	int many=1;
	while(Waypoints[LastWayp].Predok!=-1) {
			many++;
			LastWayp = Waypoints[LastWayp].Predok;
	}
	int[] walkPath = new int[many];
	
	for(int g=0;g<many;g++) {
	walkPath[g]=0;}
		
	LastWayp=end;
	int r=0;
	walkPath[r] = LastWayp;
	while(Waypoints[LastWayp].Predok!=-1) {
			r++;
			LastWayp = Waypoints[LastWayp].Predok;
			walkPath[r]  = LastWayp;	
	}
	//for(int h=0;h<many;h++) {
	Restart();
	return walkPath;
	}
	
	void DebugDeikstra(int start, int stop) { 
		
		int begin = start;
		int end = stop;
		int WayP = begin;
		Waypoints[WayP].weight = 0;
		while (t!=-1) {
		for (int y=0;y<Waypoints.Length;y++) {
			for(int x=0;x<Edges.Length;x++) {
				if ((Edges[x].begin==WayP || Edges[x].end == WayP) & (Edges[x].begin==Waypoints[y].id || Edges[x].end == Waypoints[y].id ) & (y!=WayP) & (Waypoints[y].weight>Edges[x].dist + Waypoints[WayP].weight)) {
						Waypoints[y].weight=Edges[x].dist + Waypoints[WayP].weight;
						Waypoints[y].Predok = WayP;
				}
			}
		}
		Waypoints[WayP].IsCheck  =true;
		min=30000;
		t=-1;
		for (int b=0;b<Waypoints.Length;b++) {
			if (Waypoints[b].weight<min & Waypoints[b].IsCheck==false) {
				min = Waypoints[b].weight;
				t=b;
				WayP = b;
			}
		} 
		}
	}
	
	void Restart() {
		for (int u=0;u<Waypoints.Length;u++) {
			Waypoints[u].IsCheck = false;
			Waypoints[u].weight = 99999;
			Waypoints[u].Predok = -1;
		}
	}
}

public enum DeikstraType{
	normal,
	line
}
