using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
using UnityEngine.Networking;

public struct Command
{
	public int playerId;
	public string commandId;
	public string[] parameters;
}

public class Commands : MonoBehaviour
{
    public Dictionary<string, Func<Command, bool>> cmds;
    protected Game myGame;

    void Awake()
    {
        myGame = GetComponent<Game>();
        RegisterCommands();
    }

    public bool RunCommand(Command command)
    {
        if(command.parameters.Length == 0)
            return false;

        if(cmds.ContainsKey(command.commandId))
        {
			cmds[command.commandId].Invoke(command);
        }

        return true;
    }

    bool Move(Command command)
    {
		//Debug.Assert(command.parameters.Length >= 2, "Move requires two parameters (MOVE [name] [direction]).");
		//Debug.Assert(Member.AllMembers != null, "Members dictionary is null.");

		if(command.parameters.Length < 2)
			return false;

		if(Member.AllMembers == null)
			return false;

        string memberName = command.parameters[0];
        string direction = command.parameters[1];

		// check to ensure this player has the team member
		if(Game.CurrentGame.Players[command.playerId].myTeam.Members.ContainsKey(memberName))
        {
			var memberToMove = Member.AllMembers[memberName];
			var directionToMove = Utilities.ConvertDirectionString(direction);

            memberToMove.Move(directionToMove);

			return true;
        }

        return false;
    }

    void RegisterCommands()
    {
        cmds = new Dictionary<string, Func<Command, bool>>();
        // NOTE: keep all commands lowercase :)
        cmds.Add("move", Move);

    }
}
