using System; using System.Collections.Generic; using System.Linq; using Dalamud.Bindings.ImGui; using Questionable.Controller; using Questionable.Model; using Questionable.Model.Questing; namespace Questionable.Windows.Utils; internal sealed class QuestSelector(QuestRegistry questRegistry) { private string _searchString = string.Empty; public Predicate? SuggestionPredicate { private get; set; } public Predicate? DefaultPredicate { private get; set; } public Action? QuestSelected { private get; set; } public void DrawSelection() { if (QuestSelected == null) { throw new InvalidOperationException("QuestSelected action must be set before drawing the quest selector."); } ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X); if (ImGui.BeginCombo("##QuestSelection", "Add Quest...", ImGuiComboFlags.HeightLarge)) { ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X); bool flag = ImGui.InputTextWithHint("", "Filter...", ref _searchString, 256, ImGuiInputTextFlags.AutoSelectAll | ImGuiInputTextFlags.EnterReturnsTrue); IEnumerable enumerable; if (!string.IsNullOrEmpty(_searchString)) { enumerable = Enumerable.Where(predicate: (!ElementId.TryFromString(_searchString, out ElementId elementId)) ? new Func(DefaultPredicate) : ((Func)((Quest x) => DefaultPredicate(x) || x.Id == elementId)), source: questRegistry.AllQuests.Where(delegate(Quest x) { ElementId id = x.Id; return !(id is SatisfactionSupplyNpcId) && !(id is AlliedSocietyDailyId); })); } else { enumerable = questRegistry.AllQuests.Where((Quest x) => this.DefaultPredicate?.Invoke(x) ?? true); } foreach (Quest item in enumerable) { if ((SuggestionPredicate == null || SuggestionPredicate(item)) && (ImGui.Selectable(item.Info.Name) || flag)) { QuestSelected(item); if (flag) { ImGui.CloseCurrentPopup(); flag = false; } } } ImGui.EndCombo(); } ImGui.Spacing(); bool DefaultPredicate(Quest x) { return x.Info.Name.Contains(_searchString, StringComparison.CurrentCultureIgnoreCase); } } }