using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.AspNetCore.Components;

using CommonHelpers.WASM;
using AFXUI;
using BookRegistry.Client.DB;

namespace BookRegistry.Client {

//**************************************************************************************************
partial class UserAuthorizationService {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public async Task<ServiceInvocationResult<AuthorizeUser_Response>>
   ESC_AuthorizeUser_A( string loginName, string password )
{
   if ( Program.IsDebugConfiguration )
      SetOperationTextAuto();
   else
      SetOperationText( "Authorization" );
   var sir = await ServiceInvocation.ESC_Invoke_A<AuthorizeUser_Response>(
      AddressOf( nameof(ESC_AuthorizeUser_A) ),
      new AuthorizeUser_Args(){
         LoginName = loginName,
         Password = password } );
   if ( sir.Success )
   {
      var rsp = sir.ResponseNN;
      AccessToken = rsp.AccessToken;
      RefreshToken = rsp.RefreshToken;
      AssignAuthorizationHeader( AccessToken );
   }
   else
      ResetAuthorizationHeader();
   return sir;
}

//==================================================================================================

public Task<ServiceInvocationResult<RefreshTokens_Response>>
   ESC_RefreshTokens_General_A(bool generateBothTokens) =>
      DoRefreshTokens_A( generateBothTokens );

public Task<ServiceInvocationResult<RefreshTokens_Response>>
   ESC_RefreshTokens_Admin_A(bool generateBothTokens) =>
      DoRefreshTokens_A( generateBothTokens );

//==================================================================================================

/// <exception cref="ArgumentNullException" />
async Task<ServiceInvocationResult<RefreshTokens_Response>>
   DoRefreshTokens_A( bool generateBothTokens,
                      [HRequired][CallerMemberName] string? methodName = null )
{
   var sir = await ServiceInvocation.ESC_Invoke_A<RefreshTokens_Response>(
      AddressOf( HArgChecking.NN( methodName ) ),
      new RefreshTokens_Args(){ GenerateBothTokens = generateBothTokens } );
   if ( sir.Success )
   {
      var rsp = sir.ResponseNN;
      AccessToken = rsp.AccessToken;
      if ( generateBothTokens )
         if ( rsp.RefreshToken != null )
            RefreshToken = rsp.RefreshToken;
         else
            Debug.WriteLine( "The server has not returned refresh token." );
      AssignAuthorizationHeader( AccessToken );
   }
   else
      ResetAuthorizationHeader();
   return sir.HasResponse ?
      new ServiceInvocationResult<RefreshTokens_Response>( sir.ResponseNN ) :
      new ServiceInvocationResult<RefreshTokens_Response>( sir.ExceptionNN );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // UserAuthorizationService
//**************************************************************************************************

} // BookRegistry.Client
