using System;

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

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

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

        /// <summary>
        /// Initializes a new instance of the <see cref="VCValue{T}"/> class using the specified values.
        /// </summary>
        public VCValue(T ldv, T ldt12a, T ldt2b3) : this(new T[] { ldv, ldt12a, ldt2b3 }) { }

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

        #endregion

        #region <<< Properties >>>

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

        /// <summary>
        /// Gets or sets the modeling data value associated wit the <see cref="VehicleClass.LDV"/> vehicle class.
        /// </summary>
        public T Ldv
        {
            get => this.Items[VehicleClasses.LdvIndex];
            set => this.Items[VehicleClasses.LdvIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated wit the <see cref="VehicleClass.LDT12a"/> vehicle class.
        /// </summary>
        public T Ldt12a
        {
            get => this.Items[VehicleClasses.Ldt12aIndex];
            set => this.Items[VehicleClasses.Ldt12aIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated wit the <see cref="VehicleClass.LDT2b3"/> vehicle class.
        /// </summary>
        public T Ldt2b3
        {
            get => this.Items[VehicleClasses.Ldt2b3Index];
            set => this.Items[VehicleClasses.Ldt2b3Index] = value;
        }

        /// <summary>Gets an array of modeling data specified by <see cref="VehicleClass"/>.</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 ==(VCValue<T> value1, VCValue<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 !=(VCValue<T> value1, VCValue<T> value2)
        {
            return !Equals(value1, value2);
        }

        #endregion

        #region < ICloneable Members and Other Clone Methods >

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

        #endregion

        #region < Overriden from object >

        /// <summary>
        /// Returns the string representation of this <see cref="VCValue{T}"/> instance.
        /// </summary>
        /// <returns>The string representation of the <see cref="VCValue{T}"/> instance.</returns>
        public override string ToString()
        {
            return GenericsHelper.CalcString(this.Items, VehicleClasses.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="VCValue{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 VCValue<T>) ? this.Equals((VCValue<T>)obj) : 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(VCValue<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(VCValue<T> value1, VCValue<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="VehicleClass"/>, as defined within the context
        /// of this <see cref="VCValue{T}"/>.
        /// </summary>
        /// <returns>The index corresponding to the specified <see cref="VehicleClass"/>.</returns>
        public static int GetIndex(VehicleClass vehClass)
        {
            return vehClass.GetAltListIndex();
        }
        
        #endregion
    }
}
