using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicContracts.BindingModels;
using VetClinicContracts.BusinessLogicsContracts;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;

namespace VetClinicBusinessLogic.BusinessLogics
{
	public class VaccinationLogic : IVaccinationLogic
	{
		private readonly ILogger _logger;
		private readonly IVaccinationStorage _vaccinationStorage;

		public VaccinationLogic(ILogger<VaccinationLogic> logger, IVaccinationStorage vaccinationStorage)
		{
			_logger = logger;
			_vaccinationStorage = vaccinationStorage;
		}

		public VaccinationViewModel? ReadElement(VaccinationSearchModel model)
		{
			if (model == null)
			{
				throw new ArgumentNullException(nameof(model));
			}
			_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
			var element = _vaccinationStorage.GetElement(model);
			if (element == null)
			{
				_logger.LogWarning("ReadElement element not found");
				return null;
			}
			_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
			return element;
		}

		public List<VaccinationViewModel>? ReadList(VaccinationSearchModel? model)
		{
			_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
			var list = model == null ? _vaccinationStorage.GetFullList() :
		   _vaccinationStorage.GetFilteredList(model);
			if (list == null)
			{
				_logger.LogWarning("ReadList return null list");
				return null;
			}
			
			return list;
		}

		public bool Create(VaccinationBindingModel model)
		{
			CheckModel(model);
			if (_vaccinationStorage.Insert(model) == null)
			{
				_logger.LogWarning("Insert operation failed");
				return false;
			}
			return true;
		}
		public bool Update(VaccinationBindingModel model)
		{
			CheckModel(model);
			if (_vaccinationStorage.Update(model) == null)
			{
				_logger.LogWarning("Update operation failed");
				return false;
			}
			return true;
		}
		public bool Delete(VaccinationBindingModel model)
		{
			CheckModel(model, false);
			_logger.LogInformation("Delete. Id:{Id}", model.Id);
			if (_vaccinationStorage.Delete(model) == null)
			{
				_logger.LogWarning("Delete operation failed");
				return false;
			}
			return true;
		}
		private void CheckModel(VaccinationBindingModel model, bool withParams =
	   true)
		{
			if (model == null)
			{
				throw new ArgumentNullException(nameof(model));
			}
			if (!withParams)
			{
				return;
			}
			if (string.IsNullOrEmpty(model.NameVaccination))
			{
				throw new ArgumentNullException("Нет текста рекомендации",
			   nameof(model.NameVaccination));
			}
			if (model.CostVaccination <= 0)
			{
				throw new ArgumentNullException("Цена привики должна быть больше 0", nameof(model.CostVaccination));
			}

			_logger.LogInformation("Guidance. NameVaccination:{NameVaccination}. CostVaccination:{CostVaccination}", model.NameVaccination, model.CostVaccination);
		}
	}
}

