using ExtensiblePortfolioSite.SDK.Resources;

using System;
using System.Collections.Generic;

namespace ExtensiblePortfolioSite.SDK.Caching
{
    /// <summary>
    /// Global Item Cache
    /// </summary>
    public static class Cache
    {
        internal static TimeSpan GitUserLifetime = TimeSpan.FromMinutes(1440);
        internal static TimeSpan GitRepositoryLifetime = TimeSpan.FromMinutes(120);

        private static readonly SortedDictionary<String, GitServiceCache> CacheLookup = new();

        /// <summary>
        /// Retrives the specified <see cref="GitServiceCache"/> for the given provider
        /// </summary>
        /// <param name="ServiceProvider">Git Service Provider Name</param>
        /// <returns>Git Service Cache</returns>
        public static GitServiceCache GetGitServiceCache(String ServiceProvider)
        {
            if (CacheLookup.TryGetValue(ServiceProvider, out GitServiceCache? cache))
                return cache;
            lock (CacheLookup)
            {
                if (CacheLookup.TryGetValue(ServiceProvider, out cache))
                    return cache;

                ServiceProvider = string.Intern(ServiceProvider);

                cache = new GitServiceCache(ServiceProvider);
                CacheLookup.Add(ServiceProvider, cache);
                return cache;
            }
        }
    }
}
