using System;

namespace Volpe.Cafe.Generic
{
    /// <summary>
    /// Represents an object that stores modeling data for each regulatory class, with the domestic and imported cars
    /// combined into a single passenger car regulatory class.
    /// <br/>
    /// This object supports and operates on all regulatory classes as defined by <see cref="RegClasses.AltList"/>.
    /// </summary>
    [Serializable]
    public class RCAltValue<T> : ICloneable
    {
        #region <<< Constructors >>>

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

        /// <summary>
        /// Initializes a new instance of the <see cref="RCAltValue{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 RCAltValue(Func<T> initializeT) : this()
        {
            GenericsHelper.Initialize(this.Items, initializeT);
        }

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

        RCAltValue(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.AllPassengerCar"/>
        /// regulatory class.
        /// </summary>
        public T PassengerCar
        {
            get => this.Items[RegClasses.AltPassengerCarIndex];
            set => this.Items[RegClasses.AltPassengerCarIndex] = 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.AltLightTruckIndex];
            set => this.Items[RegClasses.AltLightTruckIndex] = 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.AltHdpuvIndex];
            set => this.Items[RegClasses.AltHdpuvIndex] = 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 ==(RCAltValue<T> value1, RCAltValue<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 !=(RCAltValue<T> value1, RCAltValue<T> value2)
        {
            return !Equals(value1, value2);
        }

        #endregion

        #region < ICloneable Members and Other Clone Methods >

        /// <summary>
        /// Creates a shallow copy of the current <see cref="RCAltValue{T}"/> instance.
        /// </summary>
        /// <returns>A new object that is a copy of this <see cref="RCAltValue{T}"/>.</returns>
        object ICloneable.Clone()
        {
            return this.Clone(null);
        }
        /// <summary>
        /// Creates a shallow copy of the current <see cref="RCAltValue{T}"/> instance.
        /// </summary>
        /// <returns>A new object that is a copy of this <see cref="RCAltValue{T}"/>.</returns>
        public RCAltValue<T> Clone()
        {
            return this.Clone(null);
        }
        /// <summary>
        /// Creates a copy of the current <see cref="RCAltValue{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="RCAltValue{T}"/>.</returns>
        public RCAltValue<T> Clone(Func<T, T> cloneT)
        {
            RCAltValue<T> value = new RCAltValue<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(RCAltValue<T> source, RCAltValue<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="RCAltValue{T}"/> instance.
        /// </summary>
        /// <returns>The string representation of the <see cref="RCAltValue{T}"/> instance.</returns>
        public override string ToString()
        {
            return GenericsHelper.CalcString(this.Items, RegClasses.AltNames);
        }

        /// <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="RCAltValue{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 RCAltValue<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(RCAltValue<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(RCAltValue<T> value1, RCAltValue<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="RCAltValue{T}"/>.
        /// </summary>
        /// <returns>The index corresponding to the specified <see cref="RegulatoryClass"/>.</returns>
        public static int GetIndex(RegulatoryClass regClass)
        {
            return regClass.GetAltListIndex();
        }

        #endregion
    }
}
