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 nullable items collection (generic), message and status
/// </summary>
public partial class GeneralApiResponseNI<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 (\"GeneralApiResponseNI<TItem>\")." );
      return _Items_;
   }
   protected set => _Items_ = value;
}

/// <exception cref="InvalidOperationException" />
[JsonIgnore][HStrictResult]
public List<TItem> ItemsNN =>
   Items ?? throw new InvalidOperationException(
      "\"ItemsNN\"-property is unavailable, because request has not returned items "+
      "(\"GeneralApiResponseNI<TItem>\")." );

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

internal GeneralApiResponseNI(string errorMessage)
: base( errorMessage )
{}
/// <exception cref="ArgumentException" />
internal GeneralApiResponseNI([HRequired] Guid status)
: base( status )
{}
/// <exception cref="ArgumentException" />
internal GeneralApiResponseNI([HRelOpt] string? errorMessage,[HRelOpt] Guid status)
: base( errorMessage, status )
{}

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

internal GeneralApiResponseNI(bool success)
: base( success )
{}
internal GeneralApiResponseNI(bool success,string? message)
: base( success, message )
{}
internal GeneralApiResponseNI(bool success,[HOptional] Guid status)
: base( success, status )
{}
internal GeneralApiResponseNI(bool success,string? message,[HOptional] Guid status)
: base( success, message, status )
{}

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

internal GeneralApiResponseNI(GeneralApiResponse simpleResponse)
: base( simpleResponse )
{}

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // GeneralApiResponseNI<TItem>
//**************************************************************************************************

} // BookRegistry.Client/Server
