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

namespace MarkdownEditor.Views.Renderers;

/// <summary>
/// Renders markdown quote blocks.
/// </summary>
public class QuoteRenderer : IMarkdownBlockRenderer
{
    private readonly MarkdownRendererCoordinator _coordinator;

    /// <summary>
    /// Initializes a new instance of the <see cref="QuoteRenderer"/> class.
    /// </summary>
    /// <param name="coordinator">The coordinator for rendering nested blocks.</param>
    public QuoteRenderer(MarkdownRendererCoordinator coordinator)
    {
        _coordinator = coordinator;
    }

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

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

        var panel = new StackPanel();

        foreach (var childBlock in quote)
        {
            var control = _coordinator.RenderBlock(childBlock);
            if (control != null)
            {
                panel.Children.Add(control);
            }
        }

        return new Border
        {
            BorderBrush = new SolidColorBrush(Color.Parse(AppConstants.Colors.Accent)),
            BorderThickness = new Thickness(3, 0, 0, 0),
            Padding = new Thickness(10, 0, 0, 0),
            Margin = new Thickness(0, 5, 0, 5),
            Child = panel
        };
    }
}

