using System;
using System.Collections.Generic;

namespace Jovian.EncounterSystem {
    /// <summary>Dispatches <see cref="IEncounterEvent"/> instances to per-type handlers. Unknown types are skipped.</summary>
    public class EncounterResolver {
        private readonly Dictionary<Type, Action<IEncounterEvent, EncounterContext>> handlers = new();

        public void Register<T>(Action<T, EncounterContext> handler) where T : IEncounterEvent {
            // Wrap the typed delegate so the dictionary can hold handlers for any event type uniformly.
            // Cast is safe because the wrapper is only invoked via lookup under typeof(T).
            handlers[typeof(T)] = (evt, ctx) => handler((T)evt, ctx);
        }

        public void Unregister<T>() where T : IEncounterEvent {
            handlers.Remove(typeof(T));
        }

        /// <summary>Indexed iteration over <paramref name="events"/> — avoids the boxed enumerator
        /// that an <c>IEnumerable&lt;T&gt;</c> parameter would force. <see cref="EncounterDialogOption.events"/>
        /// is a <c>List</c>, which implements <c>IReadOnlyList</c>, so call sites just pass it directly.</summary>
        public void Resolve(IReadOnlyList<IEncounterEvent> events, EncounterContext context) {
            if(events == null) {
                return;
            }

            var count = events.Count;
            for(int i = 0; i < count; i++) {
                var evt = events[i];
                if(evt == null) {
                    continue;
                }

                if(handlers.TryGetValue(evt.GetType(), out var handler)) {
                    handler(evt, context);
                }
            }
        }
    }
}
