package controller;

import model.game.GameModel;
import model.transporters.Transporter;
import view.GameViewPanel;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jonathen on 4/14/2017.
 */
public class GameController implements KeyListener{
    GameViewPanel gameViewPanel;
    GameModel gameModel;

    AbilityController abilityController;
    TransporterController transporterController;

    //Used to map a specific key event type to a desired handler. Avoids use of CONDITIONAL LOGICCC
    Map<Integer, KeyEventHandler> keyHandlerMap;

    public GameController(GameViewPanel gameViewPanel, GameModel gameModel) {
        this.gameViewPanel = gameViewPanel;
        this.gameModel = gameModel;

        gameViewPanel.addKeyListener(this);

        abilityController = new AbilityController();
        //TODO fix this to not violate LOD?
        transporterController = new TransporterController(abilityController, (gameModel.getPlayers())[0].getTransportManager());

        initKeyHandlerMap();

        //TODO attach controller to view panel somehow
    }

    private void initKeyHandlerMap() {
        keyHandlerMap = new HashMap<>();
        keyHandlerMap.put(KeyEvent.VK_ENTER, abilityController);
        keyHandlerMap.put(KeyEvent.VK_UP, abilityController);
        keyHandlerMap.put(KeyEvent.VK_DOWN, abilityController);
        keyHandlerMap.put(KeyEvent.VK_LEFT, transporterController);
        keyHandlerMap.put(KeyEvent.VK_RIGHT, transporterController);
    }

    //TODO if we need more specificity, use different method call for typed/pressed/released
    @Override
    public void keyTyped(KeyEvent e) {
        //Currently don't need to do anything
    }

    @Override
    public void keyPressed(KeyEvent e) {
        deferToHandler(e);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //Currently don't need to do anything
    }

    private void deferToHandler(KeyEvent e) {
        int key = e.getKeyCode();
        if (keyHandlerMap.containsKey(key)) {
            keyHandlerMap.get(key).handle(key);
        }
    }

    //TODO delete: this is for glass testing
    public Transporter getCurrentTransporter() {
        return transporterController.getCurrentTransporter();
    }
}

@@ -1050,24 +1050,65 @@
 anager());%0A%0A
+        keyHandlerMap = new HashMap%3C%3E();%0A
         init
@@ -1120,16 +1120,23 @@
 ndlerMap
+ForGame
 ();%0A%0A   
@@ -1232,53 +1232,19 @@
 rMap
-() %7B%0A        keyHandlerMap = new HashMap%3C%3E();
+ForGame() %7B
 %0A   

Rename method in GameController.