using System.Collections.Generic;
using Volpe.Cafe.Generic;
using Volpe.Cafe.MarketData.Design;

namespace Volpe.Cafe.MarketData
{
    internal static class OEMQueryableHelper
    {
        //----- statistics about mix of vehicles -----
        /// <inheritdoc cref="IOEMQueryable.CalcRegClassMix(int?)"/>
        public static RCValue<bool> CalcRegClassMix<T>(List<T> vehicles, int? year = null) where T : IVehicle
        {
            var value = new RCValue<bool>();
            foreach (IVehicle veh in vehicles) {
                var regClass = GetRevisionOrBaselineInfo(veh, year).RegClass;
                if (regClass != RegulatoryClass.None) {
                    value[regClass] = true;
                }
            }
            return value;
        }

        /// <inheritdoc cref="IOEMQueryable.CalcRegClassMix(int?)"/>
        public static RCAltValue<bool> CalcAltRegClassMix<T>(List<T> vehicles, int? year = null) where T : IVehicle
        {
            var value = new RCAltValue<bool>();
            foreach (IVehicle veh in vehicles) {
                var regClass = GetRevisionOrBaselineInfo(veh, year).RegClass;
                if (regClass != RegulatoryClass.None) {
                    value[regClass] = true;
                }
            }
            return value;
        }

        /// <inheritdoc cref="IOEMQueryable.CalcRegClassMix(int?)"/>
        public static VCValue<bool> CalcVehClassMix<T>(List<T> vehicles, int? year = null) where T : IVehicle
        {
            var value = new VCValue<bool>();
            foreach (IVehicle veh in vehicles) {
                var vehClass = GetRevisionOrBaselineInfo(veh, year).VehicleClass;
                if (vehClass != VehicleClass.None) {
                    value[vehClass] = true;
                }
            }
            return value;
        }

        /// <inheritdoc cref="IOEMQueryable.CalcRegClassMix(int?)"/>
        public static FTValue<bool> CalcFuelTypeMix<T>(List<T> vehicles, int? year = null) where T : IVehicle
        {
            var value = new FTValue<bool>();
            foreach (IVehicle veh in vehicles) {
                var fuelType = GetRevisionOrBaselineInfo(veh, year).FuelType;
                foreach (FuelType ft in FuelTypes.List) {
                    if ((fuelType & ft) == ft) {
                        value[ft] = true;
                    }
                }
            }
            return value;
        }

        // calc-*-mix support method
        private static IVehicleInfo GetRevisionOrBaselineInfo(IVehicle veh, int? year)
        {
            return (year == null) ? (IVehicleInfo)veh.BaselineInfo : veh.GetRevision(year);
        }

    }
}
