using System;
using System.Collections.Generic;
using YachtDice.Modifiers.Definition;
using YachtDice.Modifiers.Runtime;

namespace YachtDice.Inventory
{
    public class InventoryModel
    {
        private readonly ModifierRegistry registry;

        public event Action OnInventoryChanged;
        public event Action<IReadOnlyList<ModifierInstance>> OnActiveModifiersChanged;

        public InventoryModel(ModifierRegistry registry)
        {
            this.registry = registry;

            registry.OnChanged += () => OnInventoryChanged?.Invoke();
            registry.OnActiveModifiersChanged += list => OnActiveModifiersChanged?.Invoke(list);
        }

        public IReadOnlyList<ModifierInstance> OwnedModifiers => registry.All;
        public int MaxActiveSlots => registry.MaxActiveSlots;
        public int ActiveCount => registry.ActiveCount;

        public void SetMaxActiveSlots(int slots) => registry.SetMaxActiveSlots(slots);

        public void AddModifier(ModifierDefinition definition) => registry.Add(definition);

        public void RemoveModifier(ModifierInstance instance) => registry.Remove(instance);

        public bool TryActivate(ModifierInstance instance) => registry.TryActivate(instance);

        public void Deactivate(ModifierInstance instance) => registry.Deactivate(instance);

        public void ConsumeUseOnActive() => registry.ConsumeChargesOnActive();

        public List<ModifierDefinition> GetActiveModifierDefinitions()
        {
            var result = new List<ModifierDefinition>();
            var active = registry.Active;
            for (int i = 0; i < active.Count; i++)
                result.Add(active[i].Definition);
            return result;
        }
    }
}
