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

namespace Volpe.Cafe.DfsSrModels
{
    /// <summary>
    /// Represents a forecast vehicle used to store sales projections for each future model year evaluated.
    /// </summary>
    [Serializable]
    public class ForecastVehicle
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ForecastVehicle" /> class, using information obtained from the
        /// specified vehicle and seeding the forecast vehicle data for the given range of model years.
        /// </summary>
        /// <param name="veh">The underlying vehicle to use for initializing the forecast vehicle.</param>
        /// <exception cref="InvalidOperationException">The vehicle does not have any valid revisions.</exception>
        public ForecastVehicle(IVehicle veh)
        {
            if (!veh.HasRevisions) {
                throw new InvalidOperationException(
                    "This vehicle cannot be used for forecasting since it does not have any valid revisions.");
            }

            this.Vehicle = veh;
            this.ForecastData = veh.Revisions.ConvertAll(item => new ForecastVehicleData(item), 0);
        }

        /// <summary>
        /// Gets the underlying vehicle associated with this forecast vehicle.
        /// </summary>
        public IVehicle Vehicle { get; }

        /// <summary>Gets the unique runtime-assigned index for the manufacturer.</summary>
        public int MfrIndex => this.Vehicle.Manufacturer.Index;

        /// <summary>Gets the unique code assigned to the vehicle.</summary>
        public int Code => this.Vehicle.Code;

        /// <summary>
        /// Gets the forecast vehicle data for all model years.
        /// </summary>
        public IAnnualValue<ForecastVehicleData> ForecastData { get; }

        /// <summary>
        /// Retrieves the forecast vehicle sales for the specified model year.
        /// </summary>
        public double GetForecastSales(int year)
        {
            var value = this.ForecastData[year];
            return (value == null) ? 0 : value.Sales;
        }

    }
}
