package se.cygni.snake.websocket.arena;


import org.eclipse.jetty.websocket.api.CloseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.WebSocketSession;

import com.google.common.eventbus.EventBus;

import se.cygni.snake.api.exception.InvalidArenaName;
import se.cygni.snake.api.exception.InvalidMessage;
import se.cygni.snake.arena.ArenaManager;
import se.cygni.snake.arena.ArenaSelectionManager;
import se.cygni.snake.websocket.BaseGameSocketHandler;

import java.io.IOException;

public class ArenaWebSocketHandler extends BaseGameSocketHandler {
    private static final Logger log = LoggerFactory.getLogger(ArenaWebSocketHandler.class);

    private static final String ARENA_NAME_WHITELIST = "[a-zA-Z0-9-_]+";

    private final ArenaSelectionManager arenaSelectionManager;
    private ArenaManager arenaManager;

    @Autowired
    public ArenaWebSocketHandler(ArenaSelectionManager arenaSelectionManager) {
        this.arenaSelectionManager = arenaSelectionManager;
    }

    // @Override
    // public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
    //     super.afterConnectionClosed(session, status);
    //     session.close(CloseStatus.SERVER_ERROR);
    //     outgoingEventBus.unregister(this);
    //     playerLostConnection();

    //     log.info("afterConnectionClosed aa{}", status);
    // }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) {
        super.afterConnectionEstablished(session);

        String uri = session.getUri().getPath();

        String arenaName = uri.replaceAll(".*/", "");

        this.arenaManager = arenaSelectionManager.getArena(arenaName);
        if (this.arenaManager == null) {
            // TODO: Fix exceptions occuring since eventbus is null after this
            handleInvalidArenaName(session);
            return;
        }

        setOutgoingEventBus(arenaManager.getOutgoingEventBus());
        setIncomingEventBus(arenaManager.getIncomingEventBus());
        log.info("Started arena web socket handler");
    }

    private void handleInvalidArenaName(WebSocketSession session) {
        InvalidArenaName invalidArenaName = new InvalidArenaName(InvalidArenaName.ArenaNameInvalidReason.Nonexistent);
        invalidArenaName.setReceivingPlayerId(this.getPlayerId());

        // Setting event bus to prevent exceptions
        setOutgoingEventBus(new EventBus("arena-outgoing"));
        setIncomingEventBus(new EventBus("arena-incoming"));

        try {
            sendSnakeMessage(invalidArenaName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void handleInvalidName(String uri, String arenaFromUri) {
        try {
            sendSnakeMessage(new InvalidMessage(
                    String.format("Arena name %s does not match allowed pattern %s", arenaFromUri, ARENA_NAME_WHITELIST),
                    uri));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    protected void playerLostConnection() {
        log.info("{} lost connection", getPlayerId());
        if (arenaManager != null) {
            arenaManager.playerLostConnection(getPlayerId());
        }
    }
}

@@ -39,60 +39,8 @@
 ;%0A%0A%0A
-import org.eclipse.jetty.websocket.api.CloseStatus;%0A
 impo
@@ -1044,390 +1044,8 @@
  %7D%0A%0A
-    // @Override%0A    // public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception %7B%0A    //     super.afterConnectionClosed(session, status);%0A    //     session.close(CloseStatus.SERVER_ERROR);%0A    //     outgoingEventBus.unregister(this);%0A    //     playerLostConnection();%0A%0A    //     log.info(%22afterConnectionClosed aa%7B%7D%22, status);%0A    // %7D%0A
 %0A   

remove unused code