using qdr.fnd.core.pqueue.Storages;
using Quadarax.Foundation.Core.Data;
using Quadarax.Foundation.Core.Logging;
using System.Xml.Linq;

namespace qdr.fnd.core.pqueue.test.Mocks
{
    public class PsQueueItemMock : PsQueueItem
    {
        public PsQueueItemMock(string id) : base(id)
        {
        }
        public PsQueueItemMock(string id, string owner, string ownerRef, string providerClass, DateTime created, ITypedArgument[]? arguments, int initialAttempts = 0): base(id, owner, ownerRef, providerClass, created, arguments, initialAttempts)
        {
        }

        public void SetOwner(string owner, string ownerRef)
        {
            Owner = owner;
            OwnerRef = ownerRef;
        }

        public void ClearArguments()
        {
            Arguments = new ITypedArgument[0];
        }

        public void AddToStorage(IPsQueueStorage storage)
        {
            var itemId = storage.CreateItem(Owner, OwnerRef, ProcessProviderClass, AtteptsInitial, null);
            foreach (var argument in Arguments)
            {
                if (argument.ValueType.FullName == null) throw new InvalidOperationException($"Cannot obtain fullName of type '{argument.ValueType.Name}'");
                
                storage.CreateItemAttribute(itemId, argument.Name, argument.Value, argument.ValueType.FullName, argument.IsOutput);
            }
            storage.UpdateItemStatus(itemId, Status,IsStatusTransient);
            Id = itemId;
        }
        public void SetValidity(DateTime validFrom, DateTime? validTo)
        {
            ValidFrom = validFrom;
            ValidTo = validTo;
        }
    }
}
