using Content.Shared.Store; using JetBrains.Annotations; using System.Linq; using Content.Shared.Store.Components; using Robust.Client.UserInterface; using Robust.Shared.Prototypes; namespace Content.Client.Store.Ui; [UsedImplicitly] public sealed class StoreBoundUserInterface : BoundUserInterface { private IPrototypeManager _prototypeManager = default!; [ViewVariables] private StoreMenu? _menu; [ViewVariables] private string _search = string.Empty; [ViewVariables] private HashSet _listings = new(); public StoreBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey) { } protected override void Open() { _menu = this.CreateWindow(); if (EntMan.TryGetComponent(Owner, out var store)) _menu.Title = Loc.GetString(store.Name); _menu.OnListingButtonPressed += (_, listing) => { SendMessage(new StoreBuyListingMessage(listing)); }; _menu.OnCategoryButtonPressed += (_, category) => { _menu.CurrentCategory = category; _menu?.UpdateListing(); }; _menu.OnWithdrawAttempt += (_, type, amount) => { SendMessage(new StoreRequestWithdrawMessage(type, amount)); }; _menu.SearchTextUpdated += (_, search) => { _search = search.Trim().ToLowerInvariant(); UpdateListingsWithSearchFilter(); }; _menu.OnRefundAttempt += (_) => { SendMessage(new StoreRequestRefundMessage()); }; } protected override void UpdateState(BoundUserInterfaceState state) { base.UpdateState(state); switch (state) { case StoreUpdateState msg: _listings = msg.Listings; _menu?.UpdateBalance(msg.Balance); UpdateListingsWithSearchFilter(); _menu?.SetFooterVisibility(msg.ShowFooter); _menu?.UpdateRefund(msg.AllowRefund); break; } } private void UpdateListingsWithSearchFilter() { if (_menu == null) return; var filteredListings = new HashSet(_listings); if (!string.IsNullOrEmpty(_search)) { filteredListings.RemoveWhere(listingData => !ListingLocalisationHelpers.GetLocalisedNameOrEntityName(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search) && !ListingLocalisationHelpers.GetLocalisedDescriptionOrEntityDescription(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search)); } _menu.PopulateStoreCategoryButtons(filteredListings); _menu.UpdateListing(filteredListings.ToList()); } }