/*
 * Created on Jan 30, 2005
 *
 * Advanced Data Management Group
 * Center for Advanced Computing and Informatics Studies
 * University of Puerto Rico, Mayaguez
 * 
 */
package ece.uprm.admg.cs2.p1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Class AutoPartCatalog is used to implement the logic the application
 * that manages the catalog of parts for a automobiles.
 * 
 * @author Manuel Rodriguez
 *
 */
public class AutoPartCatalog {
    
    // Constants to define the operations available,
    // Add a new car
    private static final int ADD_CAR = 1;
    // Remove a car
    private static final int REM_CAR = 2;
    // View a car
    private static final int VIEW_CAR = 3;
    // Add a new car
    private static final int ADD_PART = 4;
    // Remove a part
    private static final int REM_PART = 5;
    // View a part
    private static final int VIEW_PART = 6;
    // Find all parts for a car
    private static final int FIND_PARTS = 7;
    // Add a new part to a car
    private static final int ADD_PART_CAR = 8;
    // Print all cars in the catalog.
    private static final int PRINT_CARS = 9;
    // Print all parts in the catalog.
    private static final int PRINT_PARTS = 10;
    // Finish
    private static final int DONE = 11;
   
    // Catalog Manager object that keeps track of the data in the catalog
    // and provides the operations to make those data items.
    private CatalogManager manager;
    
    /*
     * Default constructor used to create a new instance of the AutoPartCatalog.
     */
    private AutoPartCatalog(){
        this.manager = new CatalogManager();
    }
    
    /*
     * Function used to start the execution of the application.
     */
    
    private void start() throws Exception {
        boolean done = false;
        int option = 0;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.println("Welcome to the Auto Part Catalog Management Application");
        while(!done){
            printMenu();
            option = getUserOption(in);
            switch(option){
            case ADD_CAR:
                handleAddCar(in);
                break;
            case ADD_PART:
                handleAddPart(in);
                break;
            case ADD_PART_CAR:
                handleAddPartToCar(in);
                break;
            case FIND_PARTS:
                handleFindParts(in);
                break;
            case PRINT_CARS:
                handlePrintCars();
                break;
            case PRINT_PARTS:
                handlePrintParts();
                break;
            case REM_CAR:
                handleRemoveCar(in);
                break;
            case REM_PART:
                handleRemovePart(in);
                break;
            case VIEW_CAR:
                handleViewCar(in);
                break;
            case VIEW_PART:
                handleViewPart(in);
                break;
            case DONE:
                done = true;
                break;  
            default:
                System.out.println("Illegal option: " + option);
            	break;
            }
        }
    }
    
    /**
     * @param in
     */
    private void handleAddPartToCar(BufferedReader in) {
        String carKey = null, partKey = null;
        Part P = null;
        Car C  = null;
        
        // Check that part is valid
        P = manager.getPart(partKey);
        if (P == null){
            System.out.println("Part with key " + partKey + " is not currently " +
                    "in the catalog");
            return;
        }
        
        // Check that car is valid
        C = manager.getCar(carKey);
        if (C == null){
            System.out.println("Car with key " + carKey + " is not currently " + 
                    "in the catalog.");
            return;
        }
        
        //Looks good, add it to the Car
        C.addPart(P);
        System.out.println("The part was added to the car.");
        return;
        
    }

    /**
     * @param in
     */
    private void handleViewPart(BufferedReader in) throws IOException{
        String key = null;
        Part P = null;
        
        System.out.println("Enter the key for the part to view: ");
        key = (String) in.readLine();
        P = manager.getPart(key);
        if (P == null){
            System.out.println("Part with key: " + key + " not found.");
        }
        else {
            System.out.println("Part information: " + P.toString());
        }
    }

    /**
     * @param in
     */
    private void handleViewCar(BufferedReader in) throws IOException{
        String key = null;
        Car C = null;
        
        System.out.println("Enter the key for the car to view: ");
             key = (String) in.readLine();
        C = manager.getCar(key);
        if (C == null){
            System.out.println("Car with key: " + key + " not found.");
        }
        else {
            System.out.println("Car information: " + C.toString());
        }
    }
       

    /**
     * @param in
     */
    private void handleRemovePart(BufferedReader in) throws IOException {
        String key = null;
        boolean res = false;
        
        System.out.println("Enter the key for the part to remove: ");
        key = (String) in.readLine();
        res = manager.removePart(key);
        if (res){
            System.out.println("Part with key: " + key + " was erased.");
        }
        else {
            System.out.println("Part with key: " + key + " was not found.");
        }        
    }

