namespace OubHackMS.Core
{
    using OubHackMS.Logging;
    using OubHackMS.MySQL;
    using OubHackMS.OConsole;
    using OubHackMS.Servers;
    using OubHackMS.Users;
    using System;
    using System.Collections.Generic;

    internal class OubHackMS
    {
        public Dictionary<string, LinkedClass> LinkedClasses = new Dictionary<string, LinkedClass>();

        public OubHackMS()
        {
            this.AddClass("Logger", new Logger());
            this.AddClass("MySQL", new OubHackMS.MySQL.MySQL());
            this.AddClass("UserDB", new UserDB());
            this.AddClass("ServerHandler", new Server());
            this.AddClass("Console", new ConsoleHandler());
        }

        private void AddClass(string sName, LinkedClass _class)
        {
            try
            {
                _class.Link(this);
                this.LinkedClasses.Add(sName, _class);
            }
            catch (Exception exception)
            {
                this.Log(Level.LOG_CONSOLE_ERROR, new object[] { "Exception occured whilst adding " + sName + "! - " + exception.Message });
                this.Log(Level.LOG_FILE_ERROR, new object[] { "Exception occured whilst adding " + sName + "!  -  " + exception.ToString() });
            }
            this.Log(Level.PRINT, new object[] { sName + " loaded!" });
        }

        public LinkedClass GetClass(string sClass)
        {
            if (!this.LinkedClasses.ContainsKey(sClass))
            {
                return null;
            }
            return this.LinkedClasses[sClass];
        }

        public void Log(Level level, params object[] oArgs)
        {
            if (this.GetClass("Logger") != null)
            {
                ((Logger) this.GetClass("Logger")).Log(level, oArgs);
            }
        }
    }
}

