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

namespace ConstructorAppUI.ViewComponents.HomeLayoutComponents
{
    public class _HomeLayoutFooterPartialComponent : ViewComponent
    {
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly string _apiBaseUrl;

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

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

                // Sadece ilk öğeyi gönderecek şekilde güncelleyin
                return View(values.FirstOrDefault());
            }
            return View();
        }
    }
}
