| | 1 | | using System.ComponentModel; |
| | 2 | |
|
| | 3 | | namespace MRA.Infrastructure.UserInput; |
| | 4 | |
|
| | 5 | | public class ConsoleProvider : IUserInputProvider |
| | 6 | | { |
| 1 | 7 | | private int PAD_RIGHT = 10; |
| 1 | 8 | | public bool ShowMessageType = true; |
| | 9 | |
|
| | 10 | |
|
| | 11 | | public void ShowMessageTrace(string message) |
| | 12 | | { |
| 0 | 13 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 14 | | Console.WriteLine(message); |
| 0 | 15 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 16 | | } |
| | 17 | | public void ShowMessageDebug(string message) |
| | 18 | | { |
| 0 | 19 | | Console.ForegroundColor = ConsoleColor.DarkGreen; |
| 0 | 20 | | Console.WriteLine(message); |
| 0 | 21 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 22 | | } |
| | 23 | | public void ShowMessageInfo(string message) |
| | 24 | | { |
| 0 | 25 | | Console.ForegroundColor = ConsoleColor.Cyan; |
| 0 | 26 | | Console.WriteLine(message); |
| 0 | 27 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | public void ShowMessageWarning(string message) |
| | 31 | | { |
| 0 | 32 | | Console.ForegroundColor = ConsoleColor.Yellow; |
| 0 | 33 | | Console.WriteLine(message); |
| 0 | 34 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public void ShowMessageSuccess(string message) |
| | 38 | | { |
| 0 | 39 | | Console.ForegroundColor = ConsoleColor.Green; |
| 0 | 40 | | Console.WriteLine(message); |
| 0 | 41 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 42 | | } |
| | 43 | | public void ShowMessageError(string message) |
| | 44 | | { |
| 0 | 45 | | Console.ForegroundColor = ConsoleColor.Red; |
| 0 | 46 | | Console.WriteLine(message); |
| 0 | 47 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 48 | | } |
| | 49 | | public void ShowMessageCritical(string message) |
| | 50 | | { |
| 0 | 51 | | var previous = Console.BackgroundColor; |
| 0 | 52 | | Console.BackgroundColor = ConsoleColor.Red; |
| 0 | 53 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 54 | | Console.WriteLine(message); |
| 0 | 55 | | Console.BackgroundColor = previous; |
| 0 | 56 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public void ShowMessagePrevious(bool isNew, string previous) |
| | 60 | | { |
| 0 | 61 | | if (!isNew) |
| | 62 | | { |
| 0 | 63 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 64 | | Console.Write("".PadRight(PAD_RIGHT)); |
| 0 | 65 | | Console.Write("[Default: '"); |
| | 66 | |
|
| 0 | 67 | | Console.ForegroundColor = ConsoleColor.Green; |
| 0 | 68 | | Console.Write(previous); |
| | 69 | |
|
| 0 | 70 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 71 | | Console.WriteLine("'][Leave empty to use it]"); |
| | 72 | |
|
| 0 | 73 | | Console.ForegroundColor = ConsoleColor.White; |
| | 74 | | } |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | public void ShowMessage(bool isNew, string previous, string field) |
| | 78 | | { |
| 0 | 79 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 80 | | ShowMessageInfo("------------------------------------------------------"); |
| 0 | 81 | | System.Console.WriteLine("INPUT:".PadRight(PAD_RIGHT) + field.ToUpper()); |
| 0 | 82 | | ShowMessagePrevious(isNew, previous); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public void ShowMessage(string field) |
| | 86 | | { |
| 0 | 87 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 88 | | System.Console.WriteLine(field); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | public string ReadValueFromConsole() |
| | 92 | | { |
| 0 | 93 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 94 | | Console.Write("> "); |
| 0 | 95 | | return Console.ReadLine(); |
| | 96 | | } |
| | 97 | |
|
| | 98 | | public string ReadValue(bool isNew, string previous) |
| | 99 | | { |
| | 100 | |
|
| 0 | 101 | | var input = ReadValueFromConsole(); |
| 0 | 102 | | if (!isNew && String.IsNullOrEmpty(input)) |
| | 103 | | { |
| 0 | 104 | | input = previous; |
| | 105 | | } |
| 0 | 106 | | ShowValueSet(input.ToString()); |
| 0 | 107 | | return input; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | public string ReadValue() |
| | 111 | | { |
| | 112 | |
|
| 0 | 113 | | var input = ReadValueFromConsole(); |
| 0 | 114 | | ShowValueSet(input.ToString()); |
| 0 | 115 | | return input; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | public int FillIntValue(bool isNew, int previous, string field, Dictionary<int, string> dictionary) |
| | 119 | | { |
| 0 | 120 | | ShowMessageInfo("------------------------------------------------------"); |
| 0 | 121 | | ShowMessageInfo(field.ToUpper() + ":"); |
| | 122 | |
|
| 0 | 123 | | foreach (var type in dictionary) |
| | 124 | | { |
| 0 | 125 | | ShowMessageInfo(type.Key.ToString().PadRight(5) + "= " + type.Value); |
| | 126 | | } |
| | 127 | |
|
| 0 | 128 | | if (!isNew) |
| | 129 | | { |
| 0 | 130 | | System.Console.WriteLine(" "); |
| 0 | 131 | | ShowMessagePrevious(isNew, dictionary[previous].ToString()); |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | var input = ReadValueFromConsole(); |
| 0 | 135 | | if (!isNew && String.IsNullOrEmpty(input)) |
| | 136 | | { |
| 0 | 137 | | ShowValueSet(dictionary[previous]); |
| 0 | 138 | | return previous; |
| | 139 | | } |
| | 140 | | else |
| | 141 | | { |
| 0 | 142 | | int.TryParse(input, out int numeroEntero); |
| 0 | 143 | | ShowValueSet(dictionary[numeroEntero]); |
| 0 | 144 | | return numeroEntero; |
| | 145 | | } |
| | 146 | | } |
| | 147 | |
|
| | 148 | | public int FillFreeIntValue(bool isNew, int previous, string field) |
| | 149 | | { |
| 0 | 150 | | ShowMessage(isNew, previous.ToString(), field); |
| | 151 | |
|
| 0 | 152 | | var input = ReadValueFromConsole(); |
| 0 | 153 | | if (!isNew && String.IsNullOrEmpty(input)) |
| | 154 | | { |
| 0 | 155 | | ShowValueSet(previous.ToString()); |
| 0 | 156 | | return previous; |
| | 157 | | } |
| | 158 | | else |
| | 159 | | { |
| 0 | 160 | | int.TryParse(input, out int numeroEntero); |
| 0 | 161 | | ShowValueSet(numeroEntero.ToString()); |
| 0 | 162 | | return numeroEntero; |
| | 163 | | } |
| | 164 | | } |
| | 165 | |
|
| | 166 | |
|
| | 167 | | public bool FillBoolValue(string field) |
| | 168 | | { |
| 0 | 169 | | ShowMessage(field); |
| 0 | 170 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 171 | | System.Console.WriteLine("".PadRight(PAD_RIGHT) + "[Y|y: 'True']"); |
| 0 | 172 | | Console.ForegroundColor = ConsoleColor.White; |
| | 173 | |
|
| 0 | 174 | | var input = ReadValueFromConsole(); |
| | 175 | |
|
| 0 | 176 | | bool value = false; |
| 0 | 177 | | if (input.ToLower().Equals("y")) |
| | 178 | | { |
| 0 | 179 | | value = true; |
| | 180 | | } |
| | 181 | |
|
| 0 | 182 | | ShowValueSet(value.ToString()); |
| 0 | 183 | | return value; |
| | 184 | | } |
| | 185 | |
|
| | 186 | | public bool FillBoolValue(bool isNew, bool previous, string field) |
| | 187 | | { |
| 0 | 188 | | ShowMessage(isNew, previous.ToString(), field); |
| 0 | 189 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 190 | | System.Console.WriteLine("".PadRight(PAD_RIGHT) + "[Y|y: 'True']"); |
| 0 | 191 | | Console.ForegroundColor = ConsoleColor.White; |
| | 192 | |
|
| 0 | 193 | | var input = ReadValueFromConsole(); |
| | 194 | |
|
| 0 | 195 | | if (!isNew && String.IsNullOrEmpty(input)) |
| | 196 | | { |
| 0 | 197 | | ShowValueSet(previous.ToString()); |
| 0 | 198 | | return previous; |
| | 199 | | } |
| | 200 | | else |
| | 201 | | { |
| 0 | 202 | | bool value = false; |
| 0 | 203 | | if (input.ToLower().Equals("y")) |
| | 204 | | { |
| 0 | 205 | | value = true; |
| | 206 | | } |
| | 207 | |
|
| 0 | 208 | | ShowValueSet(value.ToString()); |
| 0 | 209 | | return value; |
| | 210 | | } |
| | 211 | | } |
| | 212 | |
|
| | 213 | | public void ShowValueSet(string value) |
| | 214 | | { |
| 0 | 215 | | Console.WriteLine(""); |
| 0 | 216 | | Console.ForegroundColor = ConsoleColor.Green; |
| 0 | 217 | | Console.Write("VALUE ".PadRight(PAD_RIGHT)); |
| 0 | 218 | | Console.WriteLine((String.IsNullOrEmpty(value) ? "''" : value)); |
| 0 | 219 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 220 | | Console.WriteLine(""); |
| 0 | 221 | | } |
| | 222 | |
|
| | 223 | | public string FillStringValue(bool isNew, string previous, string field) |
| | 224 | | { |
| 0 | 225 | | ShowMessage(isNew, previous, field); |
| 0 | 226 | | return ReadValue(isNew, previous); |
| | 227 | | } |
| | 228 | |
|
| | 229 | | public string ReadStringValue(string prompt) |
| | 230 | | { |
| 0 | 231 | | ShowMessage(prompt); |
| 0 | 232 | | return ReadValue(); |
| | 233 | | } |
| | 234 | |
|
| | 235 | | public void PrintPropreties(object obj) |
| | 236 | | { |
| | 237 | | // Obtener todas las propiedades |
| 0 | 238 | | PropertyDescriptorCollection propiedades = TypeDescriptor.GetProperties(obj); |
| | 239 | |
|
| | 240 | | // Ordenar las propiedades por nombre |
| 0 | 241 | | var propiedadesOrdenadas = propiedades.Sort() |
| 0 | 242 | | .Cast<PropertyDescriptor>() |
| 0 | 243 | | .ToList(); |
| | 244 | |
|
| 0 | 245 | | var propiedadesOrdenadas2 = new PropertyDescriptorCollection(propiedadesOrdenadas.ToArray()); |
| | 246 | |
|
| 0 | 247 | | foreach (PropertyDescriptor descriptor in propiedadesOrdenadas2) |
| | 248 | | { |
| 0 | 249 | | string name = descriptor.Name; |
| 0 | 250 | | object value = descriptor.GetValue(obj) ?? ""; |
| 0 | 251 | | ShowMessageInfo(name.PadRight(20) + " = " + value); |
| | 252 | | } |
| 0 | 253 | | } |
| | 254 | |
|
| 0 | 255 | | public void ReadKey() => Console.ReadKey(); |
| | 256 | | } |