
/**
 * The Shovel class uses a specific type of queue made specifically for the Shovel class, the ShovelQueue
 * 
 * @author Ahmed Messaoud
 * @see ShovelQueue
 */
public class Shovel extends Resource {
	private ShovelQueue q;

	public Shovel(String name) {
		super(name);
		q = new ShovelQueue();
	}
	
	/**
	 * The goTo method is used to advise the given Truck to come to this Shovel 
	 * 
	 * @param t The Truck to come to this Shovel
	 * @return	Wether the Truck was successfully added to the Shovel's queue
	 * @see Truck#finishedCrushing()
	 */
	public boolean goTo(Truck t){
		return q.add(t);
	}

	@Override
	public void run() {
		while(this.getContinueRunning()){
			Truck t = q.poll();

			if( t != null ){
				if( this.isIdleling() ){
					try{
						this.stopIdleling();
					}catch( NotIdlelingException e ){ e.printStackTrace(); }
				}
				try{
					//Truck was in the queue
					if( t.getTruckSize() == TruckSize.FiftyTons )
						sleep(getSleepTime(10, 2));
					else
						sleep(getSleepTime(5, 1));
				}catch(InterruptedException e){ e.printStackTrace(); }
				//Finished the shoveling operation, advise the truck
				t.finishedShovel();
			}else if( !this.isIdleling() && t == null ){
				try{
					this.startIdleling();
				}catch( AlreadyIdlelingException e ){ e.printStackTrace(); }
			}
		}
	}

}
