using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using YMhut.Box.Core.Tools;

namespace YMhut.Box.WinUI.Views.Tools;

public abstract partial class AdaptiveToolPage
{
    private Border BuildModernGalleryHeader(IToolModule module)
    {
        var back = ModernUi.IconButton("\uE72B", AppLocalizer.T("返回工具箱", "Back to toolbox"), () => _goBack?.Invoke());

        var metaRow = ModernUi.BadgeRow(
            new UIElement[]
            {
                ModernUi.Badge(ToolText.CategoryLabel(module.Metadata.Category), ModernUi.Accent, ModernUi.AccentSoft),
                ModernUi.Badge(InteractionLabel(_spec.InteractionMode), ModernUi.TextSecondary, ModernUi.SurfaceAlt),
                ModernUi.Badge(ResultLabel(_spec.Result), ModernUi.TextSecondary, ModernUi.SurfaceAlt)
            },
            itemWidth: 116,
            maxHeight: 58);

        var title = new StackPanel
        {
            Spacing = 2,
            VerticalAlignment = VerticalAlignment.Center,
            Children =
            {
                ModernUi.Text(ToolText.Name(module), 22, FontWeights.SemiBold, maxLines: 1),
                metaRow
            }
        };
        ToolTipService.SetToolTip(title, ToolText.Description(module));

        var grid = new Grid { ColumnSpacing = 12 };
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
        grid.ColumnDefinitions.Add(new ColumnDefinition());
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
        grid.Children.Add(back);

        var icon = ModernUi.IconTile(module.Metadata.IconGlyph, 42, ModernUi.AccentSoft, ModernUi.Accent, 19);
        Grid.SetColumn(icon, 1);
        grid.Children.Add(icon);

        Grid.SetColumn(title, 2);
        grid.Children.Add(title);

        Grid.SetColumn(_progressRing, 3);
        grid.Children.Add(_progressRing);

        grid.SizeChanged += (_, e) => metaRow.MaxWidth = Math.Max(240, e.NewSize.Width - 160);
        return ModernUi.Card(grid, new Thickness(14), new Thickness(32, 20, 32, 10), radius: 8);
    }

    private Border BuildModernGalleryInputCard(IToolModule module)
    {
        var panel = new StackPanel { Spacing = _spec.PageDensity == ToolPageDensity.Compact ? 10 : 14 };
        panel.Children.Add(BuildGallerySectionHeader(
            AppLocalizer.T("输入", "Input"),
            InputTitle(module),
            CompactInputDescription(module),
            "\uE8D2",
            compact: true));

        if (_parameterBox.Visibility == Visibility.Visible)
        {
            panel.Children.Add(_parameterBox);
        }

        if (_queryBox.Visibility == Visibility.Visible)
        {
            panel.Children.Add(_queryBox);
        }

        if (_startDatePicker.Visibility == Visibility.Visible)
        {
            panel.Children.Add(BuildDatePanel());
        }

        if (UsesNumericPrimaryInput())
        {
            panel.Children.Add(BuildNumberInputPanel());
        }

        if (_inputBox.Visibility == Visibility.Visible)
        {
            panel.Children.Add(_inputBox);
        }

        var options = BuildModernInlineOptionsPanel(module);
        if (options is not null)
        {
            panel.Children.Add(options);
        }

        var actions = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            Spacing = 10,
            Margin = new Thickness(0, 2, 0, 0)
        };
        AddSemanticActions(actions);
        panel.Children.Add(new ScrollViewer
        {
            HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
            VerticalScrollBarVisibility = ScrollBarVisibility.Disabled,
            Content = actions
        });

        return ModernUi.Card(panel, new Thickness(16), radius: 8);
    }

    private Border? BuildModernInlineOptionsPanel(IToolModule module)
    {
        var content = BuildAssistPanelContent(module);
        if (content.Children.Count == 0)
        {
            return null;
        }

        var expander = new Expander
        {
            IsExpanded = false,
            Header = BuildGallerySectionHeader(
                AppLocalizer.T("更多选项", "More options"),
                AssistPanelTitle(),
                RuleDescription(module),
                "\uE713",
                compact: true),
            Content = content
        };

        return ModernUi.Card(expander, new Thickness(12), radius: 8);
    }
}
