#nullable enable
using System;
using System.Threading.Tasks;
using IGP.Multiplayer;
using IGP.UnitySDK;
using UnityEngine;

public sealed class IGPMultiplayerDataPlaneExample : MonoBehaviour
{
    [SerializeField] private IGPMultiplayerRuntime? multiplayerRuntime = null;

    private void Awake() =>
        multiplayerRuntime ??= FindObjectOfType<IGPMultiplayerRuntime>();

    public async Task ConnectAsync(IGPMultiplayerBootstrap bootstrap)
    {
        if (multiplayerRuntime == null)
        {
            throw new InvalidOperationException("IGPMultiplayerRuntime is not assigned.");
        }

        bool connected = await multiplayerRuntime.TryCreateRuntimeAsync(bootstrap);
        if (!connected || !multiplayerRuntime.IsRealtimeReady)
        {
            throw new InvalidOperationException("RoomNode Game data plane did not become ready.");
        }

        Debug.Log($"IGP Game data plane ready for room {bootstrap.Room.Id}.");
    }
}
