using Content.Client.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Trinary.Components;
using Content.Shared.Localizations;
using JetBrains.Annotations;
using Robust.Client.UserInterface;
namespace Content.Client.Atmos.UI
{
///
/// Initializes a and updates it when new server messages are received.
///
[UsedImplicitly]
public sealed class GasFilterBoundUserInterface : BoundUserInterface
{
[ViewVariables]
private const float MaxTransferRate = Atmospherics.MaxTransferRate;
[ViewVariables]
private GasFilterWindow? _window;
public GasFilterBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
var atmosSystem = EntMan.System();
_window = this.CreateWindow();
_window.PopulateGasList(atmosSystem.Gases);
_window.ToggleStatusButtonPressed += OnToggleStatusButtonPressed;
_window.FilterTransferRateChanged += OnFilterTransferRatePressed;
_window.SelectGasPressed += OnSelectGasPressed;
}
private void OnToggleStatusButtonPressed()
{
if (_window is null) return;
SendMessage(new GasFilterToggleStatusMessage(_window.FilterStatus));
}
private void OnFilterTransferRatePressed(string value)
{
var rate = UserInputParser.TryFloat(value, out var parsed) ? parsed : 0f;
SendMessage(new GasFilterChangeRateMessage(rate));
}
private void OnSelectGasPressed()
{
if (_window is null) return;
if (_window.SelectedGas is null)
{
SendMessage(new GasFilterSelectGasMessage(null));
}
else
{
if (!int.TryParse(_window.SelectedGas, out var gas)) return;
SendMessage(new GasFilterSelectGasMessage(gas));
}
}
///
/// Update the UI state based on server-sent info
///
///
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (_window == null || state is not GasFilterBoundUserInterfaceState cast)
return;
_window.Title = (cast.FilterLabel);
_window.SetFilterStatus(cast.Enabled);
_window.SetTransferRate(cast.TransferRate);
if (cast.FilteredGas is not null)
{
var atmos = EntMan.System();
var gas = atmos.GetGas((Gas) cast.FilteredGas);
var gasName = Loc.GetString(gas.Name);
_window.SetGasFiltered(gas.ID, gasName);
}
else
{
_window.SetGasFiltered(null, Loc.GetString("comp-gas-filter-ui-filter-gas-none"));
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_window?.Dispose();
}
}
}