namespace Kit.Helpers.Log
{
    public class CallStackLogDB: CallStackLog, IDisposable
    {
        protected string _sessionKey;
        protected LogMessageType _type;
        protected string _target;
        protected string _action;
        protected DateTime _dateCreated;
        private bool disposed = false;
        public CallStackLogDB(string sessionKey, LogMessageType type, string target, string action):base()
        {
            _sessionKey = sessionKey;
            _type = type;
            _target = target;
            _action = action;
            _dateCreated = DateTime.UtcNow;
        }

        public void Dispose()
        {
            Dispose(true);
            // подавляем финализацию
            GC.SuppressFinalize(this);

            
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    this.Logs.TryAdd($"Full Time", Convert.ToInt64((DateTime.UtcNow - _dateCreated).TotalMilliseconds));

                    Kit.Helpers.Log.LoggerDB.Commit(this._sessionKey, this._type, this._target, this._action, _dateCreated, this.Logs.ToDictionary(x => x.Key, x => x.Value));
                }
                // освобождаем неуправляемые объекты
                disposed = true;
            }
        }
    }
}
