package pt.up.fe.pt.lpoo.bombermen;

import pt.up.fe.pt.lpoo.utils.Direction;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.math.Vector3;

public class Input implements InputProcessor
{
    public static final int A_UP = 0;
    public static final int A_DOWN = 1;
    public static final int A_LEFT = 2;
    public static final int A_RIGHT = 3;
    public static final int A_BOMB = 4;
    public static final int A_NUM_OF_ACTIONS = 5;

    public interface Commands
    {
        void ExecuteAction(int action, boolean val);
    }

    private Commands _commands;
    private Camera _camera;
    private ControlPad _controlPad = null;
    private Vector3 _tempVector = new Vector3();

    public void SetControlPad(ControlPad controlPad)
    {
        _controlPad = controlPad;
    }

    public Input(Commands commands, Camera camera)
    {
        _commands = commands;
        _camera = camera;
    }

    @Override
    public boolean keyDown(int keycode)
    {
        switch (keycode)
        {
            case Keys.S:
                _commands.ExecuteAction(A_DOWN, true);
                System.out.println("Player move down.");
                Gdx.app.debug("Input", "Player move down.");
                return true;
            case Keys.W:
                Gdx.app.debug("Input", "Player move up.");
                System.out.println("Player move up.");
                _commands.ExecuteAction(A_UP, true);
                return true;
            case Keys.A:
                Gdx.app.debug("Input", "Player move left.");
                _commands.ExecuteAction(A_LEFT, true);
                return true;
            case Keys.D:
                Gdx.app.debug("Input", "Player move right.");
                _commands.ExecuteAction(A_RIGHT, true);
                return true;
            case Keys.SPACE:
                Gdx.app.debug("Input", "Player place bomb.");
                _commands.ExecuteAction(A_BOMB, true);
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean keyUp(int keycode)
    {
        switch (keycode)
        {
            case Keys.S:
                _commands.ExecuteAction(A_DOWN, false);
                return true;
            case Keys.W:
                Gdx.app.debug("Input", "Player move up.");
                _commands.ExecuteAction(A_UP, false);
                return true;
            case Keys.A:
                Gdx.app.debug("Input", "Player move left.");
                _commands.ExecuteAction(A_LEFT, false);
                return true;
            case Keys.D:
                Gdx.app.debug("Input", "Player move right.");
                _commands.ExecuteAction(A_RIGHT, false);
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean keyTyped(char character)
    {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button)
    {
        _tempVector.set(screenX, screenY, 0);
        _camera.unproject(_tempVector);

        if (_controlPad != null)
        {
            if (button == Buttons.LEFT)
            {
                int dir = _controlPad.Click(_tempVector);
                Gdx.app.debug("Input", "Pad direction " + dir);

                switch (dir)
                {
                    case Direction.NORTH:
                        _commands.ExecuteAction(A_UP, true);
                        return true;
                    case Direction.SOUTH:
                        _commands.ExecuteAction(A_DOWN, true);
                        return true;
                    case Direction.WEST:
                        _commands.ExecuteAction(A_LEFT, true);
                        return true;
                    case Direction.EAST:
                        _commands.ExecuteAction(A_RIGHT, true);
                        return true;
                    case ControlPad.PLACE_BOMB:
                        _commands.ExecuteAction(A_BOMB, true);
                        return true;
                }
            }
        }

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button)
    {
        _tempVector.set(screenX, screenY, 0);
        _camera.unproject(_tempVector);

        if (_controlPad != null)
        {
            if (button == Buttons.LEFT)
            {
                int dir = _controlPad.Click(_tempVector);

                switch (dir)
                {
                    case Direction.NORTH:
                        _commands.ExecuteAction(A_UP, false);
                        return true;
                    case Direction.SOUTH:
                        _commands.ExecuteAction(A_DOWN, false);
                        return true;
                    case Direction.WEST:
                        _commands.ExecuteAction(A_LEFT, false);
                        return true;
                    case Direction.EAST:
                        _commands.ExecuteAction(A_RIGHT, false);
                        return true;
                }
            }
        }

        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer)
    {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY)
    {
        return false;
    }

    @Override
    public boolean scrolled(int amount)
    {
        return false;
    }
}

@@ -593,16 +593,92 @@
 S = 5;%0A%0A
+    private boolean%5B%5D _activeActions = %7B false, false, false, false %7D;%0A    %0A
     publ
@@ -3629,32 +3629,85 @@
 irection.NORTH:%0A
+                        _activeActions%5BA_UP%5D = true;%0A
                 
@@ -3743,32 +3743,32 @@
 on(A_UP, true);%0A
-
                 
@@ -3822,32 +3822,87 @@
 irection.SOUTH:%0A
+                        _activeActions%5BA_DOWN%5D = true;%0A
                 
@@ -4018,32 +4018,87 @@
 Direction.WEST:%0A
+                        _activeActions%5BA_LEFT%5D = true;%0A
                 
@@ -4214,32 +4214,88 @@
 Direction.EAST:%0A
+                        _activeActions%5BA_RIGHT%5D = true;%0A
                 
@@ -4831,32 +4831,34 @@
   %7B%0A            
+/*
 if (button == Bu
@@ -5513,32 +5513,32 @@
 _RIGHT, false);%0A
-
                 
@@ -5568,32 +5568,312 @@
               %7D%0A
+            %7D*/%0A            %0A            for (int i = 0; i %3C _activeActions.length; ++i)%0A            %7B%0A                if (_activeActions%5Bi%5D)%0A                %7B%0A                    _commands.ExecuteAction(i, false);%0A                    _activeActions%5Bi%5D = false;%0A                %7D%0A
             %7D%0A  

Improve (or not) Control Pad.