package Controller;

import Model.IVehicle;
import Repository.IRepository;

public class Controller {
    private IRepository _repository;

    public Controller(IRepository repository) {
        this._repository = repository;
    }

    public void addVehicle(IVehicle vehicle) throws IllegalArgumentException {
        _repository.add(vehicle);
    }

    public void removeVehicle(int index) throws IllegalArgumentException {
        _repository.remove(index);
    }

    public IVehicle[] getAllVehicles() {
        return _repository.getAll();
    }

    public IVehicle[] filterVehicles(String color){
        IVehicle[] vehicles = _repository.getAll();
        IVehicle[] filteredVehicles = new IVehicle[_repository.getSize()];
        int index = 0;
        for(IVehicle vehicle : vehicles) {
            if(vehicle.getColor().equals(color)) {
                filteredVehicles[index++] = vehicle;
            }
        }
        IVehicle[] copy = new IVehicle[index];
        for(int i = 0; i < index; i++) {
            copy[i] = filteredVehicles[i];
        }
        return copy;
    }

}
