eorzea-votes/client/EorzeaVotes/Ui/MoreDetailsWindow.cs

116 lines
3.4 KiB
C#

using System.Numerics;
using Dalamud.Interface.Utility;
using EorzeaVotes.Model;
using EorzeaVotes.Utilities;
using ImGuiNET;
namespace EorzeaVotes.Ui;
internal class MoreDetailsWindow : IDrawable {
private Plugin Plugin { get; }
internal FullQuestion Question { get; }
private bool _visible = true;
private bool _fetching;
private bool _showEmptyCategories;
private Breakdown? _breakdownType;
private Dictionary<string, ulong[]>? _breakdown;
internal MoreDetailsWindow(Plugin plugin, FullQuestion question) {
this.Plugin = plugin;
this.Question = question;
}
public DrawStatus Draw() {
const ImGuiWindowFlags flags = ImGuiWindowFlags.NoSavedSettings;
if (!this._visible) {
return DrawStatus.Finished;
}
ImGui.SetNextWindowSize(new Vector2(512, 512), ImGuiCond.Appearing);
using var end = new OnDispose(ImGui.End);
if (!ImGui.Begin(this.Question.Text, ref this._visible, flags)) {
return DrawStatus.Continue;
}
ImGui.PushTextWrapPos();
using var popTextWrapPos = new OnDispose(ImGui.PopTextWrapPos);
ImGui.TextUnformatted(this.Question.Text);
ImGui.Spacing();
var changed = ImGuiHelper.EnumDropDownVertical(
"Break down by",
"No breakdown",
ref this._breakdownType,
nameFunction: stat => stat?.ToHuman() ?? "No breakdown"
);
ImGui.Checkbox("Show empty categories", ref this._showEmptyCategories);
if (changed) {
this.Fetch();
}
this.DrawBreakdown();
return DrawStatus.Continue;
}
private void DrawBreakdown() {
using var endChild = new OnDispose(ImGui.EndChild);
if (!ImGui.BeginChild($"##{this.Question.Id}-moredetails-breakdown")) {
return;
}
if (this._fetching) {
ImGuiHelpers.CenteredText("Fetching results...");
return;
}
if (this._breakdown == null || this._breakdownType == null) {
this.Question.DrawResponses();
return;
}
var keys = this._breakdownType.Value.GetBreakdownKeys();
foreach (var key in keys) {
if (!this._breakdown.TryGetValue(key, out var data)) {
data = new ulong[this.Question.Answers.Length];
}
if (!this._showEmptyCategories && data.Aggregate(0ul, (agg, val) => agg + val) == 0) {
continue;
}
string? keyLabel = null;
if (BreakdownExt.KeyMap.TryGetValue(this._breakdownType.Value, out var keyMap)) {
keyMap.TryGetValue(key, out keyLabel);
}
keyLabel ??= Util.SnakeToHuman(key);
if (!ImGui.CollapsingHeader(keyLabel)) {
continue;
}
this.Question.DrawTable(data, $"##-{this.Question.Text}-{this._breakdownType}");
}
}
private void Fetch() {
if (this._breakdownType is { } type) {
this._fetching = true;
Task.Run(async () => {
try {
this._breakdown = await this.Plugin.Manager.GetBreakdown(this.Question.Id, type);
} finally {
this._fetching = false;
}
});
} else {
this._breakdown = null;
}
}
}