using MangaReader.Core.HttpService;
using MangaReader.Core.Sources.NatoManga.Api;
using MangaReader.Tests.Utilities;
using NSubstitute;
using Shouldly;

namespace MangaReader.Tests.Sources.NatoManga.Api;

public class NatoMangaClientTests
{
    class TestNatoMangaClient(IHttpService httpService) : NatoMangaClient(httpService)
    {
        internal string Test_GetSearchUrl(string keyword) => GetSearchUrl(keyword);
    }

    [Fact]
    public void Get_Search_Url()
    {
        IHttpService httpService = Substitute.For<IHttpService>();
        TestNatoMangaClient searchProvider = new(httpService);

        string url = searchProvider.Test_GetSearchUrl("Gal can't be kind");

        url.ShouldBe("https://www.natomanga.com/home/search/json?searchword=gal_can_t_be_kind");
    }

    [Fact]
    public async Task Search_Manga()
    {
        string searchResultJson = await ReadJsonResourceAsync("Manga-Search-Response.json");

        IHttpService httpService = Substitute.For<IHttpService>();

        httpService.GetStringAsync(Arg.Any<string>(), CancellationToken.None)
            .Returns(Task.FromResult(searchResultJson));

        NatoMangaClient natoMangaClient = new(httpService);
        NatoMangaSearchResult[] searchResults = await natoMangaClient.SearchAsync("Gal Can't Be Kind", CancellationToken.None);

        searchResults.Length.ShouldBe(2);

        searchResults[0].Name.ShouldBe("Gal Can't Be Kind to Otaku!");
        searchResults[0].Url.ShouldBe("https://www.natomanga.com/manga/gal-can-t-be-kind-to-otaku");
        searchResults[0].Thumb.ShouldBe("https://img-r1.2xstorage.com/thumb/gal-can-t-be-kind-to-otaku.webp");

        searchResults[1].Name.ShouldBe("Gal Can’t Be Kind to Otaku!?");
        searchResults[1].Url.ShouldBe("https://www.natomanga.com/manga/gal-cant-be-kind-to-otaku");
        searchResults[1].Thumb.ShouldBe("https://img-r1.2xstorage.com/thumb/gal-cant-be-kind-to-otaku.webp");
    }

    private static async Task<string> ReadJsonResourceAsync(string resourceName)
    {
        return await ResourceHelper.ReadJsonResourceAsync($"MangaReader.Tests.Sources.NatoManga.Api.{resourceName}");
    }
}