using System;
using Tesseract;

namespace Ocr.Tesseract.Models;

/// <summary>
/// Model for <see cref="Word"/> choices
/// </summary>
public class Choice : IEquatable<Choice>
{
  /// <summary>
  /// Constructor
  /// </summary>
  /// <param name="text"></param>
  /// <param name="confidence"></param>
  public Choice(string text, float confidence)
  {
    Text = text;
    Confidence = confidence;
  }

  /// <summary>
  /// Scanned text
  /// </summary>
  public string Text { get; set; }

  /// <summary>
  /// Confidence that this <see cref="Choice"/> is the correct one
  /// </summary>
  public float Confidence { get; set; }

  /// <summary>
  /// Creates a <see cref="Choice"/> object from a <see cref="ChoiceIterator"/>
  /// </summary>
  public static Choice Parse(ChoiceIterator iterator)
  {
    return new Choice
    (
      iterator.GetText(),
      iterator.GetConfidence()
    );
  }

  #region Overrides of Object

  /// <inheritdoc />
  public override string ToString()
  {
    return Text;
  }

  #endregion

  #region Equality members

  /// <inheritdoc />
  public bool Equals(Choice? other)
  {
    if (ReferenceEquals(null, other))
    {
      return false;
    }

    if (ReferenceEquals(this, other))
    {
      return true;
    }

    return Text == other.Text;
  }

  /// <inheritdoc />
  public override bool Equals(object? obj)
  {
    if (ReferenceEquals(null, obj))
    {
      return false;
    }

    if (ReferenceEquals(this, obj))
    {
      return true;
    }

    if (obj.GetType() != this.GetType())
    {
      return false;
    }

    return Equals((Choice)obj);
  }

  /// <inheritdoc />
  public override int GetHashCode()
  {
    return HashCode.Combine(Text);
  }

  public static bool operator ==(Choice? left, Choice? right)
  {
    return Equals(left, right);
  }

  public static bool operator !=(Choice? left, Choice? right)
  {
    return !Equals(left, right);
  }

  #endregion
}
