using System;
using System.Reflection;
using System.Collections.Generic;

namespace FilterAPI.Handlers
{
    public class HandlerLoader
    {
        public Dictionary<Byte, Dictionary<Byte, MethodInfo>> Handlers;

        public HandlerLoader() { Handlers = new Dictionary<Byte, Dictionary<Byte, MethodInfo>>(); }

        public Boolean HasHandler(Byte Header, Byte Type)
        {
            if (Handlers.ContainsKey(Header))
            {
                if (Handlers[Header].ContainsKey(Type)) { return true; }
            }

            return false;
        }

        public MethodInfo GetHandler(Byte Header, Byte Type)
        {
            Dictionary<Byte, MethodInfo> HeaderDictionary;

            MethodInfo TypeMethod;

            if (Handlers.TryGetValue(Header, out HeaderDictionary))
            {
                if (HeaderDictionary.TryGetValue(Type, out TypeMethod)) { return TypeMethod; }
            }

            return null;
        }

        public Action GetAction(MethodInfo TypeMethod, params Object[] Args) { return () => TypeMethod.Invoke(null, Args); }
    }
}
