using System;
using System.Collections.Generic;

using CommonHelpers;

using MyFlat.DB;

namespace MyFlat {

//**************************************************************************************************
/// <summary>
/// User session data
/// </summary>
class SessionData {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public Guid Id { get; private set; }
public Guid ControlId { get; private set; }

public string UserName => LoginName;
public Guid AccountId { get; private set; }
public long AccountCode { get; private set; }
public DateTime LastRequestTime { get { lock (_SyncRoot_) return _LastRequestTime_; }}

public string LoginName => AuthorizationInfo.LoginName;
public DateTime LoginTime => AuthorizationInfo.LoginTime;
public DateTime RegistrationDate => AuthorizationInfo.RegistrationDate;
public bool IsAdministrator => AuthorizationInfo.IsAdministrator;
public string UserInitials => AuthorizationInfo.UserInitials;

public AuthorizationInfo AuthorizationInfo { get; private set; }
public AccountInfo_Item AccountProps { get; private set; }

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

internal void SetLastRequestTime(DateTime value)
{
   lock (_SyncRoot_)
      _LastRequestTime_ = value;
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="KeyNotFoundException" />
public ICloneable? this[object key]
{
   get { lock (_SyncRoot_) return _Dictionary_[ key ]; }
   set { lock (_SyncRoot_) _Dictionary_[ key ] = value.NN(); }
}

/// <exception cref="ArgumentNullException" />
public SessionData( Guid accountId, long accountCode,
                    AccountInfo_Item accountProps,
                    AuthorizationInfo ai, Guid controlId )
{
   HArgChecking.VerifyNotNulls( ai, accountProps );
   Id = Guid.NewGuid();
   ControlId = controlId;
   AccountId = accountId;
   AccountCode = accountCode;
   AccountProps = accountProps.AccountInfo_Duplicate();
   AuthorizationInfo = ai.AuthorizationInfo_Duplicate();
   SetLastRequestTime( LoginTime );
}

/// <exception cref="ArgumentNullException" />
public ICloneable? GetValueOrDefault(object key)
{
   lock (_SyncRoot_)
      return _Dictionary_.GetValueOrDefault( key );
}

public bool TryGetValue(object key,out ICloneable? value)
{
   lock (_SyncRoot_)
   {
      ICloneable? val;
      bool rslt = _Dictionary_.TryGetValue( key, out val );
      value = rslt ? (ICloneable)val.NN().Clone() : null;
      return rslt;
   }
}

public void ClearDictionary()
{
   lock (_SyncRoot_) _Dictionary_.Clear();
}

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

readonly object _SyncRoot_ = new object();
readonly Dictionary<object,ICloneable>
   _Dictionary_ = new Dictionary<object,ICloneable>();

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

DateTime _LastRequestTime_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // SessionData
//**************************************************************************************************

} // MyFlat
