/*
 * @topic T03715 <hr />Assignment a8 -- Fall 2015<hr />
 * @brief Flight collection of airlines and flights
*/
package airlinesV2;

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

public class FlightSchedule {

    private int currentTime;
    private ArrayList< Airline> airlines;
    private HashMap< String, Flight> flightMap;

    // constructors
    public FlightSchedule() {
        currentTime = 1200;
        airlines = new ArrayList< Airline>();
        flightMap = new HashMap< String, Flight>();
    }

    // operations
    public void addFlight( String flightID, Flight flight ) {
        flightMap.put( flightID, flight);
    }
    
    public Airline lookupAirline(String airlineCode) {
        for (Airline airline : airlines) {
            if (airlineCode.equals(airline.getCode())) {
                return airline;
            }
        }
        return null;
    }

    public void displayAirlines() {
        System.out.println("Airlines on file:");
        System.out.println("_______________________");
        System.out.println("Code\tName");
        for (Airline airline : airlines) {
            System.out.print(airline.getCode());
            System.out.print("\t");
            System.out.println(airline.getName());
        }
        System.out.println("_______________________");
    }
    
    private ArrayList< Flight > getSortedDepartures( String airport, char dayOfWeek)
    {
        Flight.setSortBy( Flight.SORT_BY_DEPARTURE_TIME );
        // create and populate array of flights
        ArrayList< Flight > sortedDepartures = new ArrayList<>();
        for ( Map.Entry<String,Flight> entry : flightMap.entrySet() ) {
            Flight flight = entry.getValue();
            DepartureArrivalInfo departureInfo = flight.getDepartureInfo();
            if ( !airport.equals( departureInfo.getAirport() ) ) {
                continue;
            } else if ( departureInfo.getDayOfWeek() != dayOfWeek ) {
                continue;
            }
            sortedDepartures.add(flight);
        }//for
        
        Collections.sort(sortedDepartures);
        return sortedDepartures;
    }
    
    public void displayDepartures( String airport, char dayOfWeek ) {
        //System.out.println( flightMap );
        ArrayList< Flight > sortedDepartures =
                getSortedDepartures(airport, dayOfWeek);
        System.out.println("_______________________");
        for ( Flight flight : sortedDepartures ) {
            DepartureArrivalInfo departureInfo = flight.getDepartureInfo();
            System.out.print( "\t" + flight.getAirline().getCode() +
                    flight.getFlightNumber() + "\t" );
            System.out.print( flight.getStatus() + "\t" );
            System.out.print( departureInfo.getDayOfWeek() + " " );
            System.out.print( departureInfo.getAirport() + " " );
            System.out.println( departureInfo.getTime() );
        }
        System.out.println("_______________________");
    }

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

    public void addAirline(String airlineName, String airlineCode) {
        Airline air = new Airline(airlineName, airlineCode);
        airlines.add(air);
    }

    public int getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(int currentTime) {
        this.currentTime = currentTime;
    }

}//class FlightSchedule