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

namespace Volpe.Cafe.Reporting.Generators
{
    /// <summary>
    /// Defines an enumerable object used for generating rows of data for an engines report.
    /// </summary>
    [Serializable]
    public class EnginesReportDataGenerator : ComponentReportDataGeneratorBase<IEngine>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EnginesReportDataGenerator"/> class, for the specified
        /// scenario and reporting data.
        /// </summary>
        /// <inheritdoc cref="ReportDataGenerator(Scenario, ReportingData, ModelingSettings)" path="/param"/>
        public EnginesReportDataGenerator(Scenario scen, ReportingData data, ModelingSettings settings)
            : base(scen, data, settings) { }

        /// <inheritdoc/>
        protected override IReadOnlyList<IEngine> GetComponentList(IManufacturer mfr)
        {
            return mfr.Engines;
        }

        /// <inheritdoc/>
        protected override string GetComponentTypeString(IEngine component, List<IVehicleRevision> vehs)
        {
            var engTypes = vehs.ConvertAll(vehRevision => GetEngineTypeAndFuel(vehRevision))
                               .Distinct().ToArray();

            return (engTypes.Length == 1)
                ? engTypes[0].Substring(1, engTypes[0].Length - 2)
                : string.Join(", ", engTypes);

            string GetEngineTypeAndFuel(IVehicleRevision vehRevision)
            {
                return $"{{{vehRevision.GetVehicleEngineType(false)}-{vehRevision.GetVehicleEngineFuel(false)}}}";
            }
        }

        /// <inheritdoc/>
        protected override IOEMComponentRevision GetComponentRevision(IEngine component, IVehicle veh, int year)
        {
            return (component.Code == veh.GetRevision(year).EngineCode)
                ? veh.GetEngineRevision(year)
                : null;
        }

    }
}
