
using System.Net.Http.Json;
using RBLFeederCommon.Models.RssFeed;

namespace RBLNews.Shared.Services
{
    public interface IFeedDataService
    {
        public RssVM Feeds { get; }
        
        public Action DataChanged { get; set; }

        Task LoadFeeds();
    }

    public class FeedDataService : IFeedDataService
    {
        private readonly EnvironmentVariablesService _environmentVariablesService;

        private static readonly HttpClient httpClient = new();

        public RssVM Feeds { get; private set; }
        
        public Action DataChanged { get; set; }


        public FeedDataService(EnvironmentVariablesService environmentVariablesService)
        {
            this._environmentVariablesService = environmentVariablesService;
        }
        
        public async Task LoadFeeds()
        {
            Feeds = await httpClient.GetFromJsonAsync<RssVM>($"{this._environmentVariablesService.RblApiUrl}/api/feeds") ?? new RssVM();
            DataChanged.Invoke();
        }
    }
}
