/*
 * Vwait.cs --
 *
 * Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: $
 */

using System;
using Eagle._Attributes;
using Eagle._Components.Private;
using Eagle._Components.Public;
using Eagle._Containers.Public;
using Eagle._Interfaces.Public;

namespace Eagle._Commands
{
    [ObjectId("9a58d5a9-85b8-43e1-9136-5667eb2e87bf")]
    [CommandFlags(CommandFlags.Safe | CommandFlags.Standard)]
    [ObjectGroup("event")]
    internal sealed class Vwait : Core
    {
        public Vwait(
            ICommandData commandData
            )
            : base(commandData)
        {
            // do nothing.
        }

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count == 2)
                    {
                        if (interpreter.ShouldWaitVariable())
                        {
                            //
                            // TODO: Add options here to allow extra variable flags,
                            //       event limiting, etc.
                            //
                            VariableFlags variableFlags = VariableFlags.WaitVariableMask;

                            //
                            // HACK: The call to ThreadOps.IsStaThread here is made
                            //       under the assumption that no user-interface thread
                            //       can exist without also being an STA thread.  This
                            //       may eventually prove to be false; however,
                            //       currently both WinForms and WPF require this (i.e.
                            //       an STA thread).
                            //
                            code = interpreter.WaitVariable(
                                variableFlags, arguments[1], 0, null, false, false,
                                false, ThreadOps.IsStaThread(), ref result);

                            if (code == ReturnCode.Ok)
                                Engine.ResetResult(interpreter, ref result);
                        }
                        else
                        {
                            result = String.Format(
                                "can't wait for variable \"{0}\": would wait forever",
                                arguments[1]);

                            code = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"vwait varName\"";
                        code = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code = ReturnCode.Error;
            }

            return code;
        }
        #endregion
    }
}
