package org.academiadecodigo.bootcamp;

import org.academiadecodigo.bootcamp.car.Car;
import org.academiadecodigo.bootcamp.car.Human;
import org.academiadecodigo.bootcamp.grid.GridDirection;
import org.academiadecodigo.simplegraphics.keyboard.Keyboard;
import org.academiadecodigo.simplegraphics.keyboard.KeyboardEvent;
import org.academiadecodigo.simplegraphics.keyboard.KeyboardEventType;
import org.academiadecodigo.simplegraphics.keyboard.KeyboardHandler;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class Handler implements KeyboardHandler {

    private Keyboard k;
    private KeyboardEvent[] resetCar = new KeyboardEvent[2];

    private Human human1;
    private Human human2;

    private final int startPlayer1 = 13; // ENTER
    private final int startPlayer2 = 9; // TAB

    private final int[] keysPlayer1 = {
            KeyboardEvent.KEY_UP,
            KeyboardEvent.KEY_DOWN,
            KeyboardEvent.KEY_LEFT,
            KeyboardEvent.KEY_RIGHT
    };
    private final int[] keysPlayer2 = {
            KeyboardEvent.KEY_W,
            KeyboardEvent.KEY_S,
            KeyboardEvent.KEY_A,
            KeyboardEvent.KEY_D
    };

    public Handler(Car player1, Car player2) {
        k = new Keyboard(this);
        human1 = (Human) player1;
        human2 = (Human) player2;

        for (int i = 0; i < resetCar.length; i++) {
            resetCar[i] = new KeyboardEvent();
            resetCar[i].setKeyboardEventType(KeyboardEventType.KEY_PRESSED);
        }
        resetCar[0].setKey(startPlayer1);
        resetCar[1].setKey(startPlayer2);
        k.addEventListener(resetCar[0]);
        k.addEventListener(resetCar[1]);
    }

    @Override
    public void keyPressed(KeyboardEvent keyboardEvent) {
        GridDirection human1 = null;
        GridDirection human2 = null;

        switch (keyboardEvent.getKey()) {
            case startPlayer1:
                this.human1.show();
                break;
            case startPlayer2:
                this.human2.show();
                break;
            case KeyboardEvent.KEY_UP:
                human1 = GridDirection.UP;
                break;
            case KeyboardEvent.KEY_DOWN:
                human1 = GridDirection.DOWN;
                break;
            case KeyboardEvent.KEY_LEFT:
                human1 = GridDirection.LEFT;
                break;
            case KeyboardEvent.KEY_RIGHT:
                human1 = GridDirection.RIGHT;
                break;
            case KeyboardEvent.KEY_W:
                human2 = GridDirection.UP;
                break;
            case KeyboardEvent.KEY_S:
                human2 = GridDirection.DOWN;
                break;
            case KeyboardEvent.KEY_A:
                human2 = GridDirection.LEFT;
                break;
            case KeyboardEvent.KEY_D:
                human2 = GridDirection.RIGHT;
                break;
            default:

        }
        this.human1.move(human1);
        this.human2.move(human2);
    }

    @Override
    public void keyReleased(KeyboardEvent keyboardEvent) {

    }
}
