using HtmlAgilityPack;

namespace JSMR.Infrastructure.Scanning.Models;

public class DLSiteHtmlDocument
{
    private readonly HtmlNodeCollection _workColumns;
    private readonly HtmlNodeCollection _workColumnRights;
    private readonly HtmlNodeCollection _workThumbs;

    public HtmlNode PageTotalNode { get; }

    public DLSiteHtmlDocument(HtmlDocument document)
    {
        _workColumns = document.DocumentNode.SelectNodes("//dl[@class='work_1col']");
        //_workColumnRights = document.DocumentNode.SelectNodes("//td[@class='work_1col_right']");
        _workColumnRights = document.DocumentNode.SelectNodes("//td[contains(@class, 'work_1col_right')]");
        _workThumbs = document.DocumentNode.SelectNodes("//div[@class='work_thumb']");

        PageTotalNode = document.DocumentNode.SelectNodes("//div[@class='page_total']/strong")[0];
    }

    public List<DLSiteHtmlNode> GetDLSiteNodes()
    {
        var nodes = new List<DLSiteHtmlNode>();

        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++)
        {
            var node = new DLSiteHtmlNode(_workColumns[i], _workColumnRights[i], _workThumbs[i]);
            nodes.Add(node);
        }

        return nodes;
    }
}