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

namespace MarkdownEditor.Views.Renderers;

/// <summary>
/// Renders markdown table blocks.
/// </summary>
public class TableRenderer : IMarkdownBlockRenderer
{
    private readonly MarkdownRendererCoordinator _coordinator;
    private readonly bool _isDarkMode;
    private readonly double _fontScale;

    /// <summary>
    /// Initializes a new instance of the <see cref="TableRenderer"/> class.
    /// </summary>
    /// <param name="coordinator">The coordinator for rendering nested blocks.</param>
    /// <param name="isDarkMode">Whether the current theme is dark mode.</param>
    /// <param name="fontScale">Font scale factor for text sizing.</param>
    public TableRenderer(MarkdownRendererCoordinator coordinator, bool isDarkMode = true, double fontScale = 1.0)
    {
        _coordinator = coordinator;
        _isDarkMode = isDarkMode;
        _fontScale = fontScale;
    }

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

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

        var grid = new Grid
        {
            Margin = new Thickness(0, 5, 0, 5)
        };

        // Add column definitions
        if (table.ColumnDefinitions != null)
        {
            foreach (var _ in table.ColumnDefinitions)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Star });
            }
        }

        int rowIndex = 0;
        foreach (var row in table)
        {
            if (row is TableRow tableRow)
            {
                grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });

                int colIndex = 0;
                foreach (var cell in tableRow)
                {
                    if (cell is TableCell tableCell)
                    {
                        var cellContent = new StackPanel();
                        foreach (var childBlock in tableCell)
                        {
                            var control = _coordinator.RenderBlock(childBlock);
                            if (control != null)
                            {
                                cellContent.Children.Add(control);
                            }
                        }

                        var border = new Border
                        {
                            BorderBrush = new SolidColorBrush(Color.Parse(_isDarkMode ? "#555555" : "#CCCCCC")),
                            BorderThickness = new Thickness(1),
                            Padding = new Thickness(8, 4),
                            Background = rowIndex == 0 
                                ? new SolidColorBrush(Color.Parse(_isDarkMode ? "#2D2D2D" : "#F0F0F0"))
                                : Brushes.Transparent,
                            Child = cellContent,
                            [Grid.RowProperty] = rowIndex,
                            [Grid.ColumnProperty] = colIndex
                        };

                        grid.Children.Add(border);
                        colIndex++;
                    }
                }
                rowIndex++;
            }
        }

        return new Border
        {
            BorderBrush = new SolidColorBrush(Color.Parse(_isDarkMode ? "#555555" : "#CCCCCC")),
            BorderThickness = new Thickness(1),
            Margin = new Thickness(0, 5, 0, 5),
            Child = grid
        };
    }
}

