using System.Text.Json.Serialization;

namespace View_by_Distance.Shared.Models;

[method: JsonConstructor]
public class Location(int bottom, double confidence, int left, int right, int top) : Properties.ILocation, IEquatable<Location>
{

    public int Bottom { init; get; } = bottom;
    public double Confidence { init; get; } = confidence;
    public int Left { init; get; } = left;
    public int Right { init; get; } = right;
    public int Top { init; get; } = top;

    public override bool Equals(object? obj) => Equals(obj as Location);

#pragma warning disable IDE0070
    public override int GetHashCode()
#pragma warning restore IDE0070
    {
        int hashCode = -773114317;
        hashCode = (hashCode * -1521134295) + Bottom.GetHashCode();
        hashCode = (hashCode * -1521134295) + Left.GetHashCode();
        hashCode = (hashCode * -1521134295) + Right.GetHashCode();
        hashCode = (hashCode * -1521134295) + Top.GetHashCode();
        return hashCode;
    }

    public bool Equals(Location? location)
    {
        return location is not null
            && Bottom == location.Bottom
            && Left == location.Left
            && Right == location.Right
            && Top == location.Top;
    }

    public static bool operator ==(Location location1, Location location2) => EqualityComparer<Location>.Default.Equals(location1, location2);

    public static bool operator !=(Location location1, Location location2) => !(location1 == location2);

    public static Location Get(int bottom, double confidence, int height, int left, int right, int top, int width, int zCount)
    {
        Location result = new(bottom, confidence, left, right, top);
        _ = Stateless.Location.Check(bottom, height, left, right, top, width, zCount, throwException: true);
        return result;
    }

}