using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApiKnockoutDemo.Models
{
    public class Outcome
    {
        public Person Competitor { get; set; }
        public decimal AverageRating
        {
            get
            {
                try
                {
                    return Competitor.RatingsReceived.Average(rr => rr.Rating1);
                }
                catch
                {
                    return 0; // will throw if no elements to average
                }

            }
        }

        public bool HasRatings
        {
            get
            {
                return Competitor.RatingsReceived.Any();
            }
        }

        public string Star { get; set; }

        /// <summary>
        /// Given a collection of outcomes
        /// </summary>
        /// <param name="outcomes"></param>
        public void CalculateStar(decimal goldStar, decimal silverStar)
        {
            if (AverageRating == goldStar)
                Star = "Gold";
            else if (AverageRating == silverStar)
                Star = "Silver";
            else if (AverageRating >= silverStar)
                throw new ArgumentException("Silver and Gold must be top two scores");
            else
                Star = "";
            

        }
    }
}