/* Program MiningTrucks
 * Description: Trucks must go to the shovel, get some ore, then go to the
 * crusher and dunp them, then go back in line at the shovel.  Each truck
 * is dedicated to one shovel.  There are two types of trucks, 20T and 50T
 * trucks.
 */

package hw4_multithreadtrucks;
import java.util.*;
import pbxlogique.Pbx;
/**
 *
 * @author Peilos
 */
public class Main {
    static private final long howLongToRunProgramForInMilli = 300000;
    static private final boolean bPrioritize = false; //PRIORITY SYSTEM ON?
    
    static private Vector vTruckList; //List of Truckers
    static private Vector vShovelList;//List of Shovels
    static private Crusher oTheCrusher;//The one and only Crusher!
    static private TimeCal myTimer;//Timer helper!
    
    static private Shovel oCurrentShovel; //Helper variable
    static private Truck oCurrentTruck; //Helper variable
    
    static private int iHelp;
    
    @SuppressWarnings("empty-statement")
    public static void main(String[] args) 
    {
        myTimer = new TimeCal();
        oTheCrusher = new Crusher();//Crusher!
        if (bPrioritize) oTheCrusher.useHighPriority(true, 1);// *********PRIORITY SETTING HERE
        oTheCrusher.setCrusherName("Ace");
        //Loading of the Shovel List
        vShovelList = new Vector();
        vShovelList.add(new Shovel(1));//Shovel 1
        myTimer.RunForSomeTime(500);
        vShovelList.add(new Shovel(2));//Shovel 2
        myTimer.RunForSomeTime(500);
        vShovelList.add(new Shovel(3));//Shovel 3
        myTimer.RunForSomeTime(500);
        vTruckList = new Vector();
        //Loading of the Truckers
        //Create new trucks
        vTruckList.add(new Truck(1,20,"Ryan Clemence"));
        vTruckList.add(new Truck(1,20,"Dimitri Berlokovitz"));
        vTruckList.add(new Truck(1,20,"Jean-Pierre LaMalice"));
        vTruckList.add(new Truck(2,50,"Ronald Hochobar"));
        vTruckList.add(new Truck(2,50,"Julie Lafontaine"));
        vTruckList.add(new Truck(2,50,"Big John"));
        vTruckList.add(new Truck(3,20,"Scout"));
        vTruckList.add(new Truck(3,50,"Ruby"));
        vTruckList.add(new Truck(3,50,"Pyro"));
        vTruckList.add(new Truck()); //This is just a joke, the trucker created 
        //here (N00by McNoob) will get fired and his truck will not go 
        //anywhere... and the thread will stop.
        myTimer.RunForSomeTime(2000);
        //We must now assign each truck to a crusher and a shovel.
        for (int i=0; i<9;i++)
        {
            oCurrentTruck = (Truck) vTruckList.elementAt(i);
            iHelp = oCurrentTruck.getShovelNumber();
            oCurrentShovel = (Shovel) vShovelList.get(iHelp-1);
            oCurrentTruck.attachCrusher(oTheCrusher);
            oCurrentTruck.attachShovel(oCurrentShovel);
        }
        //We will now activate teh crusher and the shovels...
        oTheCrusher.start();
        ( (Shovel) vShovelList.get(0)).start();
        ( (Shovel) vShovelList.get(1)).start();
        ( (Shovel) vShovelList.get(2)).start();
        new TimeCal().RunForSomeTime(2000); //Just to add an..je ne sais quoi effect!
        /* Mark approximately where the main timer starts, we want to display it
         * on screen before the trucks start spewing messagaes...
         * */
        System.out.println("-----====={ TIMER STARTING }=====-----");
        System.out.println("-----Timer set: "+Pbx.antiDecimal(howLongToRunProgramForInMilli/1000,0)+" minutes\t -----");
        if (bPrioritize) 
        {
            System.out.println("-----Priority System: ON         -----");
        }else
        {
            System.out.println("-----Priority System: OFF        -----");
        }
        System.out.println("-----=====~~~~~~~~~~~~~~~~~~=====-----");
        //Time to give the marshing orders to the trucks!
        for (int i=0; i<10; i++)
        {
            oCurrentTruck = (Truck) vTruckList.elementAt(i);
            oCurrentTruck.start();
        }
        //Delay function before shutdown procedure...
        myTimer.RunForSomeTime(howLongToRunProgramForInMilli);
        //Shutdown process start
        for (int i=0; i<9;i++)
        {
            oCurrentTruck = (Truck) vTruckList.elementAt(i);
            oCurrentTruck.jobFinished();
        }
        for (int i=0; i<9;i++) //Wait for truck instances to shutdown one by one
        {
            oCurrentTruck = (Truck) vTruckList.elementAt(i);
            while (oCurrentTruck.isAlive());
        }
        //Shut down shovels.
        for (int i=0; i<3; i++)//Shutdown Shovels
        {
            oCurrentShovel = (Shovel) vShovelList.get(i);
            oCurrentShovel.shutDownShovel();
        }
        oTheCrusher.shutDownCrusher();//Shutdown Crusher
        for (int i=0; i<3; i++) //Wait for Shovels to shutdown
        {
            oCurrentShovel = (Shovel) vShovelList.get(i);
            while (oCurrentShovel.isAlive());
        }//END FOR
        while (oTheCrusher.isAlive()); //Wait for Crusher to shutdown
        
        /*OK... So now we need the stats
         * 
         * 
         * */
        //Time to ask the trucks for their stats.
        for (int i=0; i<9; i++)
        {
            oCurrentTruck = (Truck) vTruckList.elementAt(i);
            System.out.println(oCurrentTruck.getStats());
        }

    }//END MAIN

}//END CLASS
