using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParseInput : MonoBehaviour {

    public UnityEngine.UI.Text outputField;
    public UnityEngine.UI.InputField inputField;

    public void Start()
    {
        inputField.onEndEdit.AddListener(delegate { OnNewInput(inputField.text); });
        // TODO: move this to wherever we start drone mode from
        inputField.Select();
        inputField.ActivateInputField();
    }
    
	public void OnNewInput(string input)
    {
        OutputController.instance.Log(input, OutputController.MessageType.Input);
        inputField.text = "";
        Parse(input);
        inputField.Select();
        inputField.ActivateInputField();
    }

    void Parse(string input)
    {
        // divide into tokens
        var tokens = input.Split(' ');
        int amount;
        for (int i = 0; i < tokens.Length; ++i)
            switch (tokens[i].ToLower())
            {
                case "":
                    break;
                case "f":
                case "fd":
                case "forward":
                case "advance":
                case "move":
                    float distance;
                    try
                    {
                        distance = float.Parse(tokens[i + 1]);
                        ++i;
                    }
                    catch (System.Exception)
                    { distance = 1; }
                    DroneController.active.Enqueue(new Move(distance));
                    break;
                case "rt":
                case "right":
                case "cw":
                case "clockwise":
                case "turn":
                    DroneController.active.Enqueue(new Turn(90, true));
                    break;
                case "lt":
                case "left":
                case "ccw":
                case "counterclockwise":
                case "unturn":
                    DroneController.active.Enqueue(new Turn(90, false));
                    break;
                case "pickup":
                    DroneController.active.Enqueue(new Pickup());
                    break;
                case "drain":
                case "consume":
                case "eat":
                case "charge":
                    DroneController.active.Enqueue(new Drain());
                    break;
                case "drop":
                case "release":
                    DroneController.active.Enqueue(new Drop());
                    break;
                case "inspect":
                case "look":
                case "l":
                    DroneController.active.Enqueue(new Inspect());
                    break;
                case "light":
                case "headlight":
                case "headlamp":
                case "toggle":
                    DroneController.active.Enqueue(new LightToggle());
                    break;
                case "jump":
                case "hyper":
                case "hyperjump":
                case "hyperdrive":
                case "warp":
                case "j":
                    DroneController.active.Enqueue(new JumpUI());
                    break;
                case "render":
                case "photo":
                case "r":
                    DroneController.active.Enqueue(new Render());
                    break;
                case "deposit":
                case "AC":
                case "chargeAC":
                    try
                    {
                        amount = int.Parse(tokens[i + 1]);
                        ++i;
                    }
                    catch (System.Exception)
                    { amount = 1; }
                    DroneController.active.Enqueue(new Deposit(amount));
                    break;
                case "transfer":
                case "refuel":
                case "pump":
                    try
                    {
                        amount = int.Parse(tokens[i + 1]);
                        ++i;
                    }
                    catch (System.Exception)
                    { amount = 1; }
                    DroneController.active.Enqueue(new Refuel(amount));
                    break;
                case "crush":
                case "hammer":
                case "destroy":
                case "d":
                    DroneController.active.Enqueue(new Crush());
                    break;
                case "stop":
                case "cancel":
                case "halt":
                case "s":
                    DroneController.active.StopAction();
                    break;
                case "h":
                case "help":
                case "assist":
                case "assistance":
                case "what":
                case "how":
                    OutputController.instance.Log(
@"COMMAND        | COST
help
deposit [amount]| *kJ
render          | 1kJ

# movement
fd [distance]   | *kJ
rt / lt         | 1kJ
jump            | *kJ

# advanced cpu
light           | 0kJ
stop            | 0kJ
# cable         
charge          | 0kJ
# grabber       
pickup          | 1kJ
inspect         | 1kJ
drop            | 0kJ
# hammer        
crush           | 5kJ
# refueler
transfer [amount] *kJ",
OutputController.MessageType.Output);
                    i += 1000;
                    break;
                default:
                    OutputController.instance.Log("Unknown Command: " + tokens[i] + ". Input 'help' to receive assistance.", OutputController.MessageType.Error);
                    break;
            }
    }

}
