#region  --------------- Copyright Dresser Wayne Pignone -------------
/*
 * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/Transition.cs $
 * 
 * 4     08-02-26 14:11 Mattias.larsson
 * Removed Obsolete method.
 */
#endregion
#region Old file header
/*===============================================================================
 * Transition
 * 
 * Change history
 * When         Who     Comment
 * ----------   ------  ------------------------------------
 * 2006-02-03   RMa     FXCop updates: BasicTransitionType renamed to BasicTransitionTypes.
 * 2006-01-27   RMa     Removed interface Event. Use Event class instead.
 * 2005-06-05   RMa     Added Basic transition type 'Error'.
 * 2005-12-09   RMa     Added static method GetTransitionName for internal use.
 * 2005-12-05   RMa     This header added.
 * 
---------------------------------------------------------------------------------*/

#endregion

using System;
namespace Wayne.Lib.StateEngine
{
    /// <summary>
    /// The transition class is used by state objects to signal that a change has occured.
    /// The transition is interpreted by the statemachine's State-transition lookup table. it 
    /// determines which the next state should be. 
    /// 
    /// The transition type identifies the transition and is supplied by the application. It is recommended
    /// that enums are used as transition types, but any type can be used.
    /// </summary>
    public class Transition
    {
        #region Fields
        private State sender;
        private StateEngineEvent sourceEvent;
        private object type;
        #endregion

        #region Properties
        /// <summary>
        /// Type of the transistion.
        /// </summary>
        public object Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }

        /// <summary>
        /// Represents the type name. It returns the Type.ToString(). This is
        /// used by the statemachine to find the transition in the lookup table.
        /// </summary>
        public string Name
        {
            get
            {
                return GetTransitionName(this.type);
            }
        }

        /// <summary>
        /// The state that issued the transition.
        /// </summary>
        public State Sender
        {
            get
            {
                return sender;
            }
        }

        /// <summary>
        /// If the transition is generated when a state has received an event, it can be
        /// supplied in this property.
        /// </summary>
        public StateEngineEvent SourceEvent
        {
            get
            {
                return sourceEvent;
            }
        }

        /// <summary>
        /// Internal writable source event. Assigned statemachine connects the event to the transition.
        /// </summary>
        internal StateEngineEvent WritableSourceEvent
        {
            get { return sourceEvent; }
            set { sourceEvent = value; }
        }
        #endregion

        #region Construction

        /// <summary>
        /// Transition constructor. assumes that the Source Event is null.
        /// </summary>
        /// <param name="sender">State that issued the transition</param>
        /// <param name="type">Type of the transition</param>
        public Transition(State sender, object type)
        {
            this.sender = sender;
            this.type = type;
        }

        #endregion

        #region Static Methods
        /// <summary>
        /// Returns the name of the transition to use when persisting configuration etc.
        /// </summary>
        /// <param name="transitionType"></param>
        /// <returns></returns>
        public static string GetTransitionName(object transitionType)
        {
            if (transitionType == null)
                return "-";
            else if (transitionType is Enum)
                return transitionType.GetType().FullName + "." + transitionType.ToString();
            else if (transitionType.GetType() == typeof(string))
                return transitionType.ToString();
            else
                return transitionType.GetType().FullName;
        }
        #endregion
    }
}