/**
  * Clearing the rich presence
  */
 void ClearPresence()
 {
     UpdateConsole("Clearing Group Presence...");
     GroupPresence.Clear().OnComplete(message => {
         if (message.IsError)
         {
             UpdateConsole(message.GetError().Message);
         }
         else
         {
             // Clearing the rich presence then fetching the user's presence afterwards
             Users.Get(LoggedInUserID).OnComplete(message2 =>
             {
                 if (message2.IsError)
                 {
                     UpdateConsole("Group Presence cleared! But rich presence is unknown!");
                 }
                 else
                 {
                     UpdateConsole("Group Presence cleared!\n" + message2.Data.Presence + "\n");
                 }
             });
         }
     });
 }
    /**
     * Launch the roster panel
     */
    void LaunchRosterPanel()
    {
        UpdateConsole("Launching Roster Panel...");
        var options = new RosterOptions();

        if (SuggestedUserID != 0)
        {
            options.AddSuggestedUser(SuggestedUserID);
        }
        GroupPresence.LaunchRosterPanel(options).OnComplete(message =>
        {
            if (message.IsError)
            {
                UpdateConsole(message.GetError().Message);
            }
        });
    }
    /**
     * Setting the group presence
     */
    void SetPresence()
    {
        var options = new GroupPresenceOptions();

        options.SetDestinationApiName(DestinationAPINames[DestinationIndex]);

        if (!string.IsNullOrEmpty(MatchSessionID))
        {
            options.SetMatchSessionId(MatchSessionID);
        }

        if (!string.IsNullOrEmpty(MatchSessionID))
        {
            options.SetLobbySessionId(LobbySessionID);
        }

        // Set is Joinable to let other players deeplink and join this user via the presence
        options.SetIsJoinable(IsJoinable);

        UpdateConsole("Setting Group Presence to " + DestinationAPINames[DestinationIndex] + " ...");

        // Here we are setting the group presence then fetching it after we successfully set it
        GroupPresence.Set(options).OnComplete(message => {
            if (message.IsError)
            {
                UpdateConsole(message.GetError().Message);
            }
            else
            {
                // Note that Users.GetLoggedInUser() does not do a server fetch and will
                // not get an updated presence status
                Users.Get(LoggedInUserID).OnComplete(message2 =>
                {
                    if (message2.IsError)
                    {
                        UpdateConsole("Success! But presence is unknown!");
                    }
                    else
                    {
                        UpdateConsole("Group Presence set to:\n" + message2.Data.Presence + "\n" + message2.Data.PresenceDeeplinkMessage + "\n" + message2.Data.PresenceDestinationApiName);
                    }
                });
            }
        });
    }
    // User has interacted with a deeplink outside this app
    void OnJoinIntentChangeNotif(Message <Oculus.Platform.Models.GroupPresenceJoinIntent> message)
    {
        if (message.IsError)
        {
            UpdateConsole(message.GetError().Message);
        }
        else
        {
            var joinIntent = message.Data;

            // The deeplink message, this should give enough info on how to go the
            // destination in the app.
            var deeplinkMessage = joinIntent.DeeplinkMessage;

            // The API Name of the destination. You can set the user to this after
            // navigating to the app
            var destinationApiName = joinIntent.DestinationApiName;
            var matchSessionID     = joinIntent.MatchSessionId;
            var lobbySessionID     = joinIntent.LobbySessionId;

            var detailsString = "-Deeplink Message:\n" + deeplinkMessage + "\n-Api Name:\n" + destinationApiName + "\n-Lobby Session Id:\n" + lobbySessionID + "\n-Match Session Id:\n" + matchSessionID;
            detailsString += "\n";
            UpdateConsole("Got updated Join Intent & setting the user's presence:\n" + detailsString);

            var options = new GroupPresenceOptions();

            if (!string.IsNullOrEmpty(destinationApiName))
            {
                options.SetDestinationApiName(destinationApiName);
            }

            if (!string.IsNullOrEmpty(matchSessionID))
            {
                options.SetMatchSessionId(matchSessionID);
            }

            if (!string.IsNullOrEmpty(lobbySessionID))
            {
                options.SetLobbySessionId(lobbySessionID);
            }
            GroupPresence.Set(options);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        UpdateConsole("Init Oculus Platform SDK...");
        Core.AsyncInitialize().OnComplete(message => {
            if (message.IsError)
            {
                // Init failed, nothing will work
                UpdateConsole(message.GetError().Message);
            }
            else
            {
                UpdateConsole("Init complete!");

                /**
                 * Get and cache the Logged in User ID for future queries
                 */
                Users.GetLoggedInUser().OnComplete(OnLoggedInUser);

                /**
                 * Get the list of destinations defined for this app from the developer portal
                 */
                RichPresence.GetDestinations().OnComplete(OnGetDestinations);

                /**
                 * Listen for future join intents that might come in
                 */
                GroupPresence.SetJoinIntentReceivedNotificationCallback(OnJoinIntentChangeNotif);

                /**
                 * Listen for future leave that might come in
                 */
                GroupPresence.SetLeaveIntentReceivedNotificationCallback(OnLeaveIntentChangeNotif);

                /**
                 * Listen for the list of users that the current users have invitted
                 */
                GroupPresence.SetInvitationsSentNotificationCallback(OnInviteSentNotif);
            }
        });
    }
    // User has interacted with the roster to leave the current destination / lobby / match
    void OnLeaveIntentChangeNotif(Message <Oculus.Platform.Models.GroupPresenceLeaveIntent> message)
    {
        if (message.IsError)
        {
            UpdateConsole(message.GetError().Message);
        }
        else
        {
            var leaveIntent = message.Data;

            var destinationApiName = leaveIntent.DestinationApiName;
            MatchSessionID = leaveIntent.MatchSessionId;
            LobbySessionID = leaveIntent.LobbySessionId;

            var detailsString = "-Api Name:\n" + destinationApiName + "\n-Lobby Session Id:\n" + LobbySessionID + "\n-Match Session Id:\n" + MatchSessionID;
            detailsString += "\n";
            UpdateConsole("Clearing presence because user wishes to leave:\n" + detailsString);

            // User left
            GroupPresence.Clear();
        }
    }