using System;

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

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

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

        /// <summary>
        /// Initializes a new instance of the <see cref="VSValue{T}"/> class using the specified values.
        /// </summary>
        public VSValue(T cars, T vansSuvs, T pickups, T class2b3)
            : this(new T[] { cars, vansSuvs, pickups, class2b3 }) { }

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

        #endregion

        #region <<< Properties >>>

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

        /// <summary>
        /// Gets or sets the modeling data value associated with the <see cref="VehicleStyle.GenericCar"/> vehicle
        /// style.
        /// </summary>
        public T Cars
        {
            get => this.Items[VehicleStyles.GenericCarIndex];
            set => this.Items[VehicleStyles.GenericCarIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the <see cref="VehicleStyle.GenericVanSUV"/> vehicle
        /// style.
        /// </summary>
        public T VansSuvs
        {
            get => this.Items[VehicleStyles.GenericVanSuvIndex];
            set => this.Items[VehicleStyles.GenericVanSuvIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the <see cref="VehicleStyle.GenericPickup"/> vehicle
        /// style.
        /// </summary>
        public T Pickups
        {
            get => this.Items[VehicleStyles.GenericPickupIndex];
            set => this.Items[VehicleStyles.GenericPickupIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the <see cref="VehicleStyle.Generic2b3"/> vehicle
        /// style.
        /// </summary>
        public T Class2b3
        {
            get => this.Items[VehicleStyles.Generic2b3Index];
            set => this.Items[VehicleStyles.Generic2b3Index] = value;
        }

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

        #endregion

        #region < ICloneable Members and Other Clone Methods >

        /// <summary>
        /// Creates a shallow copy of the current <see cref="VSValue{T}"/> instance.
        /// </summary>
        /// <returns>A new object that is a copy of this <see cref="VSValue{T}"/>.</returns>
        object ICloneable.Clone()
        {
            return this.Clone(null);
        }
        /// <summary>
        /// Creates a shallow copy of the current <see cref="VSValue{T}"/> instance.
        /// </summary>
        /// <returns>A new object that is a copy of this <see cref="VSValue{T}"/>.</returns>
        public VSValue<T> Clone()
        {
            return this.Clone(null);
        }
        /// <summary>
        /// Creates a copy of the current <see cref="VSValue{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="VSValue{T}"/>.</returns>
        public VSValue<T> Clone(Func<T, T> cloneT)
        {
            VSValue<T> value = new VSValue<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(VSValue<T> source, VSValue<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="VSValue{T}"/> instance.
        /// </summary>
        /// <returns>The string representation of the <see cref="VSValue{T}"/> instance.</returns>
        public override string ToString()
        {
            return GenericsHelper.CalcString(this.Items, VehicleStyles.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="VSValue{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 VSValue<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(VSValue<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(VSValue<T> value1, VSValue<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="VehicleStyle"/>, as defined within the context
        /// of this <see cref="VSValue{T}"/>.
        /// </summary>
        /// <returns>The index corresponding to the specified <see cref="VehicleStyle"/>.</returns>
        public static int GetIndex(VehicleStyle vehStyle)
        {
            return vehStyle.GetAltListIndex();
        }

        #endregion
    }
}
