using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

using CommonHelpers;
using BookRegistry.Server.DB;

namespace BookRegistry.Server {

//**************************************************************************************************
partial class UserAuthorizationController {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[HttpPost("[action]")][AllowAnonymous]
public Task<dynamic> AuthorizeUser( AuthorizeUser_Args args ) =>
Do_A(args,async()=>{ // AuthorizeUser_Response
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   string
      loginName = args.LoginName.Trim(),
      password = args.Password;
   if ( loginName == "" )
      return new GeneralApiResponse(HLocal.TEXT(
         "Login name was not specified.",
         "Имя входа не задано." ));
   var ua = await UserAuthorization.AuthorizeUser_A( loginName, password );
   var userInfo = ua.UserInfo;
   if ( userInfo==null || ua.ErrorMessage!=null )
      CON.PrintError( ua.ErrorMessage );
   if ( ua.TechMessage != null )
      CON.PrintWarning( ua.TechMessage );
   if ( userInfo == null )
      return ua.ErrorMessage != null ?
         new GeneralApiResponse( ua.ErrorMessage, ua.Status ) :
         GeneralApiResponse.GENERAL_FAILURE;
   var rsp = new AuthorizeUser_Response( userInfo );
   CON.PrintIntensive(
      "User \"{0}\"{1} was authorized at {2} (server time).",
      userInfo.LoginName,
      userInfo.IsAdministrator ? " (admin.)" : "",
      rsp.LoginUtc.ToLocalTime() );
   return rsp;
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
});

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // UserAuthorizationController
//**************************************************************************************************

} // BookRegistry.Server
