using Blazored.LocalStorage;
using drinkMe.Client.Services.Interfaces;
using drinkMe.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace drinkMe.Client.Services
{
	public class PriceService : IPriceService
	{
		public float GetTotalPrice(List<DiscountCodeViewModel> discountCodes, List<DrinkViewModel> cartDrinks)
		{
			var total = default(float);

			if (discountCodes.Count == 0)
				foreach (var cartDrink in cartDrinks)
					total += cartDrink.Price * cartDrink.Quantity;
			else
				foreach (var cartDrink in cartDrinks)
					if (discountCodes.Any(d => d.ApplicableProducts.Contains(cartDrink.Id)))
					{
						var discountPercentage = discountCodes.First(d => d.ApplicableProducts.Contains(cartDrink.Id)).DiscountPercentage / 100;
						var discountedPricePortion = cartDrink.Price * discountPercentage;
						total += (cartDrink.Price - discountedPricePortion) * cartDrink.Quantity;
					}
					else
						total += cartDrink.Price * cartDrink.Quantity;

			return total;
		}
	}
}
