using JetBrains.Annotations;
using NEG.UI.Area;
using NEG.UI.WindowSlot;
using UnityEngine;
namespace NEG.UI.Window
{
    public interface IWindow
    {
        IWindowSlot Parent { get; }
        IWindowSlot ChildWindowSlot { get; }
        
        /// 
        /// Called internally by slot to open window.
        /// 
        /// slot that opens window
        void SetOpenedState(IWindowSlot parentSlot);
        void SetData(object data);
        
        /// 
        /// Called internally to close window by slot.
        /// 
        void SetClosedState();
    }
    public static class WindowInterfaceExtensions
    {
        //Open 
        public static void Open(this IWindow window, IWindowSlot slot = null, object data = null)
        {
            if (slot != null)
            {
                slot.AttachWindow(window);
                window.SetData(data);
            }
            else
                UiManager.Instance.CurrentArea.OpenWindow(window, data);
        }
        public static void Open(this IWindow window, IArea area, object data = null) => area.OpenWindow(window, data);
        public static void OpenChildWindow(this IWindow window, IWindow windowToOpen, object data = null)
        {
            if (window.ChildWindowSlot == null)
            {
                Debug.LogError("This window doesn't contain area for child windows");
                return;
            }
            //TODO: DO it
            //window.ChildWindowArea.OpenWindow(windowToOpen, slot);
        }
        public static void Close(this IWindow window) => window.Parent.DetachWindow(window);
    }
}