#nullable enable
using System;
using Content.Shared.Administration;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Network;
using Robust.Shared.Utility;

namespace Content.Client.Administration.UI.CustomControls
{
    [GenerateTypedNameReferences]
    public sealed partial class BwoinkPanel : BoxContainer
    {
        private readonly BwoinkSystem _bwoinkSystem;
        public readonly NetUserId ChannelId;

        public int Unread { get; private set; } = 0;
        public DateTime LastMessage { get; private set; } = DateTime.MinValue;

        public BwoinkPanel(BwoinkSystem bwoinkSys, NetUserId userId)
        {
            RobustXamlLoader.Load(this);
            _bwoinkSystem = bwoinkSys;
            ChannelId = userId;

            OnVisibilityChanged += c =>
            {
                if (c.Visible)
                    Unread = 0;
            };
            SenderLineEdit.OnTextEntered += Input_OnTextEntered;
        }

        private void Input_OnTextEntered(LineEdit.LineEditEventArgs args)
        {
            if (string.IsNullOrWhiteSpace(args.Text))
                return;

            _bwoinkSystem.Send(ChannelId, args.Text);
            SenderLineEdit.Clear();
        }

        public void ReceiveLine(SharedBwoinkSystem.BwoinkTextMessage message)
        {
            if (!Visible)
                Unread++;

            var formatted = new FormattedMessage(1);
            formatted.AddMarkup($"[color=gray]{message.SentAt.ToShortTimeString()}[/color] {message.Text}");
            TextOutput.AddMessage(formatted);
            LastMessage = message.SentAt;
        }
    }
}
