#nullable enable

using System.Collections.Generic;
using Content.Shared.Administration.AdminMenu;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.IoC;
using Robust.Shared.Maths;

namespace Content.Client.UserInterface.AdminMenu.Tabs
{
    [GenerateTypedNameReferences]
    public partial class PlayerTab : Control
    {
        public delegate void PlayerListRefresh();

        public event PlayerListRefresh? OnPlayerListRefresh;

        public PlayerTab()
        {
            IoCManager.InjectDependencies(this);
            RobustXamlLoader.Load(this);
            RefreshButton.OnPressed += (_) => OnPlayerListRefresh?.Invoke();
        }

        protected override void EnteredTree()
        {
            OnPlayerListRefresh?.Invoke();
        }

        public void RefreshPlayerList(IEnumerable<AdminMenuPlayerListMessage.PlayerInfo> players)
        {
            PlayerList.RemoveAllChildren();
            var playerManager = IoCManager.Resolve<IPlayerManager>();
            PlayerCount.Text = $"Players: {playerManager.PlayerCount}";

            var altColor = Color.FromHex("#292B38");
            var defaultColor = Color.FromHex("#2F2F3B");

            var header = new HBoxContainer
            {
                HorizontalExpand = true,
                SeparationOverride = 4,
                Children =
                {
                    new Label
                    {
                        Text = "Username",
                        SizeFlagsStretchRatio = 2f,
                        HorizontalExpand = true
                    },
                    new VSeparator(),
                    new Label
                    {
                        Text = "Character",
                        SizeFlagsStretchRatio = 2f,
                        HorizontalExpand = true
                    },
                    new VSeparator(),
                    new Label()
                    {
                        Text = "Antagonist",
                        SizeFlagsStretchRatio = 2f,
                        HorizontalExpand = true,
                    }
                }
            };
            PlayerList.AddChild(new PanelContainer
            {
                PanelOverride = new StyleBoxFlat
                {
                    BackgroundColor = altColor,
                },
                Children =
                {
                    header
                }
            });
            PlayerList.AddChild(new HSeparator());

            var useAltColor = false;
            foreach (var player in players)
            {
                var hBox = new HBoxContainer
                {
                    HorizontalExpand = true,
                    SeparationOverride = 4,
                    Children =
                    {
                        new Label
                        {
                            Text = player.Username,
                            SizeFlagsStretchRatio = 2f,
                            HorizontalExpand = true,
                            ClipText = true
                        },
                        new VSeparator(),
                        new Label
                        {
                            Text = player.CharacterName,
                            SizeFlagsStretchRatio = 2f,
                            HorizontalExpand = true,
                            ClipText = true
                        },
                        new VSeparator(),
                        new Label()
                        {
                            Text = player.Antag ? "YES" : "NO",
                            SizeFlagsStretchRatio = 2f,
                            HorizontalExpand = true,
                            ClipText = true,
                        }
                    }
                };
                PlayerList.AddChild(new PanelContainer
                {
                    PanelOverride = new StyleBoxFlat
                    {
                        BackgroundColor = useAltColor ? altColor : defaultColor,
                    },
                    Children =
                    {
                        hBox
                    }
                });
                useAltColor ^= true;
            }
        }

        private static readonly Color SeparatorColor = Color.FromHex("#3D4059");

        private class VSeparator : PanelContainer
        {
            public VSeparator()
            {
                MinSize = (2, 5);
                AddChild(new PanelContainer
                {
                    PanelOverride = new StyleBoxFlat
                    {
                        BackgroundColor = SeparatorColor
                    }
                });
            }
        }

        private class HSeparator : Control
        {
            public HSeparator()
            {
                AddChild(new PanelContainer
                {
                    PanelOverride = new StyleBoxFlat
                    {
                        BackgroundColor = SeparatorColor,
                        ContentMarginBottomOverride = 2, ContentMarginLeftOverride = 2
                    }
                });
            }
        }
    }
}
