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

namespace BusinessLogic.BusinessLogic
{
	public class ProductionLogic : IProductionLogic
	{
		private readonly ILogger _logger;
		private readonly IProductionStorage _productionStorage;
		
		public ProductionLogic(ILogger<ProductionLogic> logger, IProductionStorage productionStorage)
		{
			_logger = logger;
			_productionStorage = productionStorage;
		}
		public List<ProductionViewModel>? ReadList(ProductionSearchModel? model)
		{
			_logger.LogInformation("ReadList. ProductionName:{name}. Id:{Id}. UserId:{UserId}", model?.Name, model?.Id, model?.UserId);
			var list = model == null ? _productionStorage.GetFullList() : _productionStorage.GetFilteredList(model);
			if (list == null)
			{
				_logger.LogWarning("ReadList return null list");
				return null;
			}
			_logger.LogWarning("ReadList. Count:{Count}", list.Count);
			return list;
		}
		public ProductionViewModel? ReadElement(ProductionSearchModel? model)
		{
			if (model == null)
			{
				throw new ArgumentNullException(nameof(model));
			}
			_logger.LogInformation("ReadElement. ProductionName:{Name}. Id:{Id}", model.Name, model.Id);
			var elem = _productionStorage.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(ProductionBindingModel? 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 (model.Cost <= 0)
			{
				throw new ArgumentNullException("Цена производства должна быть больше 0", nameof(model.Cost));
			}
			_logger.LogInformation("Production. ProductionName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id);
			var elem = _productionStorage.GetElement(new ProductionSearchModel
			{
				Name = model.Name
			});
			if (elem != null && elem.Id != model.Id)
			{
				throw new InvalidOperationException("Деталь с таким названием уже существует");
			}
		}
		public bool Create(ProductionBindingModel? model)
		{
			CheckModel(model);
			if (_productionStorage.Insert(model!) == null)
			{
				_logger.LogWarning("Insert error");
				return false;
			}
			return true;
		}
		public bool Update(ProductionBindingModel? model)
		{
			CheckModel(model);
			if (_productionStorage.Update(model!) == null)
			{
				_logger.LogWarning("Update error");
				return false;
			}
			return true;
		}
		public bool Delete(ProductionBindingModel? model)
		{
			CheckModel(model, false);
			_logger.LogInformation("Delete Id:{Id}", model!.Id);
			if (_productionStorage.Delete(model!) == null)
			{
				_logger.LogWarning("Delete error");
				return false;
			}
			return true;
		}
	}
}
