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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class FlightSchedule {

    // data attributes

    private int utcTime;
    private ArrayList< Airline> airlines;
    private HashMap< String, Flight> flights;

    // constructors
    public FlightSchedule() {
        airlines = new ArrayList< Airline>();
        flights = new HashMap<>();
    }

    // operations
    public ArrayList<Flight> getSortedArrivals(String dayOfWeek, String airportCode) {
        ArrayList<Flight> sortedFlights = new ArrayList<Flight>();
        for (Map.Entry<String, Flight> entry : flights.entrySet()) {
            //String flightID = entry.getKey();
            Flight flight = entry.getValue();
            DepartureArrivalInfo arrival = flight.getArrival();
            if (dayOfWeek.equals(arrival.getDayOfWeek())) {
                if (airportCode.equals(arrival.getAirportCode())) {
                    sortedFlights.add(flight);
                }
            }
        }// for each flight
        Collections.sort(sortedFlights);
        return sortedFlights;
        
    }

    public void addAirline(Airline airline) {
        airlines.add(airline);
    }//addAirline()

    public void addFlight(String flightID, Flight flight) {
        // flightID is "DL1011" type of string
        flights.put(flightID, flight);
    }//addFlight

    public void printAirline(String airlineCode) {
        for (int idx = 0; idx < airlines.size(); ++idx) {
            // visit every airline and search for the given code
            Airline airline = airlines.get(idx);
            if (airline.getCode().equals(airlineCode)) {
                // it was found, so print
                System.out.println(airline.getCode() + " " + airline.getName());
                return;
            }
        }
        System.out.println("Airline code " + airlineCode + " not found!");
    }//printAirline()

    public boolean isAirlinePresent(String airlineCode) {
        for (Airline airline : airlines) {
            if (airline.getCode().equals(airlineCode)) {
                return true;
            }
        }
        return false;
    }//isAirlinePresent

    public Airline getAirline(String airlineCode) {
        for (Airline airline : airlines) {
            if (airline.getCode().equals(airlineCode)) {
                return airline;
            }
        }
        return null;
    }//isAirlinePresent

}//class FlightSchedule