using UnityEngine;
using UnityEngine.UIElements;

namespace LiveOpsExamples
{
  public class SetupOverlayPanel : MonoBehaviour
  {
    [SerializeField] private PanelRenderer panelRenderer;
    [SerializeField] private CozyCollectorController controller;

    private VisualElement _overlay;
    private Label _errorLabel;

    private void OnEnable()
    {
      panelRenderer.RegisterUIReloadCallback(OnUIReload);
      controller.PhaseChanged += ApplyVisibility;
    }

    private void OnDisable()
    {
      panelRenderer.UnregisterUIReloadCallback(OnUIReload);
      controller.PhaseChanged -= ApplyVisibility;
    }

    private void OnUIReload(PanelRenderer renderer, VisualElement root)
    {
      _overlay = root.Q<VisualElement>("setup-overlay");
      _errorLabel = root.Q<Label>("setup-error");
      ApplyVisibility();
    }

    private void Update()
    {
      _errorLabel.text = controller.SetupError;
    }

    private void ApplyVisibility()
    {
      _overlay.style.display = controller.Phase == CozyPhase.Setup ? DisplayStyle.Flex : DisplayStyle.None;
    }
  }
}
