package com.richashworth.planningpoker.controller;

import com.richashworth.planningpoker.service.SessionManager;
import com.richashworth.planningpoker.util.MessagingUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.richashworth.planningpoker.service.SessionManager.SESSION_SEQ_START_VALUE;
import static com.richashworth.planningpoker.util.CollectionUtils.containsIgnoreCase;

/**
 * Created by Rich Ashworth on 09/04/2016.
 */
@RestController
public class GameController {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private SessionManager sessionManager;
    private MessagingUtils messagingUtils;

    @Autowired
    public GameController(SessionManager sessionManager, MessagingUtils messagingUtils) {
        this.sessionManager = sessionManager;
        this.messagingUtils = messagingUtils;
    }

    @RequestMapping(value = "joinSession", method = RequestMethod.POST)
    public void joinSession(
            @RequestParam(name = "sessionId") final Long sessionId,
            @RequestParam(name = "userName") final String userName
    ) {
        if (sessionId < SESSION_SEQ_START_VALUE || !sessionManager.isSessionActive(sessionId)) {
            throw new IllegalArgumentException("session not found");
        } else if (containsIgnoreCase(sessionManager.getUsers(sessionId), userName)) {
            throw new IllegalArgumentException("user exists");
        } else {
            sessionManager.registerUser(userName, sessionId);
            logger.info(userName + " has joined session " + sessionId);
            messagingUtils.burstUsersMessages(sessionId);
        }
    }

    @RequestMapping(value = "createSession", method = RequestMethod.POST)
    public long createSession(
            @RequestParam(name = "userName") final String userName
    ) {
        final long sessionId = sessionManager.createSession();
        sessionManager.registerUser(userName, sessionId);
        logger.info(userName + " has created session " + sessionId);
        messagingUtils.burstUsersMessages(sessionId);
        return sessionId;
    }

    @RequestMapping(value = "refresh", method = RequestMethod.GET)
    public void refresh(
            @RequestParam(name = "sessionId") final Long sessionId
    ) {
        messagingUtils.burstResultsMessages(sessionId);
        messagingUtils.burstUsersMessages(sessionId);
    }

    @RequestMapping(value = "reset", method = RequestMethod.DELETE)
    public void reset(
            @RequestParam(name = "sessionId") final Long sessionId,
            @RequestParam(name = "userName") final String userName
    ) {
        logger.info(userName + " has reset session " + sessionId);
        synchronized (sessionManager) {
            sessionManager.resetSession(sessionId);
            messagingUtils.burstResultsMessages(sessionId);
        }
    }

}

@@ -2601,37 +2601,36 @@
  messagingUtils.
-burst
+send
 ResultsMessages(
@@ -2619,33 +2619,32 @@
 ndResultsMessage
-s
 (sessionId);%0A   
@@ -2655,37 +2655,36 @@
  messagingUtils.
-burst
+send
 UsersMessages(se
@@ -2671,33 +2671,32 @@
 sendUsersMessage
-s
 (sessionId);%0A   

send single messages on refresh