    /**
     * @param in
     */
    private void handleRemoveCar(BufferedReader in) throws IOException {
        String key = null;
        boolean res = false;
        
        System.out.println("Enter the key for the car to remove: ");
        key = (String) in.readLine();
        res = manager.removeCar(key);
        if (res){
            System.out.println("Car with key: " + key + " was erased.");
        }
        else {
            System.out.println("Car with key: " + key + " was not found.");
        }        
    }

    /**
     * @param in
     */
    private void handlePrintParts() {
       System.out.println("Information about Parts in the catalog: ");
       Part[] theParts = manager.getAllParts();
       if (theParts == null){
           System.out.println("There are no parts in the catalog at this time.");
       }
       else {
           int i, len = theParts.length;
           for (i =0; i < len; ++i){
               System.out.println(theParts[i].toString());
           }
       }       
    }

    /**
     * @param in
     */
    private void handlePrintCars() {
        System.out.println("Information about Cars in the catalog: ");
        Car[] theCars = manager.getAllCars();
        if (theCars == null){
            System.out.println("There are no cars in the catalog at this time.");
        }
        else {
            int i, len = theCars.length;
            for (i =0; i < len; ++i){
                System.out.println(theCars[i].toString());
            }
        }       
    }

    /**
     * @param in
     */
    private void handleFindParts(BufferedReader in) throws IOException {
        String key = null;
        Part[] theParts = null;
        
        System.out.println("Enter the key for the car whose parts will be searched: ");
        key = (String) in.readLine();
        theParts = manager.getCarParts(key);
        if (theParts == null){
            System.out.println("Part information for Car with key: " + key + 
                    " was not found.");
        }
        else {
            System.out.println("The following parts are used for Car with key: " + key);
            int i, len = theParts.length;
            for (i =0; i < len; ++i){
                System.out.println(theParts[i].toString());
            }          
        }        
    }

    /**
     * @param in
     */
    private void handleAddPart(BufferedReader in) throws IOException {
        String info = null;
        Part P = null;
        
        System.out.println("Enter the information for the part to be added: ");
        info = (String) in.readLine();
        P = Part.parse(info);
        if (manager.addPart(P)){
            System.out.println("The part has been added to the catalog.");
        }
        else {
            System.out.println("The part was already in the catalog.");
        }
    }

    /**
     * @param in
     */
    private void handleAddCar(BufferedReader in)throws IOException {
        String info = null;
        Car C = null;
        
        System.out.println("Enter the information for the Car to be added: ");
        info = (String) in.readLine();
        C = Car.parse(info);
        if (manager.addCar(C)){
            System.out.println("The Car has been added to the catalog.");
        }
        else {
            System.out.println("The Car was already in the catalog.");
        }      
    }

/*
     * Function used to print the options available to the user.
     */
   private void printMenu(){
       System.out.println("1. Add Car");
       System.out.println("2. Remove Car");
       System.out.println("3. View Car information");
       System.out.println("4. Add Part");
       System.out.println("5. Remove Part");
       System.out.println("6. View Part information");
       System.out.println("7. Show all Parts of given Car");
       System.out.println("8. Add Part to a Car");
       System.out.println("9. Print all Cars");
       System.out.println("10. Print all Parts");
       System.out.println("11. Exit");
   }
   
   /*
    * Function used to get the input from the user.
    */
	private int getUserOption(BufferedReader in) throws IOException{
		String value = null;
		
		System.out.println("Enter your option: ");
		value = in.readLine();
		if (value.equals("1")){
			return 1;
		}
		else if (value.equals("2")){
			return 2;
		}
		else if (value.equals("3")){
			return 3;
		}
		else if (value.equals("4")){
			return 4;
		}
		else if (value.equals("5")){
			return 5;
		}
		else if (value.equals("6")){
			return 6;
		}
		else if (value.equals("7")){
			return 7;
		}
		else if (value.equals("8")){
			return 8;
		}
		else if (value.equals("9")){
			return 9;
		}
		else if (value.equals("10")){
			return 10;
		}
		else if (value.equals("11")){
			return 11;
		}
		else {
			throw new UnsupportedOperationException("Illegal option:" + value);
		}
	}
   
    public static void main(String[] args) {
        AutoPartCatalog application = new AutoPartCatalog();
        try {
            application.start();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
