using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NotificationUI : MonoBehaviour {

    [SerializeField] private GameObject notificationUIItemGameObject;
    
    public static NotificationUI Instance { get; private set; }

    private Queue<Action> _actionQueue = new Queue<Action>(); 
    
    private void Awake() {
        if (Instance == null) {
            Instance = this;
        }
    }

    void Update() {
        if (_actionQueue.Count > 0) {
            _actionQueue.Dequeue().Invoke();
        }
    }

    public void CreateNotification(string alertContent) {
        _actionQueue.Enqueue( () => {
            var item = Instantiate(notificationUIItemGameObject, transform, true);
            item.transform.localScale = new Vector3(1, 1, 1);
            item.GetComponent<RectTransform>().offsetMin = new Vector2(0, 0);
            item.GetComponent<RectTransform>().offsetMax = new Vector2(0, 0);
            Debug.Log(alertContent);
            item.GetComponent<NotificationUIItem>().Initialize(alertContent);
        });
    }
    
    public void CreateNotificationAndCloseLoadingUI(string alertContent) {
        _actionQueue.Enqueue( () => {
            LoadingUI.Instance.CloseUI();

            var item = Instantiate(notificationUIItemGameObject, transform, true);
            item.transform.localScale = new Vector3(1, 1, 1);
            item.GetComponent<RectTransform>().offsetMin = new Vector2(0, 0);
            item.GetComponent<RectTransform>().offsetMax = new Vector2(0, 0);
            Debug.Log(alertContent);
            item.GetComponent<NotificationUIItem>().Initialize(alertContent);
        });
    }
}