using System;
using System.Collections.Generic;

using CommonHelpers;

namespace MyFlat {

//**************************************************************************************************
/// <summary>
/// Data, available from page models (contains: error collection, user session info)
/// </summary>
public class PageModelData: ICloneable {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public readonly bool IsUserSessionOpen;
[HOptionalValue]
public readonly string[]? ErrorMessages;
public bool HasErrors => ErrorMessages != null;
public int ErrorCount => HasErrors ? ErrorMessagesNN.Length : 0;

public readonly string? UserName;
public readonly DateTime LoginTime;
public readonly Guid UserSessionId;
public readonly DateTime RegistrationDate;
public readonly bool IsAdministrator;
public readonly string? UserInitials;

public string[] ErrorMessagesNN => ErrorMessages.NN();
public string UserNameNN => UserName.NN();
public string UserInitialsNN => UserInitials.NN();

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

/// <exception cref="ArgumentNullException" />
public PageModelData(HomeController controller)
{
   HArgChecking.VerifyNotNull( controller );
   var app = new App( controller );
   if ( IsUserSessionOpen = app.IsUserSessionOpen )
   {
      var sd = app.SessionData;
      UserName = app.UserName;
      LoginTime = app.LoginTime;
      UserSessionId = app.UserSessionId;
      RegistrationDate = sd.RegistrationDate;
      IsAdministrator = sd.IsAdministrator;
      UserInitials = sd.UserInitials;
   }
}

/// <exception cref="ArgumentNullException" />
public PageModelData(HomeController controller,List<string> errorMessages)
: this( controller )
{
   HArgChecking.VerifyNotNull( controller );
   HArgChecking.VerifyNotNulls( errorMessages );
   ErrorMessages = errorMessages.ToArray();
}
/// <exception cref="ArgumentNullException" />
public PageModelData(HomeController controller,string errorMessage)
: this( controller )
{
   HArgChecking.VerifyNotNulls( controller, errorMessage );
   ErrorMessages = new string[]{ errorMessage };
}

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

PageModelData()
{}

object ICloneable.Clone()
{
   var instance = (PageModelData)MemberwiseClone();
   return instance;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // PageModelData
//**************************************************************************************************

} // MyFlat
