void AMessageSubsystemTestActor::LoginCallback(int32 LocalUserNum, bool bWasSuccessful, const FUniqueNetId& UserId, const FString& Error)
{
	UE_LOG(LogTemp, Log, TEXT("%s"), (_identity->GetLoginStatus(0) == ELoginStatus::LoggedIn) ? TEXT("true") : TEXT("false"));

	TArray<TSharedRef<const FUniqueNetId>> ids;
	ids.Add(UserId.AsShared());

	FOnlineMessagePayload payload = FOnlineMessagePayload();
	FVariantData data = FVariantData();
	data.SetValue(true);
	payload.SetAttribute("test", data);

	_message->SendMessage(0, ids, TEXT("testMessage"), payload);
}
void FFriendsMessageManager::SendGameInviteRequest()
{
	TSharedPtr<FUniqueNetId> UserId = OnlineSubMcp->GetIdentityInterface()->GetUniquePlayerId(0);
	if ( GameInvitesToSend[ 0 ]->IsValid()  )
	{
		FOnlineMessagePayload InvitePayload;
		TArray<TSharedRef<FUniqueNetId> > Recipients;
		Recipients.Add( GameInvitesToSend[0] );
 		InvitePayload.SetAttribute( TEXT("SenderID"), FVariantData( DisplayName ) );
		InvitePayload.SetAttribute( TEXT("GameInvite"), FVariantData( UserId.Get()->ToString() ) );
		OnlineSubMcp->GetMessageInterface()->SendMessage( 0, Recipients, TEXT("GameInvite"), InvitePayload );
	}

	GameInvitesToSend.RemoveAt( 0 );
}
void FFriendsMessageManager::SendChatMessageRequest()
{
	TSharedPtr<FUniqueNetId> UserId = OnlineSubMcp->GetIdentityInterface()->GetUniquePlayerId(0);

	if ( ChatMessagesToSend.Num() > 0 )
	{
		FString DisplayMessage = DisplayName;
		DisplayMessage += TEXT ( " says:\n" );
		DisplayMessage += ChatMessagesToSend[0].Message.ToString();

		ChatMessages.Add( MakeShareable ( new FFriendsAndChatMessage( DisplayMessage ) ) );

		FOnlineMessagePayload TestPayload;
		TArray<TSharedRef<FUniqueNetId> > Recipients;
		Recipients.Add( ChatMessagesToSend[0].FriendID );
 		TestPayload.SetAttribute(TEXT("STRINGValue"), FVariantData( ChatMessagesToSend[0].Message.ToString() ) ); 
 		TestPayload.SetAttribute(TEXT("SenderID"), FVariantData( DisplayName ) ); 
		OnlineSubMcp->GetMessageInterface()->SendMessage(0, Recipients, TEXT("TestType"), TestPayload);
	}
	ChatMessagesToSend.RemoveAt( 0 );
}
void FFriendsMessageManager::OnReadMessageComplete(int32 LocalPlayer, bool bWasSuccessful, const FUniqueMessageId& MessageId, const FString& ErrorStr)
{
	if ( bWasSuccessful )
	{
		TSharedPtr<FOnlineMessage> TempMessage = OnlineSubMcp->GetMessageInterface()->GetMessage( 0, MessageId );
		FOnlineMessagePayload Payload = TempMessage->Payload;
		FVariantData TestValue;
		Payload.GetAttribute(TEXT("STRINGValue"), TestValue);
		FVariantData SenderName;
 		Payload.GetAttribute(TEXT("SenderID"), SenderName );

		FString DisplayMessage = SenderName.ToString();

		FVariantData GameInvite;
 		if ( Payload.GetAttribute(TEXT("GameInvite"), GameInvite ) )
		{
			TSharedPtr< FUniqueNetId > NetID = FFriendsAndChatManager::Get()->FindUserID( DisplayMessage);
			if ( NetID.IsValid() )
			{
				DisplayMessage += TEXT ( " invited you to play" );
				TSharedPtr< FFriendsAndChatMessage > NotificationMessage = MakeShareable( new FFriendsAndChatMessage( DisplayMessage, NetID.ToSharedRef() ) );
				NotificationMessage->SetMessageType( EFriendsRequestType::JoinGame );
				NotificationMessage->SetRequesterName( SenderName.ToString() );
				NotificationMessage->SetSelfHandle( true );

				// auto accept - for game launch
				if ( bCanJoinGame && GameLaunchMessageID != TEXT("") && GameLaunchMessageID == MessageId.ToString() )
				{
					NotificationMessage->SetAutoAccept();
				}
				else if ( !bCanJoinGame )
				{
					// Send message ID
					NotificationMessage->SetLaunchGameID( MessageId.ToString() );
				}

				FriendsListNotificationDelegate->Broadcast( NotificationMessage.ToSharedRef() );

				// Chache, and don't delete game invites if the app cannot directly join a game
				if ( bCanJoinGame == false && LatentMessage.IsEmpty() )
				{
					for ( int32 Index = 0; Index < MessagesToDelete.Num(); Index++ )
					{
						if ( *MessagesToDelete[Index] == MessageId )
						{
							LatentMessage = MessageId.ToString();
							MessagesToDelete.RemoveAt( Index );
							break;
						}
					}
				}
			}
		}
		else
		{
			DisplayMessage += TEXT ( " says:\n" );
			DisplayMessage += TestValue.ToString();
			TSharedPtr< FFriendsAndChatMessage > ChatMessage = MakeShareable( new FFriendsAndChatMessage( DisplayMessage ) );
			ChatMessage->SetSelfHandle( false );
			ChatMessages.Add( ChatMessage );
			OChatListUpdatedDelegate.Broadcast();
		}

		MessagesToRead.RemoveAt(0);
	}

	SetState( EFriendsMessageManagerState::Idle );
}