using Microsoft.VisualBasic.
CompilerServices;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Net.NetworkInformation;    //JEN: Removed
//using System.Reflection.Metadata.Ecma335; //JEN: Removed
using static ICT711_Day5_classes.ProductGarment; //JEN: Added
namespace ICT711_Day5_classes
{
  public class Inventory : IInventory
  {
    private List<Product> products;
    public List<Product> Products
    {
      get
      {
         if (products == null) products = new List<Product>();
         return products;
      }
        set { products = value; }
    }
 //JEN: Revised
    public List<IProduct> this[string search]
    {
      get
      {
         List<IProduct> productsOut = new List<IProduct>();
            foreach (var p in this.Products)
               if (p.ProductName.ToLower().Contains(search.ToLower())) productsOut.Add(p);
            return productsOut;
        }
    }
    public IProduct this[int productId]
    {
      get
      {
         foreach (var p in this.Products)
         {
            if (p.ProductId == productId) return p;
         }
            throw new Exception("Invalid productId");
        }
    }
     /*public Inventory()
     {
       products = new List<Product>();
     }
     public List<IProduct> this[string search]
{
    get
    {
      return products.Where(p => p.ProductName.Contains(search)).ToList<IProduct>();
    }
}
public IProduct this[int productID]
{
  get
  {
     return products.FirstOrDefault(p => p.ProductId == productID);
  }
}
public List<IProduct> Products //=> Products.Cast<IProduct>().ToList();
{
  get { return products.Cast<IProduct>().ToList(); }
}
List<Product> IInventory.Products => throw new NotImplementedException(); */
public bool AddProduct(Product product)
{
  foreach (var p in Products)
  {
     if (p.ProductId == product.ProductId)
     {
        p.Add(product);
        return true;
     };
  }
    Products.Add(product);
    return true;
}
/*public bool AddProduct(Product product)
{
  //throw new NotImplementedException();
  Product existingProduct = products.FirstOrDefault(p => p.ProductId == product.ProductId);
  if (existingProduct != null)
  {
     existingProduct.Quantity += product.Quantity;
     return false;
  }
  else
  {
     products.Add(product);
     return true;
  }
}*/
public bool RemoveProduct(Product product)
{
  foreach (var p in this.Products)
  {
     if (p.ProductId == product.ProductId)
     {
                 if (p.Quantity >= product.Quantity)
                 {
                    p.Remove(product);
                    return true;
                 }
                 else throw new ArgumentOutOfRangeException("Quantity", "There is not enough quantity.");
            };
        }
        throw new Exception("Product is not in inventory.");
    }
    /*public bool RemoveProduct(Product product)
    {
      // throw new NotImplementedException();
    // FIRST CODE
    Product existingProduct = products.FirstOrDefault(p => p.ProductId == product.ProductId);
    if (existingProduct == null)
    {
       throw new InvalidOperationException("Product is not in inventory.");
    }
    if (existingProduct is ProductGarment existingGarment && product is ProductGarment removeGarment)
    {
       foreach (var entry in removeGarment.SizePriceQuantity)
       {
          string size = entry.Key;
          int quantity = entry.Value.quantity;
          decimal price = entry.Value.price;
            if (!existingGarment.SizePriceQuantity.ContainsKey(size) ||
               existingGarment.SizePriceQuantity[size].quantity < quantity)
            {
               throw new ArgumentOutOfRangeException($"There is not enough quantity of size {size}");
            }
            existingGarment.AddQuantity(size, -quantity, price);
        }
    }
    else
    {
       if (existingProduct.Quantity < product.Quantity)
       {
          throw new ArgumentOutOfRangeException((existingProduct.Quantity.ToString()), $"There is not enough
quantity.");
       }
       if (existingProduct.Quantity == product.Quantity)
       {
          products.Remove(existingProduct);
       }
       else
       {
          existingProduct.Quantity -= product.Quantity;
       }
    }
        return true;
        }*/
        public static Inventory operator +(Inventory inventory, Product product)
        {
            inventory.AddProduct(product);
            return inventory;
        }
        public static Inventory operator -(Inventory inventory, Product product)
        {
            inventory.RemoveProduct(product);
            return inventory;
        }
    }
}