import java.util.*;

/**
 * 
 * @author Ahmed Messaoud
 *
 */
public class Truck  extends Resource implements Comparable<Truck>{
	TruckSize size;
	Shovel s;
	Crusher c;
	
	/**
	 * @param name	The name of the Truck
	 * @param size	The size of the Truck, can either be Twenty of Fifty Tons
	 * @param s	The Shovel to be used by the truck
	 * @param c	The Crusher to be used by the truck
	 */
	public Truck(String name, TruckSize size, Shovel s, Crusher c){
		super(name);
		this.size = size;
		this.s = s;
		this.c = c;
	}
	
	/**
	 * the finishedCrushing method is called by the Crusher class, to advise the Truck that the Crushing has been completed.
	 * The Truck goes back to its assigned Shovel
	 */
	public void finishedCrushing(){
		//go to the shovel
		System.out.println(this.getResourceName() + " finished Crushing heading to the Shovel.");
		try{
			if( size == TruckSize.FiftyTons )
				sleep(getSleepTime(2, 0));
			else
				sleep(getSleepTime(1.5, 0));
		}catch(InterruptedException e){ e.printStackTrace(); }
		//Goto the assigned shovel for this truck
		s.goTo(this);
	}
	
	/**
	 * the finishedShovel method is called by the Shovel class to advise the Truck that the Shovelling has been completed
	 * The Truck goes to the assigned Crusher
	 */
	public void finishedShovel(){
		//go to the crusher
		System.out.println(this.getResourceName() + " is loaded heading to the Crusher.");
		try{
			if( size == TruckSize.FiftyTons )
				sleep(getSleepTime(3, 0));
			else
				sleep(getSleepTime(2.5, 0));
		}catch(InterruptedException e){ e.printStackTrace(); }
		//Goto the assigned crusher for this truck
		c.goTo(this);
	}
	
	public void run(){
		//start by going to the assigned shovel
		s.goTo(this);
	}
	
	/* (non-Javadoc)
	 * @see Resource#setContinueRunning(boolean)
	 */
	public void setContinueRunning(boolean b){
		//make sure truck stops idleling
		if( this.isIdleling() ){
			try{
				this.stopIdleling();
			}catch( NotIdlelingException e){}
		}
	}
	
	/**
	 * @return The Truck size representing the size of the truck
	 * @see TruckSize
	 */
	public TruckSize getTruckSize(){
		return size;
	}
	
	/**
	 * @return The associated Shovel with the Truck
	 * @see Shovel
	 */
	public Shovel getShovel(){
		return s;
	}
	
	/**
	 * @return The associated Crusher with the Truck
	 * @see Crusher
	 */
	public Crusher getCrusher(){
		return c;
	}

	/* (non-Javadoc)
	 * Used for returning normal ordering of Trucks based on the TruckSize (TASK 1) or specific Shovel (TASK 2)
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 * @see TruckSize
	 * @see CrusherQueue
	 */
	public int compareTo(Truck t) {
		//TASK 1 - Allow Fifty tons priority to the crusher
		if( size == t.getTruckSize() )
			return 0;
		else if( size == TruckSize.FiftyTons )
			return -1;
		else
			return 1;
		 
		//TASK 2 - Allow trucks from Shovel 1 priority
//		if( this.s.getResourceName() == t.getShovel().getResourceName() )
//			return 0;
//		else if( this.s.getResourceName() == "Shovel 1" )
//			return -1;
//		else
//			return 1;
	}
}
