using Quadarax.Foundation.Core.Data;

namespace Quadarax.Foundation.Core.Business.Processor.Task.ExecuteProvider
{
    public class DummyExecuteProvider : TaskExecuteProvider
    {
        #region *** Constants ***
        public const string EP_DMY_DURATION = "duration";
        public const string EP_DMY_RESULT = "result";
        public const string EP_DMY_ERROR = "is-error-emit";
        #endregion  

        #region *** Properties ***
        private TimeSpan Duration { get; set; }
        private ResultType Result { get; set; }
        private bool IsErrorEmit { get; set; }
        #endregion

        #region *** Overrides ***
        protected override void OnExecute(ITaskIdentifier taskIdentifier, ITaskExecuteResult result)
        {
            if (IsErrorEmit) throw new Exception("Dummy exception emitted.");


            switch (Result)
            {
                case ResultType.Random:
                    ((Result)result).SetSuccess(new Random().Next(0, 2) == 1);
                    break;
                case ResultType.Success:
                    ((Result)result).SetSuccess(true);
                    break;
                case ResultType.Fail:
                    ((Result)result).SetSuccess(false);
                    break;
            }
        }

        protected override void OnValidateArguments(ITypedArgument[] arguments)
        {
            Duration = GetArgument<TimeSpan>(arguments, EP_DMY_DURATION);
            Result = GetArgument<ResultType>(arguments, EP_DMY_RESULT);
            IsErrorEmit = GetArgument<bool>(arguments, EP_DMY_ERROR);
        }
        #endregion

        #region *** Public Operations ***
        public static ITypedArgument[] CreateDefaultArguments()
        {
            var result = new List<ITypedArgument>();
            result.Add(new TypedArgument(EP_DMY_DURATION, typeof(TimeSpan), false));
            result.Add(new TypedArgument(EP_DMY_RESULT, typeof(ResultType), false));
            result.Add(new TypedArgument(EP_DMY_ERROR, typeof(bool), false));
            return result.ToArray();
        }
        #endregion


        #region *** Nested Classes ***

        private enum ResultType
        {
            Random,
            Success,
            Fail
        }
        #endregion
    }
}
