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

namespace BusinessLogic.BusinessLogic
{
	public class WorkshopLogic : IWorkshopLogic
	{
		private readonly ILogger _logger;
		private readonly IWorkshopStorage _machineStorage;
		public WorkshopLogic(ILogger<WorkshopLogic> logger, IWorkshopStorage machineStorage)
		{
			_logger = logger;
			_machineStorage = machineStorage;
		}
		public List<WorkshopViewModel>? ReadList(WorkshopSearchModel? model)
		{
			_logger.LogInformation("ReadList. WorkshopTitle:{Title}. Id:{Id}. UserId:{UserId}", model?.Title, model?.Id, model?.UserId);
			var list = model == null ? _machineStorage.GetFullList() : _machineStorage.GetFilteredList(model);
			if (list == null)
			{
				_logger.LogWarning("ReadList return null list");
				return null;
			}
			_logger.LogWarning("ReadList. Count:{Count}", list.Count);
			return list;
		}
		public WorkshopViewModel? ReadElement(WorkshopSearchModel? model)
		{
			if (model == null)
			{
				throw new ArgumentNullException(nameof(model));
			}
			_logger.LogInformation("ReadElement. WorkshopTitle:{Title}. Id:{Id}", model.Title, model.Id);
			var elem = _machineStorage.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(WorkshopBindingModel? model, bool withParams = true)
		{
			if (model == null)
			{
				throw new ArgumentNullException(nameof(model));
			}
			if (!withParams)
			{
				return;
			}
			if (string.IsNullOrEmpty(model.Title))
			{
				throw new ArgumentNullException("Нет названия изделия", nameof(model.Title));
			}
			if (string.IsNullOrEmpty(model.Address))
			{
				throw new ArgumentNullException("Нет адреса цеха", nameof(model.Address));
			}
			if (string.IsNullOrEmpty(model.Director))
			{
				throw new ArgumentNullException("Нет директора цеха", nameof(model.Director));
			}
			_logger.LogInformation("Workshop. WorkshopTitle:{Title}. Address:{Address}. Director:{Director}. Id:{Id}", model.Title, model.Address, model.Director, model.Id);
			var elem = _machineStorage.GetElement(new WorkshopSearchModel
			{
				Title = model.Title
			});
			if (elem != null && elem.Id != model.Id)
			{
				throw new InvalidOperationException("Цех с таким названием уже существует");
			}
		}
		public bool Create(WorkshopBindingModel? model)
		{
			CheckModel(model);
			if (_machineStorage.Insert(model!) == null)
			{
				_logger.LogWarning("Insert error");
				return false;
			}
			return true;
		}
		public bool Update(WorkshopBindingModel? model)
		{
			CheckModel(model);
			if (_machineStorage.Update(model!) == null)
			{
				_logger.LogWarning("Update error");
				return false;
			}
			return true;
		}
		public bool Delete(WorkshopBindingModel? model)
		{
			CheckModel(model, false);
			_logger.LogInformation("Delete Id:{Id}", model!.Id);
			if (_machineStorage.Delete(model!) == null)
			{
				_logger.LogWarning("Delete error");
				return false;
			}
			return true;
		}
	}
}
