using HtmlAgilityPack;

namespace JSMR.Infrastructure.Scanning.Models;

public class DLSiteHtmlDocument(HtmlDocument document)
{
    private readonly HtmlNodeCollection _workColumns = document.DocumentNode.SelectNodes("//dl[@class='work_1col']");
    private readonly HtmlNodeCollection _workColumnRights = document.DocumentNode.SelectNodes("//td[contains(@class, 'work_1col_right')]");
    private readonly HtmlNodeCollection _workThumbs = document.DocumentNode.SelectNodes("//div[@class='work_thumb']");
    public HtmlNode PageTotalNode { get; } = document.DocumentNode.SelectNodes("//div[@class='page_total']/strong")[0];

    public DLSiteHtmlNode[] GetDLSiteNodes()
    {
        List<DLSiteHtmlNode> nodes = [];

        if (_workColumns.Count != _workColumnRights.Count || _workColumns.Count != _workThumbs.Count)
            throw new Exception("Work column node counts do not match!");

        for (int i = 0; i < _workColumns.Count; i++)
        {
            DLSiteHtmlNode node = new(_workColumns[i], _workColumnRights[i], _workThumbs[i]);
            nodes.Add(node);
        }

        return [.. nodes];
    }
}