using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EA.GameTalk;
using EA.GameTalkMessages;


namespace GametalkConsole
{
    class Program
    {
        static bool GetArgument(string id, string[] args, out string result)
        {
            for (int i = 0; i < args.Length; i++)
            {
                string argtag = "-" + id + ":";
                if (args[i].StartsWith(argtag))
                {
                    result = args[i].Substring(argtag.Length);
                    return true;
                }
            }
            result = "";
            return false;
        }


        static void Main(string[] args)
        {
            bool validArguments = true;

            string channel, target, key, stringdata;
            validArguments &= GetArgument("c", args, out channel);
            validArguments &= GetArgument("t", args, out target);
            validArguments &= GetArgument("k", args, out key);
            validArguments &= GetArgument("s", args, out stringdata);


            if (!validArguments)
            {
                Console.Write("Simple gametalk application to send string based key/data pairs to gametalk enabled applications.\n");
                Console.Write("\n");
                Console.Write("Parameters:\n");
                Console.Write("     -c : Gametalk Channel to broadcast on\n");
                Console.Write("     -t : Target of Message\n");
                Console.Write("     -k : Message key/identifier\n");
                Console.Write("     -s : Message data as string\n");
                Console.Write(" Example: GametalkConsole.exe -c:Fifa -t:Game.PC -k:ConsoleCommand -s:reload\n");
                return;
            }

            GameTalkManager.Instance.Initialize("GametalkConsole");

            GameTalkMessage msg = new GameTalkMessage(channel);
            GameTalkMessage.Format type = GameTalkMessage.Format.String;
            
            msg.AddKeyContent(key, stringdata, type);

            GameTalkManager.Instance.Send(target, msg);

            GameTalkManager.Instance.Shutdown();
        }
    }
}
