package org.academiadecodigo.simplegraphics.test;

import org.academiadecodigo.simplegraphics.keyboard.Keyboard;
import org.academiadecodigo.simplegraphics.keyboard.KeyboardEvent;
import org.academiadecodigo.simplegraphics.keyboard.KeyboardEventType;
import org.academiadecodigo.simplegraphics.keyboard.KeyboardHandler;
import org.academiadecodigo.simplegraphics.pictures.Picture;

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

public class Car implements KeyboardHandler {

    public enum Orientation {
        LEFT,
        RIGHT,
        UP,
        DOWN
    }

    private Picture currentSprite;
    private Orientation currentOrientation;
    private String[] spritesURLs = new String[]{
            "resources/ferrari-top.png",
            "resources/ferrari-right.png"
    };

    public Car(int x, int y) {
        this.currentOrientation = Orientation.RIGHT;
        this.currentSprite = new Picture(x, y, spritesURLs[1]);
        Keyboard k = new Keyboard(this);
        addHandlers(k);
        currentSprite.draw();
    }

    private void addHandlers(Keyboard k) {
        int[] keys = new int[]{
                KeyboardEvent.KEY_UP,
                KeyboardEvent.KEY_DOWN,
                KeyboardEvent.KEY_LEFT,
                KeyboardEvent.KEY_RIGHT
        };

        for (int key : keys) {
            KeyboardEvent event = new KeyboardEvent();
            event.setKey(key);
            event.setKeyboardEventType(KeyboardEventType.KEY_PRESSED);
            k.addEventListener(event);
        }
    }

    @Override
    public void keyPressed(KeyboardEvent e) {
        int pixels = 50;
        switch (e.getKey()) {
            case KeyboardEvent.KEY_UP:
                System.out.println("up");
                currentOrientation = Orientation.UP;
                currentSprite.load(spritesURLs[0]);
                break;
            case KeyboardEvent.KEY_DOWN:
                System.out.println("down");
                currentOrientation = Orientation.DOWN;
//                currentSprite.load(spritesURLs[0]);
//                currentSprite.grow(0, -currentSprite.getHeight());
                break;
            case KeyboardEvent.KEY_LEFT:
                System.out.println("left");
                currentOrientation = Orientation.LEFT;
//                currentSprite.load(spritesURLs[1]);
//                currentSprite.grow(-currentSprite.getWidth(),0);
                break;
            case KeyboardEvent.KEY_RIGHT:
                System.out.println("right");
                currentOrientation = Orientation.RIGHT;
                currentSprite.load(spritesURLs[1]);
                break;
        }
        move(pixels);
    }

    private void move(int pixels) {
        int dx = 0;
        int dy = 0;
        switch (currentOrientation) {
            case UP:
                dy = -pixels;
                break;
            case DOWN:
                dy = pixels;
                break;
            case LEFT:
                dx = -pixels;
                break;
            case RIGHT:
                dx = pixels;
                break;
            default:
        }
        currentSprite.translate(dx, dy);
    }

    @Override
    public void keyReleased(KeyboardEvent e) {
        return;
    }
}
