using System;
using System.Linq;
using KernelPanic.Combat;
using KernelPanic.Run;
using UnityEngine;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
/// Four pointer-blocking shades leave only the highlighted action exposed.
internal sealed class TutorialSpotlight : IDisposable
{
private readonly VisualElement _root, _overlay, _outline, _panel;
private readonly VisualElement[] _shades = new VisualElement[4];
private readonly Label _title, _copy, _progress;
private readonly Button _next;
private readonly IVisualElementScheduledItem _placement;
private TutorialGuide _guide;
private VisualElement _anchor;
private int _index = -1;
private bool _disposed;
public VisualElement Anchor => _anchor;
public TutorialSpotlight(VisualElement overlay, Action next)
{
_overlay = overlay; _root = overlay.parent ?? overlay;
_overlay.Clear(); _overlay.AddToClassList("guided-overlay");
_overlay.pickingMode = PickingMode.Ignore;
_root.AddToClassList("tutorial-guided");
for (int i = 0; i < _shades.Length; i++)
{ _shades[i] = new VisualElement(); _shades[i].AddToClassList("tutorial-shade"); _overlay.Add(_shades[i]); }
_outline = new VisualElement { name = "TutorialHighlight", pickingMode = PickingMode.Ignore };
_outline.AddToClassList("tutorial-highlight"); _overlay.Add(_outline);
_panel = new VisualElement { name = "TutorialCallout" };
_panel.AddToClassList("tutorial-callout");
_progress = TerminalLayout.BodyText("", "TutorialStepCount"); _panel.Add(_progress);
_title = TerminalLayout.Heading("", "TutorialStepTitle"); _panel.Add(_title);
var scroll = new ScrollView(ScrollViewMode.Vertical) { name = "TutorialStepScroll" };
scroll.style.flexShrink = 1; scroll.style.minHeight = 0;
scroll.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
_copy = TerminalLayout.BodyText("", "TutorialInstruction"); scroll.Add(_copy); _panel.Add(scroll);
_next = new Button(next) { name = "TutorialNext", text = "Next" };
_next.AddToClassList("primary-action"); _panel.Add(_next); _overlay.Add(_panel);
_root.RegisterCallback(TrapFocus, TrickleDown.TrickleDown);
_placement = _overlay.schedule.Execute(Place).Every(33);
}
public void Show(TutorialGuide guide, CombatManager combat)
{
_guide = guide; _overlay.style.display = DisplayStyle.Flex;
_root.EnableInClassList("tutorial-card-focus", guide.Step.Anchor == "card");
_anchor = ResolveAnchor();
_progress.text = $"Wave {combat.RunConfig.TutorialLesson}/2 / step {guide.Index + 1} of {guide.Count}";
_title.text = guide.Step.Title; _copy.text = guide.Step.Copy;
_next.style.display = guide.Reading ? DisplayStyle.Flex : DisplayStyle.None;
_next.SetEnabled(combat.TutorialInputReady);
if (_index != guide.Index)
{
_index = guide.Index;
_panel.Q().scrollOffset = Vector2.zero;
_overlay.schedule.Execute(() =>
{
if (_disposed) return;
_anchor = ResolveAnchor();
for (var ancestor = _anchor?.parent; ancestor != null; ancestor = ancestor.parent)
if (ancestor is ScrollView scroll) scroll.ScrollTo(_anchor);
FocusAction(); Place();
});
}
Place();
}
private VisualElement ResolveAnchor()
{
if (_guide?.Step == null) return null;
string key = _guide.Step.Anchor;
if (key == "card") return _root.Query