using System;
using System.Collections.Generic;
using KernelPanic.Combat;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
/// Retained scheduler readout. Completion badges are receipts, not delayed execution.
internal sealed class ExecutionTrackView : IDisposable
{
private readonly VisualElement _contents;
private readonly bool _stack;
private readonly Dictionary _chips = new();
private readonly Dictionary _completed = new();
private CardInstance _active;
private bool _disposed;
public ExecutionTrackView(VisualElement contents, bool stack)
{
_contents = contents;
_stack = stack;
contents.EnableInClassList("stack-contents", stack);
}
private VisualElement Chip(CardInstance card)
{
if (_chips.TryGetValue(card, out var chip)) return chip;
chip = new VisualElement { userData = card, tooltip = CardEffectFactory.GetRulesText(card) };
chip.AddToClassList("card-chip");
chip.Add(SoftwareCardView.Text("", "execution-order"));
chip.Add(SoftwareCardView.Text(card.Definition.DisplayName, "card-chip-name"));
_chips.Add(card, chip);
_contents.Add(chip);
return chip;
}
public void Sync(IReadOnlyList pending)
{
if (_disposed) return;
_contents.Q(className: "track-empty")?.RemoveFromHierarchy();
var retained = new HashSet(pending);
if (_active != null) retained.Add(_active);
var stale = new List();
foreach (var entry in _chips)
if (!retained.Contains(entry.Key) && !_completed.ContainsKey(entry.Key)) stale.Add(entry.Key);
foreach (var card in stale) { _chips[card].RemoveFromHierarchy(); _chips.Remove(card); }
for (int i = 0; i < pending.Count; i++)
{
var card = pending[i];
if (_completed.Remove(card, out var oldReceipt)) oldReceipt.Pause();
var chip = Chip(card);
chip.RemoveFromClassList("operation-complete");
chip.RemoveFromClassList("operation-active");
chip.EnableInClassList("operation-next", i == 0);
chip.Q