package com.uca.flights;

import java.util.HashMap;
import java.util.Map;
import java.util.Collection;

/**
 * Represent an registry to store city and airports, singleton
 * @Author Cesano Ugo, Colmerauer Clément
 * 
 */
public class CitiesAndAirportsRegistry
{
	/**Singleton instance*/
	static private CitiesAndAirportsRegistry instance = null;
	/**Registry map of cities*/
	static private Map<CityId,City>          cities;
	/**Registry map of airports*/
	static private Map<AirportId,Airport>    airports;

	/**
     * Constructor
     */
	private CitiesAndAirportsRegistry()
	{
 		this.cities   = new HashMap<CityId,City>();
 		this.airports = new HashMap<AirportId,Airport>();
 		instance      = this;
	}

	/**
     * Singleton getInstance method.
     * @return the instance of singleton
     */
	public static CitiesAndAirportsRegistry getInstance()
	{
		if(instance == null)
		{
			return new CitiesAndAirportsRegistry();
		}
		else
		{
			return instance;
		}
	}

	/**
     * Add a city to the city registry
     * @param c the city to add
     * @throws IllegalArgumentException on null parameter
     */
	public static void add(City c)
	{
		if(c == null)
		{
			throw new IllegalArgumentException("City can't be null.");
		}
		cities.put(c.getCode(),c);
	}

	/**
     * Add an airport to the airport registry
     * @param a the airport to add
     * @throws IllegalArgumentException on null parameter
     */
	public static void add(Airport a)
	{
		if(a == null)
		{
			throw new IllegalArgumentException("Airport can't be null.");
		}
		airports.put(a.getId(),a);
	}

	/**
     * Get a city from the city registry
     * @param code the city identifier
     * @throws IllegalArgumentException on null parameter
     */
	public static City get(CityId code)
	{
		if(code == null)
		{
			throw new IllegalArgumentException("City id can't be null.");
		}
		return cities.get(code);
	}

	/**
     * Get a airport from the airport registry
     * @param id the airport identifier
     * @throws IllegalArgumentException on null parameter
     */
	public static Airport get(AirportId id)
	{
		if(id == null)
		{
			throw new IllegalArgumentException("Airport id can't be null.");
		}
		return airports.get(id);
	}

	/**
     * Get a collection of cityId
     * @return a collection of city id
     */
	public static Collection<City> cities()
	{
		return cities.values();
	}

	/**
     * Get a collection of airportId
     * @return a collection of airport id
     */
	public static Collection<Airport> airports()
	{
		return airports.values();
	}
}