using System;

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

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

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

        /// <summary>
        /// Initializes a new instance of the <see cref="SCValue{T}"/> class using the specified values.
        /// </summary>
        public SCValue(T pc, T lt, T cm) : this(new T[] { pc, lt, cm }) { }

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

        #endregion

        #region <<< Properties >>>

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

        /// <summary>
        /// Gets or sets the modeling data value associated with the  <see cref="SafetyClass.PC"/> safety class.
        /// </summary>
        public T PC
        {
            get => this.Items[SafetyClasses.PcIndex];
            set => this.Items[SafetyClasses.PcIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the  <see cref="SafetyClass.LT"/> safety class.
        /// </summary>
        public T LT
        {
            get => this.Items[SafetyClasses.LtIndex];
            set => this.Items[SafetyClasses.LtIndex] = value;
        }

        /// <summary>
        /// Gets or sets the modeling data value associated with the  <see cref="SafetyClass.CM"/> safety class.
        /// </summary>
        public T CM
        {
            get => this.Items[SafetyClasses.CmIndex];
            set => this.Items[SafetyClasses.CmIndex] = value;
        }

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

        #endregion

        #region < ICloneable Members and Other Clone Methods >

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

        #endregion
    }
}
