using System;
using System.Threading;
using UnityEditor;
using UnityEngine;

public class ConnectionKiller : MonoBehaviour {
    private static readonly CancellationTokenSource EagleGlobalShutdownTokenSource = new();
    public static CancellationToken EagleCancellationToken => EagleGlobalShutdownTokenSource.Token;

    public void OnApplicationQuit() { ShutdownAll(); }

#if UNITY_EDITOR
    void OnPlayModeStateChanged(PlayModeStateChange state) {
        if (state == PlayModeStateChange.ExitingPlayMode) { ShutdownAll(); }
    }
#endif

    public static void ShutdownAll() {
        Console.Out.WriteLine("Attempting GRPC shutdown.");
        EagleGlobalShutdownTokenSource.Cancel();
        Console.Out.WriteLine("Successfully shut down.");
    }
}
