using System;

namespace Volpe.Cafe.Generic
{
    /// <summary>
    /// Represents an object that stores modeling data for each regulatory class.
    /// <br/>
    /// This object supports and operates on all regulatory classes as defined by <see cref="RegClasses.List"/>.
    /// </summary>
    [Serializable]
    public class RCValue<T> : ICloneable
    {
        #region <<< Constructors >>>

        /// <summary>
        /// Initializes a new instance of the <see cref="RCValue{T}"/> class.
        /// </summary>
        public RCValue() : this(new T[RegClasses.List.Length]) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="RCValue{T}"/> class, using the specified initializeT method
        /// for instantiating each element of type T to a specific initial value.
        /// </summary>
        /// <param name="initializeT">
        ///     Encapsulates a method that will handle instantiation for each element of type T within this instance.
        /// </param>
        public RCValue(Func<T> initializeT) : this()
        {
            GenericsHelper.Initialize(this.Items, initializeT);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RCValue{T}"/> class using the specified values.
        /// </summary>
        public RCValue(T domesticCar, T importedCar, T lightTruck, T hdpuv)
            : this(new T[] { domesticCar, importedCar, lightTruck, hdpuv }) { }

        RCValue(T[] items)
        {
            this.Items = items;
        }

        #endregion

        #region <<< Properties >>>

        /// <summary>Gets or sets the modeling data value based on the given <see cref="RegulatoryClass"/>.</summary>
        /// <exception cref="IndexOutOfRangeException">
        ///     The regClass specified does not represent a valid <see cref="RegulatoryClass"/>.
        /// </exception>
        public T this[RegulatoryClass regClass]
        {
            get => this.Items[GetIndex(regClass)];
            set => this.Items[GetIndex(regClass)] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the <see cref="RegulatoryClass.DomesticCar"/>
        /// regulatory class.
        /// </summary>
        public T DomesticCar
        {
            get => this.Items[RegClasses.DomesticCarIndex];
            set => this.Items[RegClasses.DomesticCarIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the <see cref="RegulatoryClass.ImportedCar"/>
        /// regulatory class.
        /// </summary>
        public T ImportedCar
        {
            get => this.Items[RegClasses.ImportedCarIndex];
            set => this.Items[RegClasses.ImportedCarIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the <see cref="RegulatoryClass.LightTruck"/>
        /// regulatory class.
        /// </summary>
        public T LightTruck
        {
            get => this.Items[RegClasses.LightTruckIndex];
            set => this.Items[RegClasses.LightTruckIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the <see cref="RegulatoryClass.HDPUV"/>
        /// regulatory class.
        /// </summary>
        public T HDPUV
        {
            get => this.Items[RegClasses.HdpuvIndex];
            set => this.Items[RegClasses.HdpuvIndex] = value;
        }

        /// <summary>Gets an array of modeling data specified by <see cref="RegulatoryClass"/>.</summary>
        public T[] Items { get; private set; }

        #endregion

        #region <<< Methods >>>

        #region < Overloaded Operators >

        /// <summary>
        /// Determines whether the specified two values are equal.
        /// </summary>
        /// <param name="value1">The first value to compare.</param>
        /// <param name="value2">The second value to compare.</param>
        /// <returns>true, if the two values are equal; false, otherwise.</returns>
        public static bool operator ==(RCValue<T> value1, RCValue<T> value2)
        {
            return Equals(value1, value2);
        }
        /// <summary>
        /// Determines whether the specified two values are not equal.
        /// </summary>
        /// <param name="value1">The first value to compare.</param>
        /// <param name="value2">The second value to compare.</param>
        /// <returns>true, if the two values are not equal; false, otherwise.</returns>
        public static bool operator !=(RCValue<T> value1, RCValue<T> value2)
        {
            return !Equals(value1, value2);
        }

        #endregion

        #region < ICloneable Members and Other Clone Methods >

        /// <summary>
        /// Creates a shallow copy of the current <see cref="RCValue{T}"/> instance.
        /// </summary>
        /// <returns>A new object that is a copy of this <see cref="RCValue{T}"/>.</returns>
        object ICloneable.Clone()
        {
            return this.Clone(null);
        }
        /// <summary>
        /// Creates a shallow copy of the current <see cref="RCValue{T}"/> instance.
        /// </summary>
        /// <returns>A new object that is a copy of this <see cref="RCValue{T}"/>.</returns>
        public RCValue<T> Clone()
        {
            return this.Clone(null);
        }
        /// <summary>
        /// Creates a copy of the current <see cref="RCValue{T}"/> instance, optionally using the specified method for
        /// cloning each element of type T within this instance.
        /// </summary>
        /// <param name="cloneT">
        ///     Encapsulates a method that will handle cloning for each element of type T within this instance.
        /// </param>
        /// <returns>A new object that is a copy of this <see cref="RCValue{T}"/>.</returns>
        public RCValue<T> Clone(Func<T, T> cloneT)
        {
            RCValue<T> value = new RCValue<T>();
            this.CloneInternal(this, value, cloneT);
            return value;
        }

        /// <summary>
        /// Copies all elements from source to destination, optionally using the specified method for cloning each
        /// element of type T from one instance to the other.
        /// </summary>
        /// <param name="source">The source of the clone operation.</param>
        /// <param name="destination">The destination of the clone operation.</param>
        /// <param name="cloneT">Encapsulates a method that will handle cloning for each element of type T.</param>
        protected void CloneInternal(RCValue<T> source, RCValue<T> destination, Func<T, T> cloneT)
        {
            GenericsHelper.Clone(source.Items, destination.Items, cloneT);
        }

        #endregion

        #region < Overridden from object >

        /// <summary>
        /// Returns the string representation of this <see cref="RCValue{T}"/> instance.
        /// </summary>
        /// <returns>The string representation of the <see cref="RCValue{T}"/> instance.</returns>
        public override string ToString()
        {
            return GenericsHelper.CalcString(this.Items, RegClasses.Names);
        }

        /// <summary>
        /// Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures
        /// like a hash table.
        /// </summary>
        /// <returns>A hash code for the current <see cref="RCValue{T}"/>.</returns>
        public override int GetHashCode()
        {
            return GenericsHelper.CalcHashCode(this.Items);
        }

        /// <summary>
        /// Determines whether the specified <see cref="object"/> is equal to the current instance.
        /// </summary>
        /// <param name="obj">The <see cref="object"/> to compare with the current instance.</param>
        /// <returns>
        /// true, if the specified <see cref="object"/> is equal to the current instance; false, otherwise.
        /// </returns>
        public override bool Equals(object obj)
        {
            return (obj is RCValue<T> value) ? this.Equals(value) : base.Equals(obj);
        }
        /// <summary>
        /// Determines whether the specified value is equal to the current instance.
        /// </summary>
        /// <param name="value">The value to compare with the current instance.</param>
        /// <returns>true, if the specified value is equal to the current instance; false, otherwise.</returns>
        public bool Equals(RCValue<T> value)
        {
            return Equals(this, value);
        }
        /// <summary>
        /// Determines whether the specified two values are equal.
        /// </summary>
        /// <param name="value1">The first value to compare.</param>
        /// <param name="value2">The second value to compare.</param>
        /// <returns>true, if the specified two values are equal; false, otherwise.</returns>
        public static bool Equals(RCValue<T> value1, RCValue<T> value2)
        {
            return GenericsHelper.Equals(value1?.Items, value2?.Items);
        }

        #endregion

        /// <summary>
        /// Resets all members of this instance to their default values (0, false, or null), or optionally using the
        /// specified clearT method to reset each element of type T to a specific initial value.
        /// </summary>
        /// <param name="clearT">
        ///     Encapsulates a method that will handle clearing for each element of type T within this instance.
        /// </param>
        public virtual void Clear(Action<T> clearT = null)
        {
            GenericsHelper.Clear(this.Items, clearT);
        }

        /// <summary>
        /// Returns the index corresponding to the specified <see cref="RegulatoryClass"/>, as defined within the
        /// context of this <see cref="RCValue{T}"/>.
        /// </summary>
        /// <returns>The index corresponding to the specified <see cref="RegulatoryClass"/>.</returns>
        public static int GetIndex(RegulatoryClass regClass)
        {
            return regClass.GetListIndex();
        }

        #endregion
    }
}
