package me.mariotti.tanker.messaging;

import android.util.Log;
import me.mariotti.tanker.TankActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class MessageEncoderDecoder {
    private final static String TAG = "MessageEncoderDecoder";
    // Android --> Arduino codes
    private final static int CMD_NULL_VALUE = 0;
    private final static int CMD_MOVE = 1;
    private final static int CMD_STOP = 2;
    private final static int CMD_LEFT = 3;
    private final static int CMD_RIGHT = 4;
    private final static int CMD_SHOOT = 5;
    private final static int CMD_SEARCH = 10;

    // Arduino --> Aandroid codes
    // Message types
    private final static int INFO = 0;
    private final static int STATE = 1;
    // States
    private final static int IDLE = 100;
    private final static int SEARCHING = 101;
    private final static int HUNTING = 102;
    private final static int EMERGENCY = 103;
    // Actions
    private final static int STOPPED = 150;
    private final static int MOVING = 151;
    // Infos
    private final static int SHOOTED = 201;
    private final static int RELOADED = 202;
    private final static int DISTANCE = 203;


    // Android --> Arduino messages

    public static String move(int motorLeft, int motorRight) {
        return (CMD_MOVE + "," + motorLeft + "," + motorRight);
    }

    public static String stop() {
        return (CMD_STOP + "," + CMD_NULL_VALUE + "," + CMD_NULL_VALUE);
    }

    public static String stop(long stopTime) {
        return (CMD_STOP + "," + stopTime + "," + CMD_NULL_VALUE);
    }

    public static String turnLeft() {
        return (CMD_LEFT + "," + CMD_NULL_VALUE + "," + CMD_NULL_VALUE);
    }

    public static String turnRight() {
        return (CMD_RIGHT + "," + CMD_NULL_VALUE + "," + CMD_NULL_VALUE);
    }

    public static String shoot() {
        return (CMD_SHOOT + "," + CMD_NULL_VALUE + "," + CMD_NULL_VALUE);
    }

    public static String search() {
        return (CMD_SEARCH + "," + CMD_NULL_VALUE + "," + CMD_NULL_VALUE);
    }

    // Arduino --> Android messages
    public static HashMap<String, Integer> decodeIncomingMessage(String mIncomingMessage) {
        if (TankActivity.DEBUG) {
            Log.i(TAG, "mIncomingMessage: " + mIncomingMessage);
        }
        HashMap<String, Integer> parsedMessage = new HashMap<String, Integer>(3);
        ArrayList<String> splittedMessage = new ArrayList<String>(Arrays.asList(mIncomingMessage.split(",")));
        int messageType = Integer.valueOf(splittedMessage.get(0));

        switch (messageType) {
            case INFO: {
                parsedMessage.put("MSG_TYPE", INFO);
                parsedMessage.put("INFO_MSG", Integer.valueOf(splittedMessage.get(1)));
                if (parsedMessage.get("INFO_MSG") == DISTANCE) {
                    parsedMessage.put("DISTANCE", Integer.valueOf(splittedMessage.get(2)));
                }
                parsedMessage.put("ERROR", 0);
                break;
            }
            case STATE: {
                parsedMessage.put("MSG_TYPE", STATE);
                parsedMessage.put("STATE", Integer.valueOf(splittedMessage.get(1)));
                if (parsedMessage.get("STATE") == HUNTING || parsedMessage.get("STATE") == SEARCHING) {
                    parsedMessage.put("ACTION", Integer.valueOf(splittedMessage.get(2)));
                }
                parsedMessage.put("ERROR", 0);
                break;
            }
            default: {
                parsedMessage.put("ERROR", -1);
            }
        }
        if (TankActivity.DEBUG) {
            Log.i(TAG, "parsedMessage: " + parsedMessage.toString());
        }
        return parsedMessage;
    }
}
@@ -397,20 +397,80 @@
 CMD_MOVE
- = 1
+_FORWARD = 1;%0A    private final static int CMD_MOVE_BACKWARD = 6
 ;%0A    pr
@@ -1326,16 +1326,24 @@
 ing move
+_forward
 (int mot
@@ -1392,16 +1392,176 @@
 CMD_MOVE
+_FORWARD + %22,%22 + motorLeft + %22,%22 + motorRight);%0A    %7D%0A%0A    public static String move_backward(int motorLeft, int motorRight) %7B%0A        return (CMD_MOVE_BACKWARD
  + %22,%22 +

Add Move_backward method