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

public class BackKeyMgr : MonoBehaviour
{
    [RuntimeInitializeOnLoadMethod]
    static void OnRuntimeMethodLoad()
    {
        new GameObject("BackKeyMgr").AddComponent<BackKeyMgr>();
    }

    public static BackKeyMgr Ins;

    List<Action> list_back = new List<Action>();

    private void Awake()
    {
        Ins = this;
        DontDestroyOnLoad(gameObject);
    }

    public void Run_AllBack()
    {
        while (list_back.Count > 0)
            list_back[0]?.Invoke();
    }

    public void Add_Back(Action _back)
    {
        var act = list_back.Find(f => f == _back);
        if (act == null)
            list_back.Add(_back);
    }

    public void Del_Back(Action _back)
    {
        var act = list_back.Find(f => f == _back);
        if (act != null)
            list_back.Remove(_back);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (BackKeyAdd.DontBack) return;

            if (list_back.Count > 0)
                list_back[list_back.Count - 1]?.Invoke();
            else
            {
                //if (GameUI.Ins && GameUI.Ins.IsSleepMode()) return;
                //Popup.Ins.Set_QuitPopup();
            }
        }
    }
}