package ca.antonious.habittracker;

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

import ca.antonious.habittracker.models.Habit;
import ca.antonious.habittracker.observable.IObservable;
import ca.antonious.habittracker.observable.IObserver;

/**
 * Created by George on 2016-09-02.
 */
public class HabitRepository implements IHabitRepository {
    private IHabitService habitService;
    private Map<String, Habit> habits;

    private Set<IObserver<List<Habit>>> observers = new HashSet<>();

    public HabitRepository(IHabitService habitService) {
        this.habitService = habitService;
        this.habits = new HashMap<>();
    }

    public List<Habit> getHabits() {
        if (habits.isEmpty()) {
            for (Habit habit: habitService.getHabits()) {
                habits.put(habit.getId(), habit);
            }
        }
        return new ArrayList<>(habits.values());
    }

    public Habit getHabit(String id) {
        getHabits();

        return habits.get(id);
    }

    public void addHabit(Habit habit) {
        getHabits();

        habitService.addHabit(habit);
        habits.put(habit.getId(), habit);
        notifyChange();
    }

    public void updateHabit(Habit habit) {
        getHabits();

        habitService.updateHabit(habit);
        habits.put(habit.getId(), habit);
        notifyChange();
    }

    public void removeHabit(String id) {
        getHabits();

        habitService.removeHabit(id);
        habits.remove(id);
        notifyChange();
    }

    private void notifyChange() {
        for (IObserver<List<Habit>> observer: observers) {
            observer.onNext(getHabits());
        }
    }

    @Override
    public void addObserver(IObserver<List<Habit>> observer) {
        observers.add(observer);
        observer.onNext(getHabits());
    }

    @Override
    public void removeObserver(IObserver<List<Habit>> observer) {
        observers.remove(observer);
    }

    @Override
    public void removeAllObservers() {
        observers.clear();
    }
}

@@ -53,24 +53,112 @@
 .ArrayList;%0A
+import java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0A
 import java.
@@ -167,24 +167,24 @@
 il.HashMap;%0A
-
 import java.
@@ -1016,32 +1016,33 @@
     %7D%0A        %7D%0A
+%0A
         return n
@@ -1044,40 +1044,252 @@
 urn 
-new ArrayList%3C%3E(habits.values())
+getSortedHabits();%0A    %7D%0A%0A    private List%3CHabit%3E getSortedHabits() %7B%0A        List%3CHabit%3E sortedHabits = new ArrayList%3C%3E(habits.values());%0A        Collections.sort(sortedHabits, reverseChronologicalHabitComparator);%0A%0A        return sortedHabits
 ;%0A  
@@ -2413,16 +2413,274 @@
 lear();%0A
-
     %7D
+%0A%0A    private static Comparator%3CHabit%3E reverseChronologicalHabitComparator  = new Comparator%3CHabit%3E() %7B%0A        @Override%0A        public int compare(Habit lhs, Habit rhs) %7B%0A            return rhs.getStartDate().compareTo(lhs.getStartDate());%0A        %7D%0A    %7D;
 %0A%7D%0A

Return sorted habit list from habit repository