namespace MangaReader.Core.Http;

public class HttpService(HttpClient httpClient) : IHttpService
{
    public Task<string> GetStringAsync(string url, CancellationToken cancellationToken)
        => GetStringAsync(url, new Dictionary<string, string>(),  cancellationToken);

    public async Task<string> GetStringAsync(string url, IDictionary<string, string> headers, CancellationToken cancellationToken)
    {
        using HttpRequestMessage request = new(HttpMethod.Get, url);

        foreach (KeyValuePair<string, string> header in headers)
        {
            request.Headers.TryAddWithoutValidation(header.Key, header.Value);
        }

        //httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("MangaReader/1.0");
        httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0");

        using HttpResponseMessage response = await httpClient.SendAsync(request, cancellationToken);
        response.EnsureSuccessStatusCode();

        return await response.Content.ReadAsStringAsync(cancellationToken);
    }
}