#region  --------------- Copyright Dresser Wayne Pignone -------------
/*
 * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/RootStateMachine.cs $
 * 
 * 4     08-02-26 14:08 Mattias.larsson
 * Use the LogName-property to log a state.
 * 
 * 3     07-08-15 15:28 roger.månsson
 * Support for new debug log feature.
 * 
 * 2     07-03-12 14:59 roger.månsson
 * Updated for the changes with the new state machine hiearchy.
 */
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Wayne.Lib.Log;

namespace Wayne.Lib.StateEngine
{
    //[System.Diagnostics.DebuggerNonUserCode()]
    abstract class RootStateMachine : StateMachine
    {
        #region Fields

        object handlingEventSyncObject = new object();
        bool started;

        #endregion

        #region Construction

        internal RootStateMachine(string name, IDebugLogger debugLogger, object logCategory)
            : base(name, debugLogger, logCategory)
        {
        }

        #endregion

        #region Protected methods

        protected virtual void HandleEventInCurrentState(StateEngineEvent stateEngineEvent)
        {
            lock (handlingEventSyncObject)
            {
                Transition transition = null;

                CurrentState.IncomingEvent(stateEngineEvent, ref transition);

                if (transition != null)
                {
                    this.HandleTransition(ref transition);

                    //Unhandled transition
                    if (transition != null)
                    {
                        if ((debugLogger != null) && (debugLogger.IsActive(logCategory)))
                        {
                            debugLogger.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                "State {0} issued transition {1} that was not configured to be handled anywhere.",
                                transition.Sender.LogName, transition.Name), logCategory);
                        }
                    }
                }
            }
        }



        protected void PerformInitialTransition()
        {
            lock (handlingEventSyncObject)
            {
                Transition transition = null;

                started = true;

                this.EnterInitialState(null, ref transition);

                //Unhandled transition
                if (transition != null)
                {
                    if ((debugLogger != null) && (debugLogger.IsActive(logCategory)))
                    {
                        debugLogger.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                            "State [{0}] issued transition [{1}] that was not configured to be handled anywhere.",
                            transition.Sender.LogName, transition.Name), logCategory);
                    }
                }

            }
        }


        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            started = false;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Indicates that the state machine is started.
        /// </summary>
        public override bool Started
        {
            get { return started; }
        }

        #endregion
    }
}