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

namespace YMhut.Box.WinUI.Views;

public sealed partial class ToolDetailPage
{
    private Border BuildModernHeader()
    {
        if (_module is null)
        {
            return ModernUi.Card(
                ModernUi.Text(AppLocalizer.T("工具详情", "Tool details"), 20, FontWeights.SemiBold),
                new Thickness(14),
                margin: new Thickness(24, 20, 24, 10),
                radius: 8);
        }

        var back = ModernUi.IconButton("\uE72B", AppLocalizer.T("返回工具箱", "Back to toolbox"), () => _goBack?.Invoke());
        var icon = ModernUi.IconTile(_module.Metadata.IconGlyph, 42, ModernUi.AccentSoft, ModernUi.Accent, 19);
        var meta = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            Spacing = 8,
            Children =
            {
                ModernUi.SmallBadge(ToolText.CategoryLabel(_module.Metadata.Category), ModernUi.Accent, ModernUi.AccentSoft),
                ModernUi.SmallBadge(_module.Metadata.OfflineCapable ? AppLocalizer.T("本地", "Local") : AppLocalizer.T("联网", "Online"), ModernUi.TextSecondary, ModernUi.SurfaceAlt)
            }
        };

        var title = new StackPanel
        {
            Spacing = 2,
            VerticalAlignment = VerticalAlignment.Center,
            Children =
            {
                ModernUi.Text(ToolText.Name(_module), 22, FontWeights.SemiBold, maxLines: 1),
                meta
            }
        };
        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);
        Grid.SetColumn(icon, 1);
        grid.Children.Add(icon);
        Grid.SetColumn(title, 2);
        grid.Children.Add(title);

        FrameworkElement right = _module.Id is "safe_browser" or "media_player"
            ? ModernUi.SmallBadge(AppLocalizer.T("内嵌工具", "Embedded tool"), ModernUi.TextSecondary, ModernUi.SurfaceAlt)
            : ModernUi.PillButton(AppLocalizer.T("执行", "Run"), "\uE768", RunCurrentTool, primary: true);
        Grid.SetColumn(right, 3);
        grid.Children.Add(right);

        return ModernUi.Card(grid, new Thickness(14), margin: new Thickness(24, 20, 24, 10), radius: 8);
    }
}
