using qdr.fnd.core.pqueue.Process;
using Quadarax.Foundation.Core.Data;
using Quadarax.Foundation.Core.Logging;

namespace qdr.fnd.core.pqueue.test.Process.Mock
{
    internal class PsProcessProviderBaseMock : PsProcessProviderBase
    {
        public const string CS_ARG_EMIT_EXC = "emmit-exception";

        public PsProcessProviderBaseMock(ProcessDoneCallback processDoneCallback, ProcessStdOutputCallback processStdOutputCallback, string itemId, TypedArgument[] arguments, ILogger logger) : base(processDoneCallback, processStdOutputCallback, itemId, arguments, logger)
        {
        }

        public new static TypedArgument[] CreateArguments()
        {
            var list = new List<TypedArgument>(PsProcessProviderBase.CreateArguments());
            list.Add(new TypedArgument(CS_ARG_EMIT_EXC, typeof(string), false));
            return list.ToArray();
        }

        protected override async Task OnProcess()
        {
            if (ContainsArgument(CS_ARG_EMIT_EXC) && !string.IsNullOrEmpty(GetArgumentValue<string>(CS_ARG_EMIT_EXC)))
            {
                var message = GetArgumentValue<string>(CS_ARG_EMIT_EXC);
                Log.Log(LogSeverityEnum.Info, $"Emmiting exception '{message}'");
                //await Task.FromException(new Exception(message));
                throw new Exception(message);
               
            }
            else
            {
                Log.Log(LogSeverityEnum.Info, $"Nothing to emitte");
            }
            await base.OnProcess();
            Task.Delay(3000).Wait();
        }

        public void TestOnProcessError(Exception exception)
        {
            var task = base.OnProcessError(exception);
            task.Wait();
        }

        public TValue? TestGetArgumentValue<TValue>(string name)
        {
            return base.GetArgumentValue<TValue>(name);
        }

        public bool TestContainsArgument(string name)
        {
            return base.ContainsArgument(name);
        }
    }
}
