using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SharpRss.Models;

namespace SharpRss.Core.Cache
{
    internal enum CacheFetch
    {
        Category,
        Syndication
    }

    internal static class TempCache
    {
        public static Dictionary<string, CategoryModel> CategoryCache { get; private set; } = new Dictionary<string, CategoryModel>();
        public static Dictionary<string, SyndicationModel> SyndicationCache { get; private set; } = new Dictionary<string, SyndicationModel>();

        public static async Task UpdateCache(CacheFetch cf)
        {
            switch (cf)
            {
                case CacheFetch.Category:
                    var cats = await DbAccess.GetCategoriesAsync();
                    CategoryCache = cats.ToDictionary(x => x.Id);
                    break;
                case CacheFetch.Syndication:
                    var syndications = await DbAccess.GetSyndicationsAsync();
                    SyndicationCache = syndications.ToDictionary(x => x.EncodedUrl);
                    break;
            }
        }

        public static CategoryModel? GetCategory(string catId) => CategoryCache.TryGetValue(catId, out CategoryModel catModel) ? catModel : null;
        public static SyndicationModel? GetSyndication(string encodedUrl) => SyndicationCache.TryGetValue(encodedUrl, out SyndicationModel synModel) ? synModel : null;
    }
}