using System;
using UnityEngine;
using Vanguard.Api.Contracts;
using Vanguard.Board3D;
using Vanguard.Shared.Protocol;
namespace Vanguard.UI.Overlays
{
///
/// Один стек экранов поверх боя: список карт → детали карты.
/// Возврат из деталей всегда ведёт обратно в тот же список.
///
public sealed class OverlayNavigationService
{
private readonly RectTransform _root;
private readonly CardListView _list;
private readonly CardDetailOverlay _detail;
private CardListRequest _currentRequest;
public OverlayNavigationService(
Transform parent,
Func catalog)
{
_root = UiKit.CreatePanel(parent, "BattleOverlays", Color.clear);
_root.gameObject.SetActive(false);
_list = new CardListView(_root, catalog);
_detail = new CardDetailOverlay(_root, catalog);
}
public bool IsOpen => _root.gameObject.activeSelf;
public event Action OpenChanged;
public void OpenZone(PlayerView player, int viewer, BoardZone zone)
{
OpenList(CardZoneContent.Build(player, viewer, zone));
}
public void OpenList(CardListRequest request)
{
if (request == null)
return;
_currentRequest = request;
_detail.Hide();
_list.Show(request, OpenDetail, CloseAll);
_root.SetAsLastSibling();
var wasOpen = _root.gameObject.activeSelf;
_root.gameObject.SetActive(true);
if (!wasOpen)
OpenChanged?.Invoke(true);
}
public void OpenCardDetail(CardListEntry entry)
{
if (entry == null)
return;
_currentRequest = null;
_list.Hide();
_detail.Show(entry, CloseAll);
_root.SetAsLastSibling();
var wasOpen = _root.gameObject.activeSelf;
_root.gameObject.SetActive(true);
if (!wasOpen)
OpenChanged?.Invoke(true);
}
private void OpenDetail(CardListEntry entry)
{
_list.Hide();
_detail.Show(entry, BackToList);
_root.SetAsLastSibling();
_root.gameObject.SetActive(true);
}
private void BackToList()
{
_detail.Hide();
if (_currentRequest == null)
{
CloseAll();
return;
}
_list.Show(_currentRequest, OpenDetail, CloseAll);
}
public void CloseAll()
{
var wasOpen = _root != null && _root.gameObject.activeSelf;
_list?.Hide();
_detail?.Hide();
_root.gameObject.SetActive(false);
_currentRequest = null;
if (wasOpen)
OpenChanged?.Invoke(false);
}
public void Dispose()
{
if (_root == null)
return;
if (Application.isPlaying)
UnityEngine.Object.Destroy(_root.gameObject);
else
UnityEngine.Object.DestroyImmediate(_root.gameObject);
}
}
}