using System.Text; using CommandHandler.Commands; namespace CommandHandler.Navigation.States; internal abstract class NavigationStateBase : INavigationState, INavigationDrawer { private INavigationContext? _context; protected readonly IDictionary> Commands = new Dictionary>(); protected ICommand ChangeNavigationState(INavigationState newState) { if (_context == null) { throw new InvalidOperationException(); } var commands = new List { new ChangeNavigationStateCommand(_context, newState) }; if (newState is INavigationDrawer drawer) { commands.Add(new DrawMenuCommand(drawer)); } return new CombinedCommand([.. commands]); } public void SetContext(INavigationContext context) { _context = context; } public ICommand GetCommand(string message) { var parts = message.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (parts.Length <= 0) { return new EmptyCommand(); } if (Commands.TryGetValue(parts[0], out var commandFactory)) { return commandFactory(); } if (this is INavigationDrawer navigationDrawer) { return new DrawMenuCommand(navigationDrawer); } throw new NotImplementedException(); } public string DrawNavigationMenu() { var builder = new StringBuilder(); foreach (var command in Commands.Keys) { builder.AppendLine(command); } return builder.ToString(); } }