package com.richashworth.planningpoker.service;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.richashworth.planningpoker.model.Estimate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by rich on 09/04/2016.
 */
@Component
public class SessionManager {

    private final Multimap<Long, Estimate> sessionsMap = ArrayListMultimap.create();
    private final AtomicInteger sessionSequence = new AtomicInteger(1);

    public boolean isSessionLive(long id) {
        return sessionsMap.containsKey(id);
    }

    public long createSession() {
        long i = sessionSequence.getAndIncrement();
        sessionsMap.put(i, null);
        return i;
    }

    public void registerEstimate(long sessionID, Estimate estimate) {
        sessionsMap.put(sessionID, estimate);
    }

    public List<Estimate> getResults(long sessionId) {
        List<Estimate> results = new ArrayList<>();
        for (Estimate estimate : sessionsMap.get(sessionId)) {
            if (null != estimate) {
                results.add(estimate);
            }
        }
        return results;
    }

    public void clearSessions() {
        sessionsMap.clear();
    }

    public void clearSession(long sessionId) {
        sessionsMap.removeAll(sessionId);
    }

    public List<String> getUsers(long sessionId) {
        List<String> users = new ArrayList<>();
        for (Estimate e : sessionsMap.get(sessionId)) {
            users.add(e.getUserId());
        }
        return users;
    }

}

@@ -341,16 +341,63 @@
 Integer;
+%0Aimport java.util.concurrent.atomic.AtomicLong;
 %0A%0A/**%0A *
@@ -583,23 +583,20 @@
 l Atomic
-Integer
+Long
  session
@@ -620,17 +620,15 @@
 omic
-Integer
+Long
 (1
+L
 );%0A%0A
@@ -1349,24 +1349,57 @@
 ap.clear();%0A
+        sessionSequence.set(1L);%0A
     %7D%0A%0A    p

use AtomicLong for session id sequence