using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

using CommonHelpers;

using MyFlat.DB;

namespace MyFlat {

//**************************************************************************************************
[Route("api")]
public partial class ApiController: GeneralApiResponseController {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public ApiController() =>
   LocaleHlp.UpdateCulture();

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

// GET api/roman_numbers?count=...&withDelay=...
[HttpGet("roman_numbers")]
public Task<dynamic> GetRomanNumbers_A( [FromQuery] int count,
                                        [FromQuery] bool withDelay ) =>
DoDynamic_A(null,async()=>{ // GeneralApiResponse<RomanNumber_Item> (normal response type)
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/*
   Win32.MessageBox( IntPtr.Zero,
      $"GET api/roman_numbers?count={count}",
      "MyFlat (ApiController)",
      Win32.MB_ICONINFORMATION | Win32.MB_SYSTEMMODAL );
*/
   var app = new App( this );
   var sd = app.IsUserSessionOpen ? app.SessionData : null;
   if ( sd != null )
      CON.PrintExpressive( "{0}: {1}", sd.UserName, sd.UserInitials );
   else
      if ( !HOps.IsBetween( count, 0, 49 ) )
      {
         CON.PrintError(HLocal.TEXT(
            "Numbers selection >= 50 (count) {0} is only for logged user.",
            "Выборка чисел >= 50 (кол-во) {0} только для вошедшего пользователя." ),
            CON.LongDash );
         return new UnauthorizedResult(); // HTTP#401 (Unauthorized)
         // response another than GeneralApiResponse<RomanNumber_Item>
         // (is possible due to "free" dynamic wrapping of controller action)
      }
   if ( withDelay )
   {
      const int delayMS = 3000;
      CON.PrintOperation(HLocal.TEXT(
         "Delay in request: {0} (ms)",
         "Задержка в запросе: {0} (мс)" ),
         delayMS );
      await Task.Delay( delayMS );
   }
   var list = await GetRomanNumbers_Query.PERFORM_A( count );
   // In case of exception, error message will be sent to client
   // (packed in GeneralApiResponse<TItem>).
   // Database error details will only be shown in “Expose” (development environment).
   CON.PrintSuccess(HLocal.TEXT(
      "{0} Roman number(s) was(were) selected from DB for response to client.",
      "{0} римских числа(ел) было(и) выбрано(ы) из БД для возврата клиенту." ),
      list.Count );
// return GeneralApiResponse.GENERAL_FAILURE;
// return new GeneralApiResponse( "Roman number list is unavailable." );
   return new GeneralApiResponse<RomanNumber_Item>( list );
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
});

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // ApiController
//**************************************************************************************************

} // MyFlat
