using System;

namespace Volpe.Cafe.TechAnalysis
{
    /// <summary>
    /// Defines a range of model years (as minimum and maximum bounds) during which a candidate technology was selected
    /// for application on one or more vehicles.
    /// </summary>
    [Serializable]
    public struct WindowOfApplication
    {
        /// <summary>
        /// Initializes a new <see cref="WindowOfApplication"/> instance, using the specified min and max years.
        /// </summary>
        public WindowOfApplication(int minYear, int maxYear)
        {
            this.MinYear = minYear;
            this.MaxYear = maxYear;
        }

        /// <summary>
        /// Represents an empty <see cref="WindowOfApplication"/> instance. This value is read-only.
        /// </summary>
        public static readonly WindowOfApplication Empty = new WindowOfApplication(0, 0);

        /// <summary>
        /// Gets the minimum year when a candidate technology was selected for application on one or more affected
        /// vehicles.
        /// </summary>
        public int MinYear { get; private set; }
        /// <summary>
        /// Gets the maximum year when a candidate technology was selected for application on one or more affected
        /// vehicles.
        /// </summary>
        public int MaxYear { get; private set; }

        /// <summary>
        /// Gets whether this <see cref="WindowOfApplication"/> instance represents a potential multiyear solution.
        /// </summary>
        public bool IsMultiYear => (this.MinYear != this.MaxYear);

        /// <summary>
        /// Gets whether this <see cref="WindowOfApplication"/> instance has valid min and max years defined.
        /// </summary>
        public bool IsValid => (this.MinYear > 0 && this.MaxYear > 0);

        //----- support for tuple-conversion -----        
        /// <summary>
        /// Performs an implicit conversion from a tuple of MinYear, MaxYear integers to an associated
        /// <see cref="WindowOfApplication"/> object.
        /// </summary>
        public static implicit operator WindowOfApplication((int MinYear, int MaxYear) value)
        {
            return new WindowOfApplication(value.MinYear, value.MaxYear);
        }

        /// <summary>
        /// De-constructs the current value into a tuple of its components.
        /// </summary>
        public void Deconstruct(out int minYear, out int maxYear)
        {
            minYear = this.MinYear;
            maxYear = this.MaxYear;
        }

    }
}
