using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Beam.Abstractions;

namespace Beam.Dynamic {
    public class ParagraphedContentDataProvider : IComposableDataProvider<string> {
        public IBinding? Content { get; set; }

        public string Get(HtmlDocument document) {
            if (Content is null)
                return "";

            var node = Content.Select(document);
            return node is null ? "" : Get(node);
        }

        public string Get(HtmlNode node) {
            StringBuilder content = new();
            foreach(var childNode in node.ChildNodes) {
                if (childNode.Name != "p")
                    continue;
                content.AppendLine(childNode.InnerText);
            }

            return content.ToString();
        }
        public HtmlNode? Select(HtmlDocument doc) {
            return Content?.Select(doc);
        }

        public HtmlNode? Select(HtmlNode node) {
            return node;
        }
    }
}
