using System.Linq;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Utility;

namespace Robust.Client.Graphics.FontManagement;

[GenerateTypedNameReferences]
internal sealed partial class SystemFontDebugWindow : DefaultWindow
{
    private static readonly int[] ExampleFontSizes = [8, 12, 16, 24, 36];
    private const string ExampleString = "The quick brown fox jumps over the lazy dog";

    [Dependency] private ISystemFontManager _systemFontManager = default!;

    public SystemFontDebugWindow()
    {
        IoCManager.InjectDependencies(this);
        RobustXamlLoader.Load(this);

        var buttonGroup = new ButtonGroup();

        foreach (var group in _systemFontManager.SystemFontFaces.GroupBy(k => k.FamilyName).OrderBy(k => k.Key))
        {
            var fonts = group.ToArray();
            SelectorContainer.AddChild(new Selector(this, buttonGroup, group.Key, fonts));
        }
    }

    private void SelectFontFamily(ISystemFontFace[] fonts)
    {
        FamilyLabel.Text = fonts[0].FamilyName;

        FaceContainer.RemoveAllChildren();

        foreach (var font in fonts)
        {
            var exampleContainer = new BoxContainer
            {
                Orientation = BoxContainer.LayoutOrientation.Vertical,
                Margin = new Thickness(8)
            };

            foreach (var size in ExampleFontSizes)
            {
                var fontInstance = font.Load(size);

                var richTextLabel = new RichTextLabel
                {
                    Stylesheet = new Stylesheet([
                        StylesheetHelpers.Element<RichTextLabel>().Prop("font", fontInstance)
                    ]),
                };
                richTextLabel.SetMessage(FormattedMessage.FromUnformatted(ExampleString));
                exampleContainer.AddChild(richTextLabel);
            }

            FaceContainer.AddChild(new BoxContainer
            {
                Orientation = BoxContainer.LayoutOrientation.Vertical,
                Children =
                {
                    new RichTextLabel
                    {
                        Text = $"""
                        {font.FullName}
                        Family: "{font.FamilyName}", face: "{font.FaceName}", PostScript = "{font.PostscriptName}"
                        Weight: {font.Weight} ({(int) font.Weight}), slant: {font.Slant} ({(int) font.Slant}), width: {font.Width} ({(int) font.Width})
                        """,
                    },
                    exampleContainer
                },
                Margin = new Thickness(0, 0, 0, 8)
            });
        }
    }

    private sealed class Selector : Control
    {
        public Selector(SystemFontDebugWindow window, ButtonGroup group, string family, ISystemFontFace[] fonts)
        {
            var button = new Button
            {
                Text = family,
                Group = group,
                ToggleMode = true
            };
            AddChild(button);

            button.OnPressed += _ => window.SelectFontFamily(fonts);
        }
    }
}
