using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;

namespace Robust.Client.ViewVariables.Editors;

internal sealed class VVPropEditorNullableEntProtoId : VVPropEditor
{
    [Dependency] private readonly IPrototypeManager _protoMan = default!;

    public VVPropEditorNullableEntProtoId()
    {
        IoCManager.InjectDependencies(this);
    }

    protected override Control MakeUI(object? value)
    {
        var lineEdit = new LineEdit
        {
            Text = value is EntProtoId protoId ? protoId.Id : "",
            Editable = !ReadOnly,
            HorizontalExpand = true,
        };

        if (!ReadOnly)
        {
            lineEdit.OnTextEntered += e =>
            {
                if (string.IsNullOrWhiteSpace(e.Text))
                {
                    ValueChanged(null);
                }
                else
                {
                    var id = (EntProtoId)e.Text;
                    if (_protoMan.HasIndex(id))
                        ValueChanged(id);
                }
            };
        }

        return lineEdit;
    }
}
