package com.quizdeck.services;

import com.quizdeck.model.database.ActiveQuiz;
import com.quizdeck.model.database.User;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Cade on 3/12/2016.
 */

@Getter
@Setter
@Service
public class RedisActiveQuiz {

    @Autowired
    public RedisTemplate redisTemplate;

    private String ClassKey ="ActiveQuiz";
    private String prefix = "AQ";
    
    @Resource(name="redisTemplate")
    private HashOperations<String, String,ActiveQuiz> hashOperations;

    public void addEntry(String quizId, ActiveQuiz quiz){
        hashOperations.put(ClassKey, prefix+quizId, quiz);
    }

    public ActiveQuiz getEntry(String quizId){
        ActiveQuiz aq = hashOperations.get(ClassKey, prefix+quizId);
        return aq;
    }

    public void removeEntry(String quizId){
        hashOperations.delete(ClassKey, prefix+quizId);
    }

    public List<ActiveQuiz> getAllActiveQuizzes(User user){
        List<ActiveQuiz> activeQuizzes = new ArrayList<>();

        return activeQuizzes;
    }

    public long getKeySize(){
        return hashOperations.size(ClassKey);
    }

    public void updateEntry(String quizId, ActiveQuiz quiz) {
        quiz.setStart(hashOperations.get(ClassKey, prefix+quizId).getStart());
        hashOperations.put(ClassKey, prefix+quizId, quiz);
    }

}

@@ -153,24 +153,81 @@
 bok.Setter;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -277,16 +277,16 @@
 owired;%0A
-
 import o
@@ -526,16 +526,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -782,20 +782,90 @@
 = %22AQ%22;%0A
+%0A
     
+private Logger log = LoggerFactory.getLogger(RedisActiveQuiz.class);%0A
 %0A    @Re
@@ -1397,16 +1397,16 @@
  user)%7B%0A
-
         
@@ -1456,16 +1456,429 @@
 ist%3C%3E();
+%0A        log.info(%22Looking for quizzes for: %22 + user.getUserName());%0A        Map%3CString, ActiveQuiz%3E quizzes = hashOperations.entries(ClassKey);%0A        for(Map.Entry%3CString, ActiveQuiz%3E entry : quizzes.entrySet())%7B%0A            if(user.getSubscriptions().contains(entry.getValue().getOwner()) && entry.getValue().isPubliclyAvailable())%7B%0A                activeQuizzes.add(entry.getValue());%0A            %7D%0A        %7D
 %0A%0A      

Allow users to poll for active quizzes they are subscribed to