using UnityEngine;

public static class GlobalInput
{
    private class InputTracker : MonoBehaviour
    {
        private void Start() => DontDestroyOnLoad(gameObject);

        private bool m_IsContact;
        private bool m_TouchStartedThisFrame;
        private float m_HeldTime;

        private static bool IsPressedInternal()
        {
            return
                Input.GetMouseButton(0) ||
                Input.anyKey ||
                Input.touchCount != 0;
        }

        private void Update()
        {
            float tmp = m_HeldTime;
            m_HeldTime = 0f;

            if (!IsPressedInternal())
            {
                m_IsContact = false;
                return;
            }

            m_TouchStartedThisFrame = !m_IsContact;
            m_IsContact = true;

            m_HeldTime = tmp + Time.unscaledDeltaTime;
        }

        public bool _IsScreenClicked() => m_TouchStartedThisFrame;
        public bool _IsScreenHeld() => m_IsContact && m_HeldTime > 0.5f;
    }

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void Initialize()
    {
        GameObject go = new ("InputTracker");
        s_InputTracker = go.AddComponent<InputTracker>();
    }
    
    private static InputTracker s_InputTracker;

    public static bool IsScreenClicked => s_InputTracker._IsScreenClicked();
    public static bool IsScreenHeld => s_InputTracker._IsScreenHeld();
}
