using System;
using System.Collections.Generic;


namespace ActionGame.GameArtifacts.Models.Items
{
    public class Inventory
    {
        public List<InventorySlot> InventorySlots { get; private set; }
        public InventorySlot Slot1 { get; set; }
        public InventorySlot Slot2 { get; set; }


        public Inventory()
        {
            this.InventorySlots = new List<InventorySlot>();
        }


        public Boolean AddItem(Item item, Int32? quantity)
        {
            if (ContainsItemAlready(item))
            {
                if ((this.Slot1 != null) && (this.Slot1.Item.Key == item.Key)) return AddQuantityToSlot(this.Slot1, quantity);
                if ((this.Slot2 != null) && (this.Slot2.Item.Key == item.Key)) return AddQuantityToSlot(this.Slot2, quantity);

                foreach (InventorySlot slot in InventorySlots)
                {
                    if (slot.Item.Key == item.Key)
                    {
                        return AddQuantityToSlot(slot, quantity);
                    }
                }

                return false;   //catch-all
            }
            else
            {
                if (Slot1 == null)
                {
                    this.Slot1 = new InventorySlot(item, quantity);

                    if((quantity != null) && (this.Slot1.Quantity > this.Slot1.Item.MaxCapacity))
                    {
                        this.Slot1.Quantity = this.Slot1.Item.MaxCapacity;
                    }
                }
                else if (Slot2 == null)
                {
                    this.Slot2 = new InventorySlot(item, quantity);

                    if ((quantity != null) && (this.Slot2.Quantity > this.Slot2.Item.MaxCapacity))
                    {
                        this.Slot2.Quantity = this.Slot2.Item.MaxCapacity;
                    }
                }
                else
                {
                    InventorySlot slot = new InventorySlot(item, quantity);

                    InventorySlots.Add(slot);

                    if ((quantity != null) && (slot.Quantity > slot.Item.MaxCapacity))
                    {
                        slot.Quantity = slot.Item.MaxCapacity;
                    }
                }

                return true;
            }
        }


        private Boolean AddQuantityToSlot(InventorySlot slot, Int32? quantity)
        {
            if (quantity == null) return true;
            if (slot.Quantity == null) return true;

            if (slot.Quantity == slot.Item.MaxCapacity)
            {
                return false;
            }
            else
            {
                slot.Quantity += quantity;

                if (slot.Quantity > slot.Item.MaxCapacity)
                {
                    slot.Quantity = slot.Item.MaxCapacity;
                }

                return true;
            }

        }


        private Boolean ContainsItemAlready(Item item)
        {
            if ((this.Slot1 != null) && (this.Slot1.Item.Key == item.Key)) return true;
            if ((this.Slot2 != null) && (this.Slot2.Item.Key == item.Key)) return true;

            foreach (InventorySlot slot in InventorySlots)
            {
                if (slot.Item.Key == item.Key) return true;
            }

            return false;
        }


        public void FillAllItems()
        {
            FillSlot(this.Slot1);
            FillSlot(this.Slot2);

            foreach(InventorySlot slot in this.InventorySlots)
            {
                FillSlot(slot);
            }
        }

        
        private void FillSlot(InventorySlot slot)
        {
            if (slot == null) return;
            if (slot.Item == null) return;
            if (slot.Quantity == null) return;

            slot.Quantity = slot.Item.MaxCapacity;
        }

    }
}
