package model;

import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;

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

/**
 * Created by Gondr on 4/04/2017.
 */
public class DataManager {
    private ObservableList<Ride> rides;
    private ObservableList<Stop> stops;
    private ObservableList<Route> routes;
    private ObservableList<Trip> trips;
    private ObservableList<SharedTrip> sharedTrips;
    private Map<String, User> users;
    private Map<String, Driver> drivers;//seperated from users as they are two different types of accounts

    public DataManager(List rides, List stops, List routes, List trips, List sharedTrips, Map users, Map drivers){
        this.rides = FXCollections.observableArrayList(rides);
        this.stops = FXCollections.observableArrayList(stops);
        this.routes = FXCollections.observableArrayList(routes);
        this.trips = FXCollections.observableArrayList(trips);
        this.sharedTrips = FXCollections.observableArrayList(sharedTrips);
        this.users = users;
        this.drivers = drivers;
    }

    public DataManager(){
        this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>());
    }

    public ObservableList<Ride> getRides() {
        return rides;
    }

    public void addRides(Ride ride){
        rides.add(ride);
    }

    public void removeRide(Ride ride){
        rides.remove(ride);
    }

    public void removeRide(int i){
        rides.remove(i);
    }

    public ObservableList<Stop> getStops() {
        return stops;
    }

    public boolean addStop(Stop stop){
        for (int i = 0; i < stops.size(); i++){
            if (stop.equals(stops.get(i))){
                return false;
            }
        }
        stops.add(stop);
        return true;
    }

    public ObservableList<Route> getRoutes() {
        return routes;
    }

    public boolean addRoute(Route route){
        for (Route r: routes){
            if (route.equals(r)){
                return false;
            }
        }
        routes.add(route);
        return true;
    }

    public ObservableList<Trip> getTrips() {
        return trips;
    }

    public boolean addTrip(Trip trip){
        for (Trip t: trips){
            if (trip.equals(t)){
                return false;
            }
        }
        trips.add(trip);
        return true;
    }

    public ObservableList<SharedTrip> getSharedTrips() {
        return sharedTrips;
    }

    public boolean addSharedTrip(SharedTrip sharedTrip){
        for (SharedTrip t: sharedTrips){
            if (sharedTrip.equals(t)){
                return false;
            }
        }
        sharedTrips.add(sharedTrip);
        return true;
    }

    public boolean addUser(User user){
        if (!users.containsKey(String.valueOf(user.studentNumber))) {
            users.put(String.valueOf(user.studentNumber), user);
            return true;
        }
        return false;
    }

    public Map<String, User> getUsers() {
        return users;
    }

    public boolean addDriver(Driver driver){
        if (!drivers.containsKey(String.valueOf(driver.studentNumber))) {
            drivers.put(String.valueOf(driver.studentNumber), driver);
            return true;
        }
        return false;
    }

    public Map<String, Driver> getDrivers() {
        return drivers;
    }


}
