using HtmlAgilityPack;

namespace MangaReader.Core.Sources.MangaNato.Metadata;

public class MangaNatoMangaDocument
{
    public HtmlNode? StoryInfoNode { get; }
    public HtmlNode? TitleNode { get; }
    public HtmlNode? StoryInfoRightNode { get; }
    public HtmlNode? VariationsTableInfo { get; }
    public HtmlNodeCollection? VariationsTableValueNodes { get; }
    public HtmlNode? AlternateTitlesNode { get; }
    public HtmlNode? AuthorsNode { get; }
    public HtmlNode? StatusNode { get; }
    public HtmlNode? GenresNode { get; }
    public HtmlNode? StoryInfoRightExtentNode { get; }
    public HtmlNodeCollection? StoryInfoRightExtentValueNodes { get; }
    public HtmlNode? UpdateDateNode { get; }
    public HtmlNode? ViewsNode { get; }
    public HtmlNode? ReviewAggregateNode { get; }
    public HtmlNode? RatingNode { get; }
    public HtmlNode? AverageRatingNode { get; }
    public HtmlNode? BestRatingNode { get; }
    public HtmlNode? VotesNode { get; set; }
    public HtmlNode? StoryDescriptionNode { get; }
    public List<HtmlNode> StoryDescriptionTextNodes { get; }
    public HtmlNode? StoryChapterListNode { get; }
    public HtmlNodeCollection? ChapterNodes { get; }

    public MangaNatoMangaDocument(HtmlDocument document)
    {
        StoryInfoNode = document.DocumentNode.SelectSingleNode(".//div[@class='panel-story-info']");
        TitleNode = StoryInfoNode?.SelectSingleNode(".//h1");
        StoryDescriptionNode = StoryInfoNode?.SelectSingleNode(".//div[@class='panel-story-info-description']");
        StoryDescriptionTextNodes = StoryDescriptionNode?.ChildNodes.Skip(2).Take(StoryDescriptionNode.ChildNodes.Count - 2).ToList() ?? [];

        StoryInfoRightNode = StoryInfoNode?.SelectSingleNode(".//div[@class='story-info-right']");

        VariationsTableInfo = StoryInfoRightNode?.SelectSingleNode(".//table[@class='variations-tableInfo']");
        VariationsTableValueNodes = VariationsTableInfo?.SelectNodes(".//td[@class='table-value']");
        AlternateTitlesNode = VariationsTableValueNodes?.FirstOrDefault();

        if (VariationsTableValueNodes != null && VariationsTableValueNodes.Count >= 3)
        {
            AuthorsNode = VariationsTableValueNodes[1];
            StatusNode = VariationsTableValueNodes[2];
            GenresNode = VariationsTableValueNodes[3];
        }

        StoryInfoRightExtentNode = StoryInfoRightNode?.SelectSingleNode(".//div[@class='story-info-right-extent']");
        StoryInfoRightExtentValueNodes = StoryInfoRightExtentNode?.SelectNodes(".//span[@class='stre-value']");

        if (StoryInfoRightExtentValueNodes != null && StoryInfoRightExtentValueNodes.Count >= 2)
        {
            UpdateDateNode = StoryInfoRightExtentValueNodes[0];
            ViewsNode = StoryInfoRightExtentValueNodes[1];
        }

        // v:Review-aggregate
        ReviewAggregateNode = StoryInfoRightNode?.SelectSingleNode(".//em[@typeof='v:Review-aggregate']");
        RatingNode = ReviewAggregateNode?.SelectSingleNode(".//em[@typeof='v:Rating']");
        AverageRatingNode = RatingNode?.SelectSingleNode(".//em[@property='v:average']");
        BestRatingNode = RatingNode?.SelectSingleNode(".//em[@property='v:best']");
        VotesNode = ReviewAggregateNode?.SelectSingleNode(".//em[@property='v:votes']");

        StoryChapterListNode = document.DocumentNode.SelectSingleNode(".//div[@class='panel-story-chapter-list']");
        ChapterNodes = StoryChapterListNode?.SelectNodes(".//li[@class='a-h']");
    }
}