using System;
using System.Text;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;

using CommonHelpers.WASM;
using AFXUI;

namespace BookRegistry.Client.Shared {

//**************************************************************************************************
/// <summary>
/// Error-list element (application components lib.)
/// </summary>
public partial class AErrorList: AComponent {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public ElementReference? HRefElement { get; protected set; }
//==================================================================================================

public static string DefaultPointerSign = "\u2666"; // Black Diamond Suit

[Parameter] public string? ContainerId { get; set; }
[Parameter] public string? ContainerClasses { get; set; }
[Parameter] public string? ContainerStyle { get; set; }
[Parameter] public string? TableClasses { get; set; }
[Parameter] public string? TableStyle { get; set; }
[Parameter] public string? PointerTDClasses { get; set; }
[Parameter] public string? PointerTDStyle { get; set; }
[Parameter] public string? PointerSpanClasses { get; set; }
[Parameter] public string? PointerSpanStyle { get; set; }
[Parameter] public string? MessageTDClasses { get; set; }
[Parameter] public string? MessageTDStyle { get; set; }
[Parameter] public string? MessageDivClasses { get; set; }
[Parameter] public string? MessageDivStyle { get; set; }
[Parameter] public string? OverrideTableClasses { get; set; }
[Parameter] public string? OverridePointerTDClasses { get; set; }
[Parameter] public string? OverridePointerSpanClasses { get; set; }
[Parameter] public string? OverrideMessageTDClasses { get; set; }
[Parameter] public string? OverrideMessageDivClasses { get; set; }
[Parameter] public string? OverrideSpanClasses { get; set; }
[Parameter] public string? PointerSign { get; set; } = DefaultPointerSign;

[Parameter(CaptureUnmatchedValues=true)]
public Dictionary<string,object>? Attributes { get; set; }

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

public string? TableBaseClasses => OverrideTableClasses ?? "error_list_TABLE";
public string? PointerTDBaseClasses => OverridePointerTDClasses ?? "error_list_PointerTD";
public string? PointerSpanBaseClasses => OverridePointerSpanClasses ?? "error_list_PointerSPAN";
public string? MessageTDBaseClasses => OverrideMessageTDClasses ?? "error_list_MessageTD";
public string? MessageDivBaseClasses => OverrideMessageDivClasses ?? "error_list_MessageDIV";

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

public bool HasErrors => ErrorCount > 0;
public int ErrorCount => ErrorMessages.Count;

public IReadOnlyList<string> ErrorMessages
{
   get => _ErrorMessages_;
   set { _ErrorMessages_ = value; StateHasChanged(); }
}

public string? FirstErrorMessage =>
   HasErrors ? ErrorMessages[0] : null;

/// <exception cref="InvalidOperationException" />
[HStrictResult]
public string AggregatedMessageNN =>
   GetAggregatedMessage() ?? throw new InvalidOperationException(
      "Error list does not have expected messages (\"AErrorList\")." );

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

public string? GetAggregatedMessage() =>
   GetAggregatedMessage( false );
public string? GetAggregatedMessage(bool separateItems)
{
   if ( !HasErrors )
      return null;
   var sb = new StringBuilder();
   foreach (var message in ErrorMessages)
      if ( !string.IsNullOrWhiteSpace( message ) )
      {
         if ( sb.Length > 0 )
         {
            sb.AppendLine();
            if ( separateItems )
               sb.AppendLine();
         }
         sb.Append( message.TrimEnd() );
      }
   return sb.ToString();
}

public void Clear() =>
   ErrorMessages = new string[0];

public bool SetErrorMessages(IReadOnlyList<string>? errorMessages)
{
   ErrorMessages = errorMessages!=null ? errorMessages : new string[0];
   return HasErrors;
}
public bool SetErrorMessagesFrom(IEnumerable<string?>? errorMessages)
{
   if ( errorMessages == null )
   {
      Clear();
      return false;
   }
   var list = new List<string>();
   foreach (var message in errorMessages)
      if ( !string.IsNullOrWhiteSpace( message ) )
         list.Add( message.TrimEnd() );
   ErrorMessages = list.ToArray();
   return HasErrors;
}

public bool SetError(string? errorMessage)
{
   if ( errorMessage != null )
      SetSingleMessage( errorMessage );
   else
      Clear();
   return HasErrors;
}
public string SetSingleMessage(string errorMessage)
{
   ErrorMessages = new []{ errorMessage };
   return errorMessage;
}

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

IReadOnlyList<string> _ErrorMessages_ = new string[0];

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // AErrorList
//**************************************************************************************************

} // BookRegistry.Client.Shared
