/*
 * Date        Sign  TaskId  Description
 * ----------  ----  ------- -----------
 * 2006-07-31  RMa           Added Debug ToString methods.
 * 2006-03-02  RMa           Supply DeviceConnectionState in ConnectionChangedEventArgs instead of bool connected.
 * 2006-02-13  MLa           This header added
 */

using System;

namespace Wayne.Lib
{
    /// <summary>
    /// Event arguments for a connection changed event from a Wayne Device.
    /// </summary>
    public sealed class ConnectionChangedEventArgs : EventArgs
    {
        #region Fields

        private DeviceConnectionState connectionState;

        #endregion

        #region Construction

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connectionState">The new state of the connection</param>
        public ConnectionChangedEventArgs(DeviceConnectionState connectionState)
        {
            this.connectionState = connectionState;
        }

        #endregion

        #region Properties

        /// <summary>
        /// True if the device was connected.
        /// </summary>
        public DeviceConnectionState ConnectionState
        {
            get { return connectionState; }
        }

        #endregion

        #region Debug methods

        /// <summary>
        /// Presents the class as a string.
        /// </summary>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "format")]
        public string ToString(string format, IFormatProvider provider)
        {
            return string.Format(provider, "ConnectionChange ({0})", connectionState);
        }

        /// <summary>
        /// Presents the class as a string using the specified culture-specific format information.
        /// </summary>
        /// <returns></returns>
        public string ToString(IFormatProvider provider)
        {
            return ToString("", provider);
        }

        /// <summary>
        /// Presents the class as a string using a format string.
        /// </summary>
        /// <returns></returns>
        public string ToString(string format)
        {
            return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Presents the class as a string using a format string and the specified culture-specific format information.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return ToString("", System.Globalization.CultureInfo.InvariantCulture);
        }

        #endregion
    }

    /// <summary>
    /// The state of the connection to a device.
    /// </summary>
    public enum DeviceConnectionState
    {
        #region Fields

        /// <summary>
        /// Unknown state of the connection.
        /// </summary>
        Unknown,

        /// <summary>
        /// Device is not connected.
        /// </summary>
        Disconnected,

        /// <summary>
        /// Trying to connect to device.
        /// </summary>
        Connecting,

        /// <summary>
        /// Connected to device.
        /// </summary>
        Connected,

        /// <summary>
        /// Disconnecting from device
        /// </summary>
        Disconnecting

        #endregion
    }

    /// <summary>
    /// The IConnectable defines a standard set of methods and properties for 
    /// classes that represents devices that theoretically can have a connected/disconnected state.
    /// </summary>
    public interface IConnectable
    {
        #region Properties

        /// <summary>
        /// Indicates the state of the connection.
        /// </summary>
        DeviceConnectionState ConnectionState { get; }

        #endregion

        #region Methods

        /// <summary>
        /// Connect to the device.
        /// </summary>
        /// <param name="connectionString">A string that contains the connection parameters. 
        /// The format is provided by the implementation.</param>
        void Connect(string connectionString);

        /// <summary>
        /// Disconnects from the device.
        /// </summary>
        void Disconnect();

        #endregion

        #region Events

        /// <summary>
        /// An event that is fired when the ConnectionState changes for to device.        
        /// </summary>
        event EventHandler<ConnectionChangedEventArgs> OnConnectionStateChange;

        #endregion
    }
}