/*
 * @topic T02391 Assignment a8 -- Airline Flight Schedule demo Dec 9 2014
 * @brief class Menu
 */
package a8demo;

import java.util.List;
import java.util.Scanner;

public class Menu {

    // data attributes
    public static final int CHOICE_UNKNOWN = 0;
    public static final int CHOICE_SET_CLOCK = 1;
    public static final int CHOICE_CLEAR_SCHEDULE = 2;
    public static final int CHOICE_ADD_AIRLINE = 3;
    public static final int CHOICE_ADD_FLIGHT = 4;
    public static final int CHOICE_CANCEL_FLIGHT = 5;
    public static final int CHOICE_SHOW_FLIGHT_INFO = 6;
    public static final int CHOICE_SHOW_DEPARTURES = 7;
    public static final int CHOICE_SHOW_ARRIVALS = 8;
    public static final int CHOICE_FIND_FLIGHTS = 9;
    public static final int CHOICE_EXIT = 10;

    private String strChoices;
    private Scanner sc;
    private int choice;

    // constructors
    public Menu() {
        strChoices
                = "___________________________________\n"
                + "Please make your choice\n"
                + "by entering the corresponding menu number:\n";
        sc = new Scanner(System.in);
        choice = CHOICE_UNKNOWN;
    }//Menu

    // operations
    public void attach(String aChoice) {
        strChoices += aChoice;
        strChoices += "\n";
    }//attach

    public int getUserChoice() {
        System.out.print(strChoices);
        //System.out.print(">>> ");
        choice = Validator.getInt(
                sc,
                ">>> ",
                CHOICE_SET_CLOCK - 1, // min
                CHOICE_EXIT + 1 // max
        );
        //sc.nextLine(); // eat up the remaining new line character
        // TODO -- validate user selection
        return choice;
    }//getUserChoice

    public void pause() {
        System.out.print("                         Press Enter to continue: ");
        sc.nextLine();
    }//pause

    public void addAirline(FlightSchedule schedule) {
        String airlineName;
        String airlineCode;
        String aircraftModel;

        airlineCode = Validator.getString(sc,
                "  Airline code: ");

        if (schedule.isAirlinePresent(airlineCode)) {
            System.out.println("Sorry the airline with this code already exists!");
            schedule.printAirline(airlineCode);
            return;
        }
        airlineName = Validator.getString(sc,
                "  Airline name: ");
        aircraftModel = Validator.getString(sc,
                "Aircraft model: ");

        Airline airline = new Airline(airlineName, airlineCode, aircraftModel);
        schedule.addAirline(airline);
        schedule.printAirline(airlineCode);
        System.out.println("Added successfully.");
    }//addAirline

    public void addFlight(FlightSchedule schedule) {
        //Flight number (ID) -- make sure no duplicate
        //flight numbers per airline are permitted in the system.
        String flightID; // "DL1011" type of string
        flightID = Validator.getString(sc, "Flight number (or x to cancel): ");
        if (flightID.equalsIgnoreCase("x")) {
            return;
        }
        // extract the airline code out of the flightID
        String airlineCode = flightID.substring(0, 2);
        if (airlineCode.length() < 2) {
            System.out.println(
                    "Sorry, the flight number must be in \"DL1011\" format.");
            return;
        }
        if (!schedule.isAirlinePresent(airlineCode)) {
            System.out.println(
                    "Sorry the airline with "
                    + airlineCode
                    + " code doesn't exist!");
            return;
        }
        Airline airline = schedule.getAirline(airlineCode);

        //Flight status -- S/C/D/A for Scheduled/Cancelled/Departed/Arrived
        String flightStatus = Validator.getString(sc, "Flight Status S/C/D/A: ");

        //Flight Type -- D/I for Domestic/International
        String flightType = Validator.getString(sc, "Flight Type D/I: ");

        // Gather departure data
        String dayOfWeek = Validator.getString(
                sc, " Departure day of week: ");
        int time = Validator.getInt(
                sc, "        Departure time: ");
        String airportCode = Validator.getString(
                sc, "Departure airport code: ");
        String gate = Validator.getString(
                sc, "        Departure gate: ");

        DepartureArrivalInfo departure
                = new DepartureArrivalInfo(dayOfWeek, time, airportCode, gate);

        // Gather arrival data
        dayOfWeek = Validator.getString(
                sc, " Arrival day of week: ");
        time = Validator.getInt(
                sc, "        Arrival time: ");
        airportCode = Validator.getString(
                sc, "Arrival airport code: ");
        gate = Validator.getString(
                sc, "        Arrival gate: ");

        DepartureArrivalInfo arrival
                = new DepartureArrivalInfo(dayOfWeek, time, airportCode, gate);

        Flight flight = new Flight(
                airline,
                flightID,
                flightStatus,
                flightType,
                departure,
                arrival
        );
        schedule.addFlight(flightID, flight);
        System.out.println("Flight added successfully:");
        System.out.println(flight.toString());
    }//addFlight

    public void showArrivals(FlightSchedule schedule) {
        String dayOfWeek = Validator.getString(sc, "Arrival day of week: ");
        String airportCode = Validator.getString(sc, "Arrival airport: ");
        List<Flight> sortedFlights = schedule.getSortedArrivals(dayOfWeek, airportCode);
        for (Flight flight : sortedFlights) {
            System.out.println(
                    flight.getFlightID()
                    + '\t'
                    + flight.getArrival().getTime()
                    + '\t'
                    + flight.getDeparture().getAirportCode()
                    + '\t'
                    + flight.getArrival().getAirportCode()
            );
        }
    }
}//class Menu