package org.academiadecodigo.bootcamp.fxlogin.service.bootcamp;

import org.academiadecodigo.bootcamp.fxlogin.model.Bootcamp;
import org.academiadecodigo.bootcamp.fxlogin.model.Cadet;

import java.util.*;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class MockBootcampService implements BootcampService {
    private Map<Integer, Bootcamp> bootcamps;
    private Integer selectedBootcamp;

    public MockBootcampService() {
        this.bootcamps = new HashMap<>();
    }

    @Override
    public void createBootcamp(Integer id, Bootcamp.Location location, String start, String end) {
        if (bootcamps.containsKey(id)) {
            return;
        }

        bootcamps.put(id, new Bootcamp(id, location, start, end));
    }

    @Override
    public void addBootcamp(Bootcamp bootcamp) {
        bootcamps.put(bootcamp.getId(), bootcamp);
    }

    @Override
    public void addCodeCadet(Integer bootcampId, Cadet cadet) {
        if (!bootcamps.containsKey(bootcampId)) {
            return;
        }

        if (bootcamps.get(bootcampId).contains(cadet)) {
            return;
        }

        bootcamps.get(bootcampId).addCodeCadet(cadet);
    }

    @Override
    public void addCodeCadets(Integer bootcampId, Cadet... cadets) {
        if (!bootcamps.containsKey(bootcampId)) {
            return;
        }

        Bootcamp bootcampToAddTo = bootcamps.get(bootcampId);
        for (Cadet cadet : cadets) {
            bootcampToAddTo.addCodeCadet(cadet);
        }
    }

    @Override
    public Bootcamp getById(Integer bootcampId) {
        return bootcamps.get(bootcampId);
    }

    @Override
    public List<Bootcamp> listBootcamps() {
        return new LinkedList<>(bootcamps.values());
    }

    @Override
    public Set<Cadet> listCodeCadets(Integer bootcampId) {
        return bootcamps.get(bootcampId).getCadets();
    }

    @Override
    public void setSelectedBootcamp(Integer bootcampId) {
        this.selectedBootcamp = bootcampId;
    }

    @Override
    public Integer getSelectedBootcamp() {
        return selectedBootcamp;
    }

}
