using Nickvision.Desktop.Globalization;
using System;

namespace Nickvision.Parabolic.Shared.Models;

public class SubtitleLanguage : IComparable<SubtitleLanguage>, IEquatable<SubtitleLanguage>
{
    public string Language { get; }
    public bool IsAutoGenerated { get; }

    public SubtitleLanguage(string language, bool isAutoGenerated)
    {
        Language = language;
        IsAutoGenerated = isAutoGenerated;
    }

    public int CompareTo(SubtitleLanguage? other)
    {
        if (other is null)
        {
            return 1;
        }
        if (IsAutoGenerated && !other.IsAutoGenerated)
        {
            return 1;
        }
        else if (!IsAutoGenerated && other.IsAutoGenerated)
        {
            return -1;
        }
        return Language.CompareTo(other.Language);
    }

    public override bool Equals(object? obj)
    {
        if (obj is SubtitleLanguage other)
        {
            return Equals(other);
        }
        return false;
    }

    public bool Equals(SubtitleLanguage? other) => other is not null && Language == other.Language && IsAutoGenerated == other.IsAutoGenerated;

    public override int GetHashCode() => HashCode.Combine(Language, IsAutoGenerated);

    public override string ToString() => ToString(null);

    public string ToString(ITranslationService? translator) => IsAutoGenerated ? (translator?._("{0} (Auto-generated)", Language) ?? $"{Language} (Auto-generated)") : Language;

    public static bool operator >(SubtitleLanguage left, SubtitleLanguage right) => left.CompareTo(right) > 0;

    public static bool operator <(SubtitleLanguage left, SubtitleLanguage right) => left.CompareTo(right) < 0;

    public static bool operator >=(SubtitleLanguage left, SubtitleLanguage right) => left.CompareTo(right) >= 0;

    public static bool operator <=(SubtitleLanguage left, SubtitleLanguage right) => left.CompareTo(right) <= 0;

    public static bool operator ==(SubtitleLanguage left, SubtitleLanguage right) => left.Equals(right);

    public static bool operator !=(SubtitleLanguage left, SubtitleLanguage right) => !left.Equals(right);
}
