﻿using System.Collections.Generic;
using System.Linq;
using HAL.Interfaces;

namespace HAL.Core
{
    public class Bot
    {
        private readonly List<HalPluginWrapper> _commandProcessors;
        public const string NoResponseText = "I'm sorry Dave, I'm afraid I can't do that.";

        public Bot()
        {
            _commandProcessors = new List<HalPluginWrapper>();
        }

        public IEnumerable<string> ProcessCommand(string command)
        {
            var matchedProcessors =
                _commandProcessors.Where(x => x.IsEnabled)
                    .SelectMany(x => x.Plugin.SupportedCommands)
                    .Where(x => x.Key.IsMatch(command)).ToList();

            if (!matchedProcessors.Any())
                return new List<string> {NoResponseText};

            var responses = new List<string>();

            foreach (var processor in matchedProcessors)
                responses.Add(processor.Value(command, processor.Key.Match(command)));

            return responses;
        }

        public void AddPlugin(IHalPlugin halPlugin, bool enabled)
        {
            _commandProcessors.Add(new HalPluginWrapper(halPlugin, enabled));
        }

        public IEnumerable<HalPluginWrapper> GetPlugins()
        {
            return _commandProcessors;
        }
    }
}
