﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E2Print.Domain.Entities {
    public class Cart {
        private List<CartLine> lineCollection = new List<CartLine>();

        public void AddItem(Product product, int quantity, decimal price) {
            CartLine line = lineCollection
                .Where(c => c.Product.Id == product.Id)
                .FirstOrDefault();

            if (line == null) {
                lineCollection.Add(new CartLine { Product = product, Quantity = quantity,Price = price});
            } else {
                line.Quantity += quantity;
            }
        }

        public  void RemoveLine(Product product) {
            lineCollection.RemoveAll(c => c.Product.Id == product.Id);
        }

        public decimal ComputeTotalValue() {
            return lineCollection.Sum(c => c.Product.Price * c.Quantity *0.8m);
        }

        public void Clear() {
            lineCollection.Clear();
        }

        public IEnumerable<CartLine> Lines {
            get { return lineCollection; }
        }
    }

    public class CartLine {
        public Product Product { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }
    }
}
