using Contracts.BindingModels;
using Contracts.BusinessLogicsContracts;
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using Microsoft.Extensions.Logging;

namespace BusinessLogic.BusinessLogic
{
	public class WorkerLogic : IWorkerLogic
	{
		private readonly ILogger _logger;
		private readonly IWorkerStorage _workerStorage;
		public WorkerLogic(ILogger<WorkerLogic> logger, IWorkerStorage workerStorage)
		{
			_logger = logger;
			_workerStorage = workerStorage;
		}
		public List<WorkerViewModel>? ReadList(WorkerSearchModel? model)
		{
			_logger.LogInformation("ReadList. WorkerName:{Name}. Id:{Id}. UserId:{UserId}", model?.Name, model?.Id, model?.UserId);
			var list = model == null ? _workerStorage.GetFullList() : _workerStorage.GetFilteredList(model);
			if (list == null)
			{
				_logger.LogWarning("ReadList return null list");
				return null;
			}
			_logger.LogWarning("ReadList. Count:{Count}", list.Count);
			return list;
		}
		public WorkerViewModel? ReadElement(WorkerSearchModel? model)
		{
			if (model == null)
			{
				throw new ArgumentNullException(nameof(model));
			}
			_logger.LogInformation("ReadElement. WorkerName:{Name}. Id:{Id}", model.Name, model.Id);
			var elem = _workerStorage.GetElement(model);
			if (elem == null)
			{
				_logger.LogWarning("ReadElement element not found");
				return null;
			}
			_logger.LogInformation("ReadElement find. Id:{Id}", elem.Id);
			return elem;
		}
		private void CheckModel(WorkerBindingModel? model, bool withParams = true)
		{
			if (model == null)
			{
				throw new ArgumentNullException(nameof(model));
			}
			if (!withParams)
			{
				return;
			}
			if (string.IsNullOrEmpty(model.Name))
			{
				throw new ArgumentNullException("Нет названия изделия", nameof(model.Name));
			}
			if (string.IsNullOrEmpty(model.Specialization))
			{
				throw new ArgumentNullException("Нет специальности", nameof(model.Specialization));
			}
			_logger.LogInformation("Worker. WorkerName:{Name}. Country:{Country}. Id:{Id}", model.Name, model.Specialization, model.Id);
			var elem = _workerStorage.GetElement(new WorkerSearchModel
			{
				Name = model.Name
			});
			if (elem != null && elem.Id != model.Id)
			{
				throw new InvalidOperationException("Станок с таким названием уже существует");
			}
		}
		public bool Create(WorkerBindingModel? model)
		{
			CheckModel(model);
			if (_workerStorage.Insert(model!) == null)
			{
				_logger.LogWarning("Insert error");
				return false;
			}
			return true;
		}
		public bool Update(WorkerBindingModel? model)
		{
			CheckModel(model);
			if (_workerStorage.Update(model!) == null)
			{
				_logger.LogWarning("Update error");
				return false;
			}
			return true;
		}
		public bool Delete(WorkerBindingModel? model)
		{
			CheckModel(model, false);
			_logger.LogInformation("Delete Id:{Id}", model!.Id);
			if (_workerStorage.Delete(model!) == null)
			{
				_logger.LogWarning("Delete error");
				return false;
			}
			return true;
		}
	}
}
