using Console.Waterworks.Core.Assistants; using Console.Waterworks.Core.Constants; using Console.Waterworks.Core.Loggers; using Console.Waterworks.Core.Models; using Console.Waterworks.Core.Specialists; using System; namespace Console.Waterworks.Core.CoOrdinators { class CoOrdinator { CoOrdinatorAssistant _assistant = new CoOrdinatorAssistant(); ProgramInfoSpecialist _progInfoSpec = new ProgramInfoSpecialist(); ConsoleIOSpecialist _consoleSpec = new ConsoleIOSpecialist(); CommandsSpecialist _commSpec = new CommandsSpecialist(); HelpSpecialist _helpSpec = new HelpSpecialist(); CW_Logger _logger = new CW_Logger(); internal void PrepareConsoleEnvironment() { _logger.LogInfoMessage($"Preparing console.."); _consoleSpec.PrepareConsoleEnvironment(); _consoleSpec.SetInputPrompt($"{_progInfoSpec.GetProductName()}> "); _logger.LogSuccessMessage("Console environment is now setup"); } internal void DisplayProgramInfo() { _logger.LogInfoMessage($"Displaying program information.."); _assistant.SetConsoleTitle(_logger, _progInfoSpec, _consoleSpec); _assistant.OutputProgramInfo(_logger, _progInfoSpec, _consoleSpec); } internal void RunProgram(string commandsNamespace) { _logger.LogInfoMessage("Attempting to building commands library.."); var commandClasses = _commSpec.GetCommandClasses(commandsNamespace); var commandLibraries = _commSpec.GetCommandLibraries(commandClasses); _assistant.LogCommandGatheringAttempt(commandClasses, commandLibraries, _logger); while (true) { var consoleInput = _consoleSpec.GetInputFromUser(); if (string.IsNullOrEmpty(consoleInput)) continue; try { _logger.LogInfoMessage("Parsing input from user.."); var command = new Command(consoleInput, commandsNamespace, CW_Constants.COMMAND_CLASS_NAME); _logger.LogInfoMessage("Attempting to execute command.."); var result = _commSpec.ExecuteCommand(commandsNamespace, command, commandClasses, commandLibraries, _consoleSpec); _consoleSpec.WriteOutputToConsole(result); _logger.LogSuccessMessage("Command has been executed."); _logger.LogNoteMessage("An error message does not mean the command did not execute properly or sucessfully."); } catch (Exception ex) { _logger.LogErrorMessage("Command was not successfully executed. See the error message in console for further details"); _consoleSpec.WriteErrorMessage(ex.Message); } _logger.LogInfoMessage("Resetting the console's formatting.."); _logger.LogNoteMessage("This is to make sure no error messages or one-off formating change corrupts the console environment."); _consoleSpec.ResetConsoleColour(); _logger.LogSuccessMessage("Console's formating has been reset"); } } internal string DisplayHelpSection(string commandsNamespace) { _logger.LogInfoMessage("Attempting to display help section.."); var commandClasses = _commSpec.GetCommandClasses(commandsNamespace); if (commandClasses.Count == 0) { _logger.LogErrorMessage("Unable to find any help information. Make sure the commands hace the correct atrributes applied"); _consoleSpec.WriteErrorSuffix(); return "Unable to find help information"; } _logger.LogSuccessMessage("Found help information"); _logger.LogInfoMessage("Attempting to display the help information.."); _consoleSpec.WriteOutputToConsole("Displaying Help section..."); _consoleSpec.LineBreak(); var commandMembers = _helpSpec.GetCommandMembers(commandClasses); foreach (var command in commandMembers) { if (_helpSpec.ListCommand(command) == true) { _consoleSpec.WriteOutputToConsole($"Command Name: {command.Name}"); _consoleSpec.WriteOutputToConsole($"Parameters: {_helpSpec.GetParametres(command)}"); _consoleSpec.WriteOutputToConsole($"Description: {_helpSpec.GetDescription(command)}"); _consoleSpec.WriteOutputToConsole($"Example: {_helpSpec.GetUsageExamples(command)}"); _consoleSpec.LineBreak(); } } _logger.LogSuccessMessage("Help section displayed in the console"); return "End of Help section."; } } }