2024-02-13 02:59:40 +01:00
using CommunityToolkit.Mvvm.ComponentModel ;
using CommunityToolkit.Mvvm.Input ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Text.RegularExpressions ;
using System.Threading.Tasks ;
2024-02-14 02:07:22 +01:00
using VDownload.Services.Data.Configuration ;
using VDownload.Services.UI.Dialogs ;
using VDownload.Services.UI.StringResources ;
using VDownload.Services.UI.WebView ;
2024-02-13 02:59:40 +01:00
using VDownload.Sources.Twitch.Authentication ;
2024-02-14 02:07:22 +01:00
namespace VDownload.Core.ViewModels.Authentication
2024-02-13 02:59:40 +01:00
{
public partial class AuthenticationViewModel : ObservableObject
{
#region ENUMS
public enum AuthenticationButton
{
SignIn ,
SignOut ,
Loading
}
#endregion
#region SERVICES
2024-02-14 02:07:22 +01:00
protected readonly IDialogsService _dialogsService ;
protected readonly IWebViewService _webViewService ;
protected readonly IConfigurationService _configurationService ;
protected readonly IStringResourcesService _stringResourcesService ;
protected readonly ITwitchAuthenticationService _twitchAuthenticationService ;
2024-02-13 02:59:40 +01:00
#endregion
#region PROPERTIES
[ObservableProperty]
2024-02-14 02:07:22 +01:00
private AuthenticationButton _twitchButtonState = AuthenticationButton . Loading ;
2024-02-13 02:59:40 +01:00
[ObservableProperty]
2024-02-14 02:07:22 +01:00
private string _twitchDescription ;
2024-02-13 02:59:40 +01:00
#endregion
#region CONSTRUCTORS
2024-02-14 02:07:22 +01:00
public AuthenticationViewModel ( IDialogsService dialogsService , IWebViewService webViewService , IConfigurationService configurationService , IStringResourcesService stringResourcesService , ITwitchAuthenticationService twitchAuthenticationService )
2024-02-13 02:59:40 +01:00
{
2024-02-14 02:07:22 +01:00
_dialogsService = dialogsService ;
2024-02-13 02:59:40 +01:00
_webViewService = webViewService ;
2024-02-14 02:07:22 +01:00
_configurationService = configurationService ;
_stringResourcesService = stringResourcesService ;
2024-02-13 02:59:40 +01:00
_twitchAuthenticationService = twitchAuthenticationService ;
}
#endregion
#region PUBLIC METHODS
2024-02-14 02:07:22 +01:00
[RelayCommand]
public async Task Navigation ( )
{
List < Task > refreshTasks = new List < Task >
{
TwitchAuthenticationRefresh ( )
} ;
await Task . WhenAll ( refreshTasks ) ;
}
2024-02-13 02:59:40 +01:00
[RelayCommand]
public async Task TwitchAuthentication ( )
{
AuthenticationButton state = TwitchButtonState ;
TwitchButtonState = AuthenticationButton . Loading ;
if ( state = = AuthenticationButton . SignOut )
{
await _twitchAuthenticationService . DeleteToken ( ) ;
}
else
{
2024-02-14 02:07:22 +01:00
Sources . Twitch . Configuration . Models . Authentication auth = _configurationService . Twitch . Authentication ;
string authUrl = string . Format ( auth . Url , auth . ClientId , auth . RedirectUrl , auth . ResponseType , string . Join ( ' ' , auth . Scopes ) ) ;
string url = await _webViewService . Show ( new Uri ( authUrl ) , ( url ) = > url . StartsWith ( auth . RedirectUrl ) , _stringResourcesService . AuthenticationViewResources . Get ( "TwitchAuthenticationWindowTitle" ) ) ;
2024-02-13 02:59:40 +01:00
2024-02-14 02:07:22 +01:00
Regex regex = new Regex ( auth . RedirectUrlRegex ) ;
Match match = regex . Match ( url ) ;
2024-02-13 02:59:40 +01:00
if ( match . Success )
{
string token = match . Groups [ 1 ] . Value ;
await _twitchAuthenticationService . SetToken ( Encoding . UTF8 . GetBytes ( token ) ) ;
}
else
{
2024-02-14 02:07:22 +01:00
await _dialogsService . ShowOk ( _stringResourcesService . AuthenticationViewResources . Get ( "TwitchAuthenticationDialogTitle" ) , "An unknown error occured" ) ; // TODO : Change to string resource
2024-02-13 02:59:40 +01:00
}
}
await TwitchAuthenticationRefresh ( ) ;
}
#endregion
#region PRIVATE METHODS
private async Task TwitchAuthenticationRefresh ( )
{
TwitchButtonState = AuthenticationButton . Loading ;
byte [ ] ? token = await _twitchAuthenticationService . GetToken ( ) ;
if ( token is null )
{
2024-02-14 02:07:22 +01:00
TwitchDescription = _stringResourcesService . AuthenticationViewResources . Get ( "TwitchAuthenticationDescriptionNotAuthenticated" ) ;
2024-02-13 02:59:40 +01:00
TwitchButtonState = AuthenticationButton . SignIn ;
}
else
{
TwitchValidationResult validationResult = await _twitchAuthenticationService . ValidateToken ( token ) ;
if ( validationResult . Success )
{
2024-02-14 02:07:22 +01:00
TwitchDescription = string . Format ( _stringResourcesService . AuthenticationViewResources . Get ( "TwitchAuthenticationDescriptionAuthenticated" ) , validationResult . TokenData . Login , validationResult . TokenData . ExpirationDate ) ;
2024-02-13 02:59:40 +01:00
TwitchButtonState = AuthenticationButton . SignOut ;
}
else
{
await _twitchAuthenticationService . DeleteToken ( ) ;
2024-02-14 02:07:22 +01:00
TwitchDescription = _stringResourcesService . AuthenticationViewResources . Get ( "TwitchAuthenticationDescriptionAuthenticationInvalid" ) ;
2024-02-13 02:59:40 +01:00
TwitchButtonState = AuthenticationButton . SignIn ;
}
}
}
#endregion
}
}