﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StillOneScriptCore.EntityModel.CustomFunctionModel
{
    public class ExecutedCustomFunction : StillOneScriptCore.EntityModel.CustomFunctionModel.Support.BaseExecutedElement
    {
        public ExecutedCustomFunction(StillOneScriptCore.EntityModel.CustomFunctionModel.Support.ExecutedFunctionContext context)
            : base(context)
        {
        }

        private StillOneScriptCore.EntityModel.CustomFunctionModel.Statements.Executed.BaseStatement mStatement = null;
 
        public StillOneScriptCore.EntityModel.CustomFunctionModel.Statements.Executed.BaseStatement Statement
        {
            get
            {
                lock(LockObj)
                {
                    return mStatement;
                }
            }

            set
            {
                lock (LockObj)
                {
                    if (mStatement == value)
                    {
                        return;
                    }

                    mStatement = value;
                }
            }
        }

        public StillOneScriptCore.EntityModel.LngObjectsModel._Object Call()
        {
            lock(LockObj)
            {
                var tmpRez = mStatement.Call();

                if(tmpRez.OpResultKind == Statements.Executed.ResultOfStatementKind.Return)
                {
                    return tmpRez.ReturnValue;
                }

                return this.Context.EngineEnvironment.ObjectsConstants.Undefined;
            }
        }
    }
}
