using System;

namespace Volpe.Cafe.Generic
{
    /// <summary>
    /// Represents an object that stores modeling data for each supported pollutant for which the CAFE Model estimates
    /// monetized emission damages.
    /// <br/>
    /// This object supports and operates on all pollutants as defined by <see cref="Pollutants.MonetizedList"/>.
    /// </summary>
    [Serializable]
    public class MonetizedPollutantValue<T> : ICloneable
    {
        #region <<< Constructors >>>

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

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

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

        #endregion

        #region <<< Properties >>>

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

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

        /// <summary>Gets the modeling data associated with the Carbon Monoxide pollutant.</summary>
        public T CO => this[Pollutant.CO];
        /// <summary>Gets the modeling data associated with the Volatile Organic Compound pollutant.</summary>
        public T VOC => this[Pollutant.VOC];
        /// <summary>Gets the modeling data associated with the Nitrogen Oxides pollutant.</summary>
        public T NOx => this[Pollutant.NOx];
        /// <summary>Gets the modeling data associated with the Sulfur Dioxide pollutant.</summary>
        public T SO2 => this[Pollutant.SO2];
        /// <summary>
        /// Gets the modeling data associated with the Particulate Matter (diameter of ~2.5 micrometers) pollutant.
        /// </summary>
        public T PM => this[Pollutant.PM];
        /// <summary>Gets the modeling data associated with the Carbon Dioxide pollutant.</summary>
        public T CO2 => this[Pollutant.CO2];
        /// <summary>Gets the modeling data associated with the Methane pollutant.</summary>
        public T CH4 => this[Pollutant.CH4];
        /// <summary>Gets the modeling data associated with the Nitrous Oxide pollutant.</summary>
        public T N2O => this[Pollutant.N2O];

        #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 ==(MonetizedPollutantValue<T> value1, MonetizedPollutantValue<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 !=(MonetizedPollutantValue<T> value1, MonetizedPollutantValue<T> value2)
        {
            return !Equals(value1, value2);
        }

        #endregion

        #region < ICloneable Members and Other Clone Methods >

        /// <summary>
        /// Creates a copy of the current <see cref="MonetizedPollutantValue{T}"/> instance, optionally using the
        /// specified method for cloning each element of type <typeparamref name="T"/> within this instance.
        /// </summary>
        /// <param name="cloneT">
        ///     Encapsulates a method that will handle cloning for each element of type <typeparamref name="T"/> within
        ///     this instance.
        /// </param>
        /// <returns>A new object that is a copy of this <see cref="PollutantValue{T}"/>.</returns>
        public MonetizedPollutantValue<T> Clone(Func<T, T> cloneT = null)
        {
            MonetizedPollutantValue<T> value = new MonetizedPollutantValue<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 <typeparamref name="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(MonetizedPollutantValue<T> source,
                                     MonetizedPollutantValue<T> destination,
                                     Func<T, T> cloneT)
        {
            GenericsHelper.Clone(source.Items, destination.Items, cloneT);
        }

        object ICloneable.Clone()
        {
            return this.Clone(null);
        }

        #endregion

        #region < Overridden from object >

        /// <summary>
        /// Returns the string representation of this <see cref="MonetizedPollutantValue{T}"/> instance.
        /// </summary>
        /// <returns>The string representation of the <see cref="MonetizedPollutantValue{T}"/> instance.</returns>
        public override string ToString()
        {
            return GenericsHelper.CalcString(this.Items, Pollutants.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="MonetizedPollutantValue{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 MonetizedPollutantValue<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(MonetizedPollutantValue<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(MonetizedPollutantValue<T> value1, MonetizedPollutantValue<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 <typeparamref name="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="Pollutant"/>, as defined within the context of
        /// this <see cref="MonetizedPollutantValue{T}"/>.
        /// </summary>
        /// <returns>The index corresponding to the specified <see cref="Pollutant"/>.</returns>
        public static int GetIndex(Pollutant pollutant)
        {
            return (int)pollutant;
        }

        #endregion
    }
}
