using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Markdig.Syntax;

namespace MarkdownEditor.Views.Renderers;

/// <summary>
/// Renders markdown paragraph blocks with inline formatting.
/// </summary>
public class ParagraphRenderer : IMarkdownBlockRenderer
{
    private readonly InlineRenderer _inlineRenderer;

    /// <summary>
    /// Initializes a new instance of the <see cref="ParagraphRenderer"/> class.
    /// </summary>
    /// <param name="inlineRenderer">The inline renderer for formatting inline elements.</param>
    public ParagraphRenderer(InlineRenderer inlineRenderer)
    {
        _inlineRenderer = inlineRenderer;
    }

    /// <inheritdoc/>
    public bool CanRender(Block block) => block is ParagraphBlock;

    /// <inheritdoc/>
    public Control? Render(Block block)
    {
        if (block is not ParagraphBlock paragraph)
        {
            return null;
        }

        var textBlock = new TextBlock
        {
            Margin = new Thickness(0, 5, 0, 5),
            TextWrapping = TextWrapping.Wrap
        };

        if (textBlock.Inlines != null)
        {
            _inlineRenderer.BuildInlines(paragraph.Inline, textBlock.Inlines);
        }

        return textBlock;
    }
}

