using System;
using System.ComponentModel;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;
using Unity;
using Gebhardt.StoreWare.WcsWms.InterfaceWcsWms.Interfaces;
using Gebhardt.StoreWare.WcsWms.InterfaceWcsWms.Services;
using Gebhardt.StoreWare.Wcs.HostBooking.InterfaceWcsWms.Interfaces;
using Gebhardt.Shared.Process;
using Gebhardt.Shared.Process.ProducerConsumer;
using Gebhardt.StoreWare.WcsWms.InterfaceWcsWms.Interfaces;
using Gebhardt.StoreWare.WcsWms.InterfaceWcsWms.Models;
using Gebhardt.StoreWare.WcsWms.InterfaceWcsWms.EntityFramework;
using Gebhardt.StoreWare.WcsWms.InterfaceWcsWms.EntityFramework.Models;
using Gebhardt.StoreWare.WcsWms.InterfaceWcsWms.Mapping;
using Gebhardt.StoreWare.WcsWms.InterfaceWcsWms.Helpers;

using Gebhardt.Shared;
using Gebhardt.StoreWare.WcsWms.Constants;


namespace Gebhardt.StoreWare.Wcs.HostBooking.InterfaceWcsWms
{
    internal class FromWmsBookingConsumer : Consumer<IHostMessage>
    {
        #region Private Fields

        private readonly IUnityContainer _unityContainer;
        private readonly IHostMessageFromWmsService _serviceFromWms;

        #endregion Private Fields

        #region Public Constructors

        public FromWmsBookingConsumer(string name, int aliveTime, int queueLength, IUnityContainer unityContainer)
            : base(name, aliveTime, true, queueLength)
        {
            _unityContainer = unityContainer;
            _serviceFromWms = _unityContainer.Resolve<IHostMessageFromWmsService>();
        }

        #endregion Public Constructors

        #region Public Methods

        public override bool DoWork(IHostMessage hostMessage)
        {

            try
            {
                IHandleRecord<IHostMessage> handler = null;
                try
                {
                    //handler = _unityContainer.Resolve<IHandleRecord>(hostMessage.RecordType);
                    handler = (IHandleRecord<IHostMessage>)_unityContainer.Resolve(hostMessage.GetType());  //TODO test
                }
                catch (Exception e)
                {
                    Log.Write(LogLevel.Error, $"IHandleRecord on UnityContainer in  RegisterFromWmsServices from MessageInitializer.cs not registired. '{e.Message}'");
                }
                if (handler == null)
                {
                    hostMessage.Handle();
                }
                else
                {
                    handler.Handle(hostMessage);
                    hostMessage.SetFinished();
                }
            }
            catch (Exception e)
            {
                //Log.WriteException(e);
                string exceptionMessage = e.InnerException?.InnerException?.Message ?? e.InnerException?.Message ?? e.Message;
                Log.Write(LogLevel.Error, $"Nachricht {hostMessage.RecordType} kann nicht verbucht werden: {exceptionMessage}");
                hostMessage.SetFailed(e.Message);
            }
            finally
            {
                _serviceFromWms.Update(hostMessage);
            }
            return true;
        }

        #endregion Public Methods
    }
}