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

namespace BusinessLogic.BusinessLogic
{
	public class ProductLogic : IProductLogic
	{
		private readonly ILogger _logger;
		private readonly IProductStorage _productStorage;
		public ProductLogic(ILogger<ProductLogic> logger, IProductStorage productStorage)
		{
			_logger = logger;
			_productStorage = productStorage;
		}
		public List<ProductViewModel>? ReadList(ProductSearchModel? model)
		{
			_logger.LogInformation("ReadList. ProductName:{name}. Id:{Id}. UserId:{UserId}", model?.Name, model?.Id, model?.UserId);
			var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model);
			if (list == null)
			{
				_logger.LogWarning("ReadList return null list");
				return null;
			}
			_logger.LogWarning("ReadList. Count:{Count}", list.Count);
			return list;
		}
		public ProductViewModel? ReadElement(ProductSearchModel? model)
		{
			if (model == null)
			{
				throw new ArgumentNullException(nameof(model));
			}
			_logger.LogInformation("ReadElement. ProductName:{Name}. Id:{Id}", model.Name, model.Id);
			var elem = _productStorage.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(ProductBindingModel? 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));
			}
			if (model.ProductDetails.Count == 0)
			{
				throw new ArgumentNullException("Количество деталей в изделии должно быть больше 0", nameof(model.ProductDetails));
			}
			_logger.LogInformation("Product. ProductName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id);
			var elem = _productStorage.GetElement(new ProductSearchModel
			{
				Name = model.Name
			});
			if (elem != null && elem.Id != model.Id)
			{
				throw new InvalidOperationException("Изделие с таким названием уже существует");
			}
		}
		public bool Create(ProductBindingModel? model)
		{
			CheckModel(model);
			if (_productStorage.Insert(model!) == null)
			{
				_logger.LogWarning("Insert error");
				return false;
			}
			return true;
		}
		public bool Update(ProductBindingModel? model)
		{
			CheckModel(model);
			if (_productStorage.Update(model!) == null)
			{
				_logger.LogWarning("Update error");
				return false;
			}
			return true;
		}
		public bool Delete(ProductBindingModel? model)
		{
			CheckModel(model, false);
			_logger.LogInformation("Delete Id:{Id}", model!.Id);
			if (_productStorage.Delete(model!) == null)
			{
				_logger.LogWarning("Delete error");
				return false;
			}
			return true;
		}
	}
}
