using Console.Waterworks.Core.Assistants; using Console.Waterworks.Core.Models; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Console.Waterworks.Core.Specialists { class CommandsSpecialist { CommandsAssistant _assistant = new CommandsAssistant(); internal List GetCommandClasses(string commandsNameSpace) { string theNamespace = commandsNameSpace; var q = from t in Assembly.GetEntryAssembly().GetTypes() where t.GetTypeInfo().IsClass && t.Namespace == theNamespace select t; return q.ToList(); } internal Dictionary>> GetCommandLibraries(List commandClasses) { var commandLibraries = new Dictionary>>(); foreach (var commandClass in commandClasses) { var methods = commandClass.GetMethods(BindingFlags.Static | BindingFlags.Public); var methodDictionary = new Dictionary>(); foreach (var method in methods) { string commandName = method.Name; methodDictionary.Add(commandName, method.GetParameters()); } commandLibraries.Add(commandClass.Name, methodDictionary); } return commandLibraries; } internal string ExecuteCommand(string commandsNamespace, Command command, List commandClasses, Dictionary>> commandLibraries, ConsoleIOSpecialist consoleSpec) { var classValidated = _assistant.ValidateClass(command, commandLibraries); var commandNameValidated = _assistant.ValidateCommand(command, commandLibraries); Dictionary> methodDictionary; if (classValidated != true || commandNameValidated != true) return _assistant.ExecuteBadCommandProcedure(command, consoleSpec); else methodDictionary = commandLibraries[command.ClassName]; var paramInfoList = methodDictionary[command.Name].ToList(); var paramsValidated = _assistant.ValidateParamArguments(command, paramInfoList); if (paramsValidated == false) return _assistant.ExecuteMissingArgumentProcedure(command, paramInfoList, consoleSpec); var methodParametreValueList = _assistant.GetParametreValueList(command, paramInfoList); var typeInfo = _assistant.BuildCommandLibraryClass(command, commandsNamespace); var inputArguments = _assistant.GetInputArguments(methodParametreValueList); var result = _assistant.InvokeCommand(command, typeInfo, inputArguments); return result; } } }