using System.Globalization;
namespace Umbraco.Cms.Core.Models;
/// <summary>
/// Represents a range with a minimum and maximum value.
/// </summary>
/// <typeparam name="T">The type of the minimum and maximum values.</typeparam>
/// <seealso cref="System.IEquatable{Umbraco.Core.Models.Range{T}}" />
public class Range<T> : IEquatable<Range<T>>
where T : IComparable<T>
{
/// Gets or sets the minimum value.
/// <value>
/// The minimum value.
/// </value>
public T? Minimum { get; set; }
/// Gets or sets the maximum value.
/// The maximum value.
public T? Maximum { get; set; }
/// Indicates whether the current object is equal to another object of the same type.
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// <see langword="true" /> if the current object is equal to the <paramref name="other" /> parameter; otherwise,
/// <see langword="false" />.
/// </returns>
public bool Equals(Range<T>? other) => other != null && Equals(other.Minimum, other.Maximum);
/// Returns a <see cref="string" /> that represents this instance.
/// A <see cref="string" /> that represents this instance.
public override string ToString() => ToString("{0},{1}", CultureInfo.InvariantCulture);
/// <param name="format">
/// A composite format string for a single value (minimum and maximum are equal). Use {0} for the
/// minimum and {1} for the maximum value.
/// </param>
/// <param name="formatRange">
/// A composite format string for the range values. Use {0} for the minimum and {1} for the
/// maximum value.
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
public string ToString(string format, string formatRange, IFormatProvider? provider = null) =>
ToString(Minimum?.CompareTo(Maximum) == 0 ? format : formatRange, provider);
/// A composite format string for the range values. Use {0} for the minimum and {1} for the maximum
/// value.
public string ToString(string format, IFormatProvider? provider = null) =>
string.Format(provider, format, Minimum, Maximum);
/// Determines whether this range is valid (the minimum value is lower than or equal to the maximum value).
/// <c>true</c> if this range is valid; otherwise, <c>false</c>.
public bool IsValid() => Minimum?.CompareTo(Maximum) <= 0;
/// Determines whether this range contains the specified value.
/// <param name="value">The value.</param>
/// <c>true</c> if this range contains the specified value; otherwise, <c>false</c>.
public bool ContainsValue(T? value) => Minimum?.CompareTo(value) <= 0 && value?.CompareTo(Maximum) <= 0;
/// Determines whether this range is inside the specified range.
/// <param name="range">The range.</param>
/// <c>true</c> if this range is inside the specified range; otherwise, <c>false</c>.
public bool IsInsideRange(Range<T> range) =>
IsValid() && range.IsValid() && range.ContainsValue(Minimum) && range.ContainsValue(Maximum);
/// Determines whether this range contains the specified range.
/// <c>true</c> if this range contains the specified range; otherwise, <c>false</c>.
public bool ContainsRange(Range<T> range) =>
IsValid() && range.IsValid() && ContainsValue(range.Minimum) && ContainsValue(range.Maximum);
/// Determines whether the specified <see cref="object" />, is equal to this instance.
/// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
/// <c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.
public override bool Equals(object? obj) => obj is Range<T> other && Equals(other);
/// Determines whether the specified <paramref name="minimum" /> and <paramref name="maximum" /> values are equal to
/// this instance values.
/// <param name="minimum">The minimum value.</param>
/// <param name="maximum">The maximum value.</param>
/// <c>true</c> if the specified <paramref name="minimum" /> and <paramref name="maximum" /> values are equal to this
/// instance values; otherwise, <c>false</c>.
public bool Equals(T? minimum, T? maximum) => Minimum?.CompareTo(minimum) == 0 && Maximum?.CompareTo(maximum) == 0;
/// Returns a hash code for this instance.
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
public override int GetHashCode() => (Minimum, Maximum).GetHashCode();
}