namespace OubHackMS.OConsole
{
    using OubHackMS.Core;
    using OubHackMS.Logging;
    using OubHackMS.OConsole.ConsoleCommands;
    using System;
    using System.Collections.Generic;
    using System.Threading;

    internal class ConsoleHandler : LinkedClass
    {
        private List<CommandPackage> lPackages;

        public ConsoleHandler()
        {
            ThreadStart start = null;
            ThreadStart start2 = null;
            this.lPackages = new List<CommandPackage>();
            if (start == null)
            {
                start = delegate {
                    Thread.Sleep(100);
                    this.registerCommandPackage(new User(base.parent));
                    this.registerCommandPackage(new Crypto(base.parent));
                };
            }
            new Thread(start).Start();
            if (start2 == null)
            {
                start2 = delegate {
                    while (true)
                    {
                        this.onCommand(Console.ReadLine());
                    }
                };
            }
            new Thread(start2).Start();
        }

        private void onCommand(string sCommand)
        {
            string[] strArray = sCommand.Split(new char[] { ' ' });
            for (int i = 0; i < this.lPackages.Count; i++)
            {
                CommandPackage package = this.lPackages[i];
                string key = "";
                for (int j = 0; j < strArray.Length; j++)
                {
                    key = key + strArray[j] + "/";
                    if (package.dCommands.ContainsKey(key))
                    {
                        string[] strArray2 = new string[(strArray.Length - j) - 1];
                        for (int k = j + 1; k <= (strArray.Length - 1); k++)
                        {
                            strArray2[(k - j) - 1] = strArray[k];
                        }
                        package.dCommands[key](sCommand, strArray2);
                        return;
                    }
                }
            }
            base.parent.Log(Level.PRINT, new object[] { "Command or module '" + strArray[0] + "' not found" });
        }

        private void registerCommandPackage(CommandPackage cPackage)
        {
            this.lPackages.Add(cPackage);
        }
    }
}

