using System;
using System.Collections.Generic;
using Newtonsoft.Json;
#if WASM
   using CommonHelpers.WASM;
namespace BookRegistry.Client {
#else // !WASM:
   using System.Reflection; using Microsoft.AspNetCore.Mvc;
   using CommonHelpers;
namespace BookRegistry.Server {
#endif // !WASM

//**************************************************************************************************
/// <summary>
/// Common structure for API-response with not null items collection (generic), message and status
/// </summary>
public partial class GeneralApiResponse<TItem>: GeneralApiResponse {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// public bool Success { get; set; }
// public bool HasError => !Success;
// public string? Message { get; set; }
// [HOptionalValue] public Guid Status { get; set; }
// public static string UndefinedMessage_SuccessfulCompletion;
// public static string UndefinedErrorMessage;

[JsonIgnore]
public bool HasItems => _Items_ != null;

/// <exception cref="InvalidOperationException" />
[JsonIgnore]
public List<TItem> Items {
   get {
      if ( !Success )
         throw new InvalidOperationException(
            "\"Items\"-property is not accessible for operation, which has completed "+
            "with failure (\"GeneralApiResponse<TItem>\")." );
      return _Items_ ?? throw new InvalidOperationException(
         "\"ItemsNN\"-property is unavailable, because request has not returned items "+
         "(\"GeneralApiResponse<TItem>\")." );
   }
   protected set => _Items_ = value;
}

[JsonIgnore]
public List<TItem>? ItemsOrNull  => _Items_;

//==================================================================================================
#if WASM
public GeneralApiResponse() {}
#else // !WASM:
internal GeneralApiResponse() {}
#endif // !WASM
//--------------------------------------------------------------------------------------------------

/// <exception cref="ArgumentException" />
internal GeneralApiResponse(string errorMessage)
: base( errorMessage ) => CheckItems_Arguments();
/// <exception cref="ArgumentException" />
internal GeneralApiResponse([HRequired] Guid status)
: base( status ) => CheckItems_Arguments();
/// <exception cref="ArgumentException" />
internal GeneralApiResponse([HRelOpt] string? errorMessage,[HRelOpt] Guid status)
: base( errorMessage, status ) => CheckItems_Arguments();

//--------------------------------------------------------------------------------------------------

/// <exception cref="ArgumentException" />
internal GeneralApiResponse(bool success)
: base( success ) => CheckItems_Arguments();
/// <exception cref="ArgumentException" />
internal GeneralApiResponse(bool success,string? message)
: base( success, message ) => CheckItems_Arguments();
/// <exception cref="ArgumentException" />
internal GeneralApiResponse(bool success,[HOptional] Guid status)
: base( success, status ) => CheckItems_Arguments();
/// <exception cref="ArgumentException" />
internal GeneralApiResponse(bool success,string? message,[HOptional] Guid status)
: base( success, message, status ) => CheckItems_Arguments();

//--------------------------------------------------------------------------------------------------

/// <exception cref="ArgumentException" />
internal GeneralApiResponse(GeneralApiResponse simpleResponse)
: base( simpleResponse ) => CheckItems_Arguments();

//==================================================================================================
#if WASM
[JsonIgnore]
internal override bool HasExpectedItemsLack => Success && !HasItems;
#endif // WASM
//==================================================================================================

// <exception cref="ArgumentException" />
protected void CheckItems_Arguments()
{
   if ( Success && !HasItems )
      throw new ArgumentException(
         "Successful result requies not null items (\"GeneralApiResponse<TItem>\")." );
}

//==================================================================================================
// JSON-serializable fields:

[JsonProperty(PropertyName="response_Items",Order=Int32.MinValue+4)]
List<TItem>? _Items_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // GeneralApiResponse<TItem>
//**************************************************************************************************

} // BookRegistry.Client/Server
