using System.Diagnostics;
using RGB.NET.Core;

namespace RGB.NET.Devices.Asus
{
    /// <inheritdoc />
    /// <summary>
    /// Represents a Id of a <see cref="T:RGB.NET.Core.Led" /> on a <see cref="T:RGB.NET.Devices.Asus.AsusRGBDevice" />.
    /// </summary>
    [DebuggerDisplay("{" + nameof(LedId) + "}")]
    public class AsusLedId : ILedId
    {
        #region Properties & Fields

        internal readonly AsusLedIds LedId;

        internal readonly int Index;

        /// <inheritdoc />
        public IRGBDevice Device { get; }

        /// <inheritdoc />
        public bool IsValid => LedId != AsusLedIds.Invalid;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="AsusLedId"/> class.
        /// </summary>
        /// <param name="device">The <see cref="IRGBDevice"/> the <see cref="ILedId"/> belongs to.</param>
        /// <param name="ledId">The <see cref="AsusLedId"/> of the represented <see cref="Led"/>.</param>
        public AsusLedId(IRGBDevice device, AsusLedIds ledId)
        {
            this.Device = device;
            this.LedId = ledId;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsusLedId"/> class.
        /// </summary>
        /// <param name="device">The <see cref="IRGBDevice"/> the <see cref="ILedId"/> belongs to.</param>
        /// <param name="ledId">The <see cref="AsusLedId"/> of the represented <see cref="Led"/>.</param>
        /// <param name="index">The index in the mapping array of the device.</param>
        public AsusLedId(IRGBDevice device, AsusLedIds ledId, int index)
        {
            this.Device = device;
            this.LedId = ledId;
            this.Index = index;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Converts the Id of this <see cref="AsusLedId"/> to a human-readable string.
        /// </summary>
        /// <returns>A string that contains the Id of this <see cref="AsusLedId"/>. For example "Enter".</returns>
        public override string ToString() => LedId.ToString();

        /// <summary>
        /// Tests whether the specified object is a <see cref="AsusLedId" /> and is equivalent to this <see cref="AsusLedId" />.
        /// </summary>
        /// <param name="obj">The object to test.</param>
        /// <returns><c>true</c> if <paramref name="obj" /> is a <see cref="AsusLedId" /> equivalent to this <see cref="AsusLedId" />; otherwise, <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            AsusLedId compareLedId = obj as AsusLedId;
            if (ReferenceEquals(compareLedId, null))
                return false;

            if (ReferenceEquals(this, compareLedId))
                return true;

            if (GetType() != compareLedId.GetType())
                return false;

            return compareLedId.LedId == LedId;
        }

        /// <summary>
        /// Returns a hash code for this <see cref="AsusLedId" />.
        /// </summary>
        /// <returns>An integer value that specifies the hash code for this <see cref="AsusLedId" />.</returns>
        public override int GetHashCode() => LedId.GetHashCode();

        #endregion

        #region Operators

        /// <summary>
        /// Returns a value that indicates whether two specified <see cref="AsusLedId" /> are equal.
        /// </summary>
        /// <param name="ledId1">The first <see cref="AsusLedId" /> to compare.</param>
        /// <param name="ledId2">The second <see cref="AsusLedId" /> to compare.</param>
        /// <returns><c>true</c> if <paramref name="ledId1" /> and <paramref name="ledId2" /> are equal; otherwise, <c>false</c>.</returns>
        public static bool operator ==(AsusLedId ledId1, AsusLedId ledId2) => ReferenceEquals(ledId1, null) ? ReferenceEquals(ledId2, null) : ledId1.Equals(ledId2);

        /// <summary>
        /// Returns a value that indicates whether two specified <see cref="AsusLedId" /> are equal.
        /// </summary>
        /// <param name="ledId1">The first <see cref="AsusLedId" /> to compare.</param>
        /// <param name="ledId2">The second <see cref="AsusLedId" /> to compare.</param>
        /// <returns><c>true</c> if <paramref name="ledId1" /> and <paramref name="ledId2" /> are not equal; otherwise, <c>false</c>.</returns>
        public static bool operator !=(AsusLedId ledId1, AsusLedId ledId2) => !(ledId1 == ledId2);

        #endregion
    }
}
