using System;
using System.Linq;
using Volpe.Cafe.Generic;

namespace Volpe.Cafe.EffectsData.Collections
{
    /// <summary>
    /// Provides a collection of modeling effects for energy consumption from vehicle operation, separated by
    /// regulatory class and fuel type.
    /// </summary>
    /// <inheritdoc cref="ModelingEffectsCollection{T}" path="/remarks"/>
    [Serializable]
    public sealed class EnergyEffectsCollection : ModelingEffectsCollection<EnergyEffects>
    {
        /// <summary>
        /// Returns the average fuel share for the specified regulatory class and fuel type.
        /// </summary>
        public double GetFuelShare(RegulatoryClass regClass, FuelType fuelType)
        {
            if (fuelType == FuelType.All) {
                return 1;
            }

            double vmtFuel = 0;
            double vmtTotal = 0;

            switch (regClass) {
                case RegulatoryClass.All:
                    SumVMTs(this._values.Items);
                    break;

                case RegulatoryClass.AllLightDuty:
                    SumVMTs(this._values.PassengerCar, this._values.LightTruck);
                    break;

                default:
                    SumVMTs(this._values[regClass]);
                    break;
            }

            return vmtFuel / vmtTotal;

            // local helper function
            void SumVMTs(params FTValue<EnergyEffects>[] values)
            {
                foreach (var value in values) {
                    vmtFuel += value[fuelType].VMT;
                    vmtTotal += value.Items.Sum(x => x.VMT);
                }
            }
        }

    }
}
