using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Rssdp.Infrastructure
{
	/// 
	/// Correclty implements the  interface and pattern for an object containing only managed resources, and adds a few common niceities not on the interface such as an  property.
	/// 
	public abstract class DisposableManagedObjectBase : IDisposable
	{
		#region Public Methods
		/// 
		/// Override this method and dispose any objects you own the lifetime of if disposing is true;
		/// 
		/// True if managed objects should be disposed, if false, only unmanaged resources should be released.
		protected abstract void Dispose(bool disposing);
		/// 
		/// Throws and  if the  property is true.
		/// 
		/// 
		/// Thrown if the  property is true.
		/// 
		protected virtual void ThrowIfDisposed()
		{
			if (this.IsDisposed) throw new ObjectDisposedException(this.GetType().FullName);
		}
		#endregion
		#region Public Properties
		/// 
		/// Sets or returns a boolean indicating whether or not this instance has been disposed.
		/// 
		/// 
		public bool IsDisposed
		{
			get;
			private set;
		}
        #endregion
        public string BuildMessage(string header, Dictionary values)
        {
            var builder = new StringBuilder();
            const string argFormat = "{0}: {1}\r\n";
            builder.AppendFormat("{0}\r\n", header);
            foreach (var pair in values)
            {
                builder.AppendFormat(argFormat, pair.Key, pair.Value);
            }
            builder.Append("\r\n");
            return builder.ToString();
        }
        
        #region IDisposable Members
        /// 
        /// Disposes this object instance and all internally managed resources.
        /// 
        /// 
        /// Sets the  property to true. Does not explicitly throw an exception if called multiple times, but makes no promises about behaviour of derived classes.
        /// 
        /// 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification="We do exactly as asked, but CA doesn't seem to like us also setting the IsDisposed property. Too bad, it's a good idea and shouldn't cause an exception or anything likely to interfer with the dispose process.")]
		public void Dispose()
		{
            IsDisposed = true;
            Dispose(true);
        }
        #endregion
    }
}