using System.Text.Json;
using System.Text.RegularExpressions;

namespace Ocr.Report.Models;

public struct ScanFileInfo
{
  public string Path { get; private init; }

  private static readonly Regex parseRegex = new(
    @"(?'image'.+)\.(?'processor'.+)\..+",
    RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase
  );

  public string ProcessorName { get; set; }

  public string ImageName { get; set; }

  public (double Elapsed, ICollection<string> Words) GetData()
  {
    using var file = File.OpenRead(Path);
    var root = JsonDocument.Parse(file).RootElement;

    var words = root
      .GetProperty("Words")
      .EnumerateArray()
      .Select(e => e.GetProperty("Text").GetString() ?? string.Empty)
      .ToArray();

    var elapsed = root.GetProperty("Elapsed").GetDouble();

    return (elapsed, words);
  }

  public static ScanFileInfo FromPath(string path)
  {
    var match = parseRegex.Match(System.IO.Path.GetFileName(path));
    return new ScanFileInfo
    {
      Path = path,
      ProcessorName = match.Groups["processor"].Value,
      ImageName = match.Groups["image"].Value
    };
  }

  /// <inheritdoc />
  public override string ToString() => ImageName;
}
