using ConstructorAppUI.Dtos.ProjectDtos;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace ConstructorAppUI.ViewComponents.HomeComponents
{
    public class _HomeProjectPartialComponent : ViewComponent
    {
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly string? _apiBaseUrl;

        public _HomeProjectPartialComponent(IHttpClientFactory httpClientFactory, IConfiguration configuration)
        {
            _httpClientFactory = httpClientFactory;
            _apiBaseUrl = configuration["ApiSettings:BaseUrl"];
        }

        public async Task<IViewComponentResult> InvokeAsync()
        {
            var client = _httpClientFactory.CreateClient();
            var responseMessage = await client.GetAsync($"{_apiBaseUrl}/api/Project");
            var jsonData = await responseMessage.Content.ReadAsStringAsync();
            var values = JsonConvert.DeserializeObject<List<ResultProjectDto>>(jsonData);
            return View(values);
        }
    }
}
