using MangaReader.Core.Metadata;
using MangaReader.Core.Sources.MangaDex.Api;
using MangaReader.Core.Sources.MangaDex.Metadata;
using NSubstitute;
using Shouldly;

namespace MangaReader.Tests.Sources.MangaDex.Metadata;

public class MangaDexMetadataTests
{
    [Fact]
    public async Task Get_Manga()
    {
        MangaDexEntityResponse mangaEntityResponse = new()
        {
            Result = "ok",
            Response = "entity",
            Data = new MangaEntity()
            {
                Id = new Guid("ee96e2b7-9af2-4864-9656-649f4d3b6fec"),
                Type = "manga",
                Attributes = new()
                {
                    Title =
                    {
                        { "en", "Gals Can’t Be Kind to Otaku!?" }
                    },
                    AltTitles =
                    [
                        new()
                        {
                            { "ja", "オタクに優しいギャルはいない！？" }
                        },
                        new()
                        {
                            { "ja-ro", "Otaku ni Yasashii Gal wa Inai!?" }
                        },
                        new()
                        {
                            { "ja-ro", "Otaku ni Yasashii Gyaru ha Inai!?" }
                        },
                        new()
                        {
                            { "en", "Gal Can't Be Kind to Otaku!?" }
                        },
                        new()
                        {
                            { "en", "Gals Can't Be Kind To A Geek!?" }
                        }
                    ],
                    Tags =
                    [
                        new()
                        {
                            Id = new Guid("423e2eae-a7a2-4a8b-ac03-a8351462d71d"),
                            Type = "tag",
                            Attributes = new()
                            {
                                Group = "genre",
                                Name =
                                {
                                    { "en", "Romance" }
                                }
                            }
                        },
                        new()
                        {
                            Id = new Guid("4d32cc48-9f00-4cca-9b5a-a839f0764984"),
                            Type = "tag",
                            Attributes = new()
                            {
                                Group = "genre",
                                Name =
                                {
                                    { "en", "Comedy" }
                                }
                            }
                        },
                        new()
                        {
                            Id = new Guid("caaa44eb-cd40-4177-b930-79d3ef2afe87"),
                            Type = "tag",
                            Attributes = new()
                            {
                                Group = "theme",
                                Name =
                                {
                                    { "en", "School Life" }
                                }
                            }
                        },
                        new()
                        {
                            Id = new Guid("e5301a23-ebd9-49dd-a0cb-2add944c7fe9"),
                            Type = "tag",
                            Attributes = new()
                            {
                                Group = "genre",
                                Name =
                                {
                                    { "en", "Slice of Life" }
                                }
                            }
                        },
                        new()
                        {
                            Id = new Guid("fad12b5e-68ba-460e-b933-9ae8318f5b65"),
                            Type = "tag",
                            Attributes = new()
                            {
                                Group = "theme",
                                Name =
                                {
                                    { "en", "Gyaru" }
                                }
                            }
                        }
                    ],
                },
                Relationships =
                [
                    new AuthorEntity()
                    {
                        Id = new Guid(),
                        Type = "author",
                        Attributes = new()
                        {
                            Name = "Norishiro-chan"
                        }
                    },
                    new ArtistEntity()
                    {
                        Id = new Guid(),
                        Type = "artist",
                        Attributes = new()
                        {
                            Name = "Sakana Uozimi"
                        }
                    }
                ]
            }
        };

        MangaDexCollectionResponse feedCollectionResponse = new()
        {
            Result = "ok",
            Response = "collection",
            Data =
            [
                new ChapterEntity()
                {
                    Id = new Guid("46084762-855c-46dd-a7b6-66e5cd15604d"),
                    Type = "chapter",
                    Attributes = new()
                    {
                        TranslatedLanguage = "en",
                        Volume = null,
                        Chapter = "69",
                        Title = "Otaku & Gyaru & Playing Couple"
                    }
                },
                new ChapterEntity()
                {
                    Id = new Guid("7521d1eb-0caf-4c4d-b96a-adc816ada3ec"),
                    Type = "chapter",
                    Attributes = new()
                    {
                        TranslatedLanguage = "en",
                        Volume = "9",
                        Chapter = "68",
                        Title = "Otaku & Gyaru & A Couple Date"
                    }
                },
                new ChapterEntity()
                {
                    Id = new Guid("b5206e9b-6e3e-4ef0-aa62-381fd0ff75a5"),
                    Type = "chapter",
                    Attributes = new()
                    {
                        TranslatedLanguage = "en",
                        Volume = "2",
                        Chapter = "8.1",
                        Title = "Otaku & Gyaru & Protegee"
                    }
                }
            ]
        };

        MangaDexCollectionResponse coverArtCollectionResponse = new()
        {
            Result = "ok",
            Response = "collection",
            Data =
            [
                new CoverArtEntity()
                {
                    Id = new Guid("0045f243-5625-4f0f-9066-a6c3a95d84d3"),
                    Type = "cover_art",
                    Attributes = new()
                    {
                        FileName = "2569ffd8-4ba1-4030-8d08-b7a21333a7a6.jpg"
                    }
                },
                new CoverArtEntity()
                {
                    Id = new Guid("a81ad8d3-ba2c-4003-9126-fbd9d28e3732"),
                    Type = "cover_art",
                    Attributes = new()
                    {
                        FileName = "d2314e9b-4287-4e65-8045-b713d97c0b28.jpg"
                    }
                }
            ]
        };

        IMangaDexClient mangaDexClient = Substitute.For<IMangaDexClient>();

        mangaDexClient.GetMangaAsync(Arg.Any<Guid>(), CancellationToken.None)
            .Returns(mangaEntityResponse);

        mangaDexClient.GetFeedAsync(Arg.Any<Guid>(), CancellationToken.None)
           .Returns(feedCollectionResponse);

        mangaDexClient.GetCoverArtAsync(Arg.Any<Guid>(), CancellationToken.None)
           .Returns(coverArtCollectionResponse);

        MangaDexMetadataProvider metadataProvider = new(mangaDexClient);
        SourceManga? sourceManga = await metadataProvider.GetMangaAsync("https://mangadex.org/title/ee96e2b7-9af2-4864-9656-649f4d3b6fec/gals-can-t-be-kind-to-otaku", CancellationToken.None);

        sourceManga.ShouldNotBeNull();
        sourceManga.Title.ShouldBe("Gals Can’t Be Kind to Otaku!?");

        sourceManga.AlternateTitles.Count.ShouldBe(5);

        sourceManga.AlternateTitles[0].Title.ShouldBe("オタクに優しいギャルはいない！？");
        sourceManga.AlternateTitles[0].Language.ShouldBe(SourceMangaLanguage.Japanese);

        sourceManga.AlternateTitles[1].Title.ShouldBe("Otaku ni Yasashii Gal wa Inai!?");
        sourceManga.AlternateTitles[1].Language.ShouldBe(SourceMangaLanguage.Romanji);

        sourceManga.AlternateTitles[2].Title.ShouldBe("Otaku ni Yasashii Gyaru ha Inai!?");
        sourceManga.AlternateTitles[2].Language.ShouldBe(SourceMangaLanguage.Romanji);

        sourceManga.AlternateTitles[3].Title.ShouldBe("Gal Can't Be Kind to Otaku!?");
        sourceManga.AlternateTitles[3].Language.ShouldBe(SourceMangaLanguage.English);

        sourceManga.AlternateTitles[4].Title.ShouldBe("Gals Can't Be Kind To A Geek!?");
        sourceManga.AlternateTitles[4].Language.ShouldBe(SourceMangaLanguage.English);

        sourceManga.Genres.Count.ShouldBe(5);
        sourceManga.Genres[0].ShouldBe("Romance");
        sourceManga.Genres[1].ShouldBe("Comedy");
        sourceManga.Genres[2].ShouldBe("School Life");
        sourceManga.Genres[3].ShouldBe("Slice of Life");
        sourceManga.Genres[4].ShouldBe("Gyaru");

        sourceManga.Contributors.Length.ShouldBe(2);

        sourceManga.Contributors[0].Name.ShouldBe("Norishiro-chan");
        sourceManga.Contributors[0].Role.ShouldBe(SourceMangaContributorRole.Author);

        sourceManga.Contributors[1].Name.ShouldBe("Sakana Uozimi");
        sourceManga.Contributors[1].Role.ShouldBe(SourceMangaContributorRole.Artist);

        sourceManga.Chapters.Count.ShouldBe(3);

        sourceManga.Chapters[0].Volume.ShouldBeNull();
        sourceManga.Chapters[0].Number.ShouldBe(69);
        sourceManga.Chapters[0].Title.ShouldBe("Otaku & Gyaru & Playing Couple");
        sourceManga.Chapters[0].Url.ShouldBe("https://mangadex.org/chapter/46084762-855c-46dd-a7b6-66e5cd15604d");

        sourceManga.Chapters[1].Volume.ShouldBe(9);
        sourceManga.Chapters[1].Number.ShouldBe(68);
        sourceManga.Chapters[1].Title.ShouldBe("Otaku & Gyaru & A Couple Date");
        sourceManga.Chapters[1].Url.ShouldBe("https://mangadex.org/chapter/7521d1eb-0caf-4c4d-b96a-adc816ada3ec");

        sourceManga.Chapters[2].Volume.ShouldBe(2);
        sourceManga.Chapters[2].Number.ShouldBe(8.1f);
        sourceManga.Chapters[2].Title.ShouldBe("Otaku & Gyaru & Protegee");
        sourceManga.Chapters[2].Url.ShouldBe("https://mangadex.org/chapter/b5206e9b-6e3e-4ef0-aa62-381fd0ff75a5");

        sourceManga.CoverArt.Length.ShouldBe(2);
        sourceManga.CoverArt[0].ShouldBe("https://mangadex.org/covers/ee96e2b7-9af2-4864-9656-649f4d3b6fec/2569ffd8-4ba1-4030-8d08-b7a21333a7a6.jpg");
        sourceManga.CoverArt[1].ShouldBe("https://mangadex.org/covers/ee96e2b7-9af2-4864-9656-649f4d3b6fec/d2314e9b-4287-4e65-8045-b713d97c0b28.jpg");
    }
}