﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace GoblinPlugin
{
	public class MessageHub
	{
		private Dictionary<string, IGamePlugin> Receivers;
		private Dictionary<string, Action> Builtins;

		public MessageHub()
		{
			Receivers = new Dictionary<string, IGamePlugin>();
			Builtins = new Dictionary<string, Action>();
		}

		public void AddBuiltin(string message, Action action)
		{
			Builtins.Add(message, action);
		}

		public void Subscribe(string message, IGamePlugin plugin)
		{
			Receivers.Add(message, plugin);
		}

		public void Send(string message)
		{
			foreach (string subscription in Receivers.Keys)
			{
				if (Regex.Match(message, subscription).Success) Receivers[subscription].Message(message);
			}
			foreach (string builtin in Builtins.Keys)
			{
				if (Regex.Match(message, builtin).Success) Builtins[builtin]();
			}
		}
	}
}

