using System.Text;

namespace Ocr.Cli.Monitor;

public class CliTaskMonitor : TaskMonitor
{
  /// <inheritdoc />
  public CliTaskMonitor(IEnumerable<(string? Name, Task Task)> tasks) : base(tasks) { }

  /// <inheritdoc />
  public CliTaskMonitor(IEnumerable<Task> tasks) : base(tasks) { }

  #region Overrides of TaskMonitor

  /// <inheritdoc />
  protected override void OnUpdate(ICollection<(string? Name, Task Task)> tasks)
  {
    var sb = new StringBuilder(tasks.Count * 30);

    foreach (var (name, task) in tasks)
    {
      sb.Append($"{StatusMap[task.Status],-5}");

      if (name is not null)
      {
        sb.Append(": ")
          .Append(name);
      }

      sb.AppendLine();

      if (task.Exception is not null)
      {
        sb.Append("> EX: ")
          .AppendLine(task.Exception.Message);
      }
    }

    sb.AppendLine();

    Console.Clear();
    Console.Write(sb.ToString());
  }

  #endregion
}