import java.awt.*;
import java.util.*;

public class SolutionList
{
	String name;
	int cost;
	SolutionList next;

	public SolutionList( String NAME, int COST )
	{
		name = NAME;
		cost = COST;
		next = null;
	}
	
	public SolutionList lastNode()
	{
		SolutionList tempSolution = this;
		while ( tempSolution.next != null )
			tempSolution = tempSolution.next;
		return tempSolution;

	}

	public void addNode( String NAME)
	{ //Adds a node to the end of a list
		SolutionList endOfSolutionList = this.lastNode();
		SolutionList newSolution = new SolutionList( NAME, 0 );
		endOfSolutionList.next = newSolution;
		
	}

	public void addNode( String NAME, int COST)
	{ //Adds a node to the end of a list
		SolutionList endOfSolutionList = this.lastNode();
		SolutionList newSolution = new SolutionList( NAME, COST );
		endOfSolutionList.next = newSolution;
		
	}

	public boolean findNode( String s)
	{
		SolutionList TempSolutionList = this;
		if ( TempSolutionList == null ) return false;
		while( TempSolutionList != null )
		{
			if (TempSolutionList.name.equalsIgnoreCase( s ))
				return true;
			else
				TempSolutionList = TempSolutionList.next;
		}
		return false;
			
	}

	public int TotalCost()
	{
		int Total = 0;
		SolutionList SolutionListToTotal = this;
				
		while( SolutionListToTotal != null ){
			Total += SolutionListToTotal.cost;
			SolutionListToTotal = SolutionListToTotal.next;
		}
		if ( Total == 0 ){
			return 9999;
		}
		else{
			return Total;
		}
	}

	public void print( Graphics g )
	{
		SolutionList tempSolutionList = this;
		int yPos = 20;
		if ( tempSolutionList != null ){
			tempSolutionList = tempSolutionList.next;
		}
		while ( tempSolutionList != null ){
			g.drawString( tempSolutionList.name + "=" + Integer.toString( tempSolutionList.cost ), 0, yPos);
			tempSolutionList = tempSolutionList.next;
			yPos += 10;
		}
		g.drawString( Integer.toString( this.TotalCost() ), 0, yPos);
	}

	public SolutionList copy() {
		SolutionList newSolutionList = new SolutionList( this.name, this.cost );
		if ( this.next == null ){
			newSolutionList.next = null;
		}
		else{
			newSolutionList.next = this.next.copy();
		}
		return newSolutionList;
	}

	public void drawlist( Graphics g , MapList MAPHEAD)
	{
		int yPos = 10;
		if ( !g.getColor().equals( Color.black ) ){
			Color tempColor = new Color( g.getColor().getRGB() );
			g.setColor( Color.white );
			g.fillRect( 350, 0, 400, 400 );
			g.setColor( tempColor );
		} 
		MapList MapNodeToDrawFrom;
		MapList MapNodeToDrawTo;
		SolutionList tempSolutionList = this;
		tempSolutionList = tempSolutionList.next;
		while( tempSolutionList.next != null ){
			if ( !g.getColor().equals( Color.black ) ){
				g.drawString ("FROM:" + tempSolutionList.name + " TO:" + tempSolutionList.next.name, 350, yPos );
			}
			MapNodeToDrawFrom = MAPHEAD.findMapNode( tempSolutionList.name );
			MapNodeToDrawTo = MAPHEAD.findMapNode( tempSolutionList.next.name );
			g.drawLine( MapNodeToDrawFrom.xPos + 15, MapNodeToDrawFrom.yPos + 15, MapNodeToDrawTo.xPos + 15, MapNodeToDrawTo.yPos + 15 );
			tempSolutionList = tempSolutionList.next;
			wait( 400 );
			yPos += 10;
			
		}

		if ( g.getColor().equals( Color.red ) ){
			g.drawString ("NODE ALREADY VISITED", 350, yPos );
		}
		if ( g.getColor().equals( Color.green ) ){
			g.drawString ("DESTINATION COST " + this.TotalCost(), 350, yPos );
		}
	}

	public void wait( int milliSecondsToWait )
	{
	 	long WaitUntilTime = new Date().getTime();
		WaitUntilTime += milliSecondsToWait;
		while ( new Date().getTime() < WaitUntilTime ){

		}

	}

}