using JetBrains.Annotations;
using System;
using UnityEngine;
namespace NEG.UI.Popup
{
    [PublicAPI]
    public class PopupData
    {
        /// 
        /// Event that is fired on closing popup.
        /// 
        public event Action PopupClosedEvent
        {
            add => popup.OnPopupClosed += value;
            remove => popup.OnPopupClosed -= value;
        }
        /// 
        /// Is this data is still valid. If set to false, popup will not show.
        /// 
        public bool IsValid { get; protected set; }
        public IPopup Popup => popup;
        
        protected IPopup popup;
        /// 
        /// PopupData constructor.
        /// 
        /// attached to this data, can be used by different data instances
        public PopupData(IPopup popup)
        {
            this.popup = popup;
            IsValid = true;
        }
        
        /// 
        /// Show popup and pass needed data.
        /// 
        public virtual void Show() => popup.Show(this);
        
        /// 
        /// Hide popup. Close visuals without firing events;
        /// 
        public virtual void Hide()
        {
            if(popup is MonoBehaviour behaviour && behaviour == null)
                return;
            popup.Close(true);
        }
        /// 
        /// Invalidate popup,  will automatically skip this popup
        /// 
        public virtual void Invalidate()
        {
            IsValid = false;
            UiManager.Instance.RefreshPopups();
        }
    }
}