namespace Kit.Helpers.Auth
{
    using System.Collections.Generic;

    public interface ISecurityKeyRegisterService
    {
        ISecurityKeyRegisterService RegisterKey(string name, string value);
    }

    public interface ISecurityKeyService
    {
        bool CheckKey(string name, string value);
        string GetKey(string name);
    }

    public class SecurityKeyService : ISecurityKeyService, ISecurityKeyRegisterService
    {
        private readonly IDictionary<string, string> _securityKeys;

        public SecurityKeyService() 
        {
            _securityKeys = new Dictionary<string, string>();
        }

        public bool CheckKey(string name, string value)
        {
            string targetValue;
            if(_securityKeys.TryGetValue(name, out targetValue))
            {
                if (string.IsNullOrWhiteSpace(targetValue)) return true;
                return targetValue == value;
            }
            return false;
        }

        public string GetKey(string name)
        {
            string targetValue;
            return _securityKeys.TryGetValue(name, out targetValue) 
                   ? targetValue 
                   : string.Empty;
        }

        public ISecurityKeyRegisterService RegisterKey(string name, string value)
        {
            _securityKeys[name] = value;
            return this;
        }
    }
}