using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;

namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
    public class ComponentLogic : IComponentLogic
    {
        private readonly ILogger _logger;
        private readonly IComponentStorage _componentStorage;

        public ComponentLogic(ILogger<ComponentLogic> logger, IComponentStorage componentStorage)
        {
            _logger = logger;
            _componentStorage = componentStorage;
        }

        public List<ComponentViewModel>? ReadList(ComponentSearchModel? model)
        {
            _logger.LogInformation("ReadList Component. Name:{Name}. Id:{ Id}", model?.Name, model?.Id);
            var list = model == null ? _componentStorage.GetFullList() :
                _componentStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList Component return null list");
                return null;
            }
            _logger.LogInformation("ReadList Component. Count:{Count}", list.Count);
            return list;
        }

        public ComponentViewModel? ReadElement(ComponentSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement Component. Name:{Name}. Id:{ Id}", model.Name, model.Id);
            var element = _componentStorage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement Component element not found");
                return null;
            }
            _logger.LogInformation("ReadElement Component find. Id:{Id}", element.Id);
            return element;
        }

        public bool Create(ComponentBindingModel model)
        {
            CheckModel(model);
            if (_componentStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert Component operation failed");
                return false;
            }
            return true;
        }

        public bool Update(ComponentBindingModel model)
        {
            CheckModel(model);
            if (_componentStorage.Update(model) == null)
            {
                _logger.LogWarning("Update Component operation failed");
                return false;
            }
            return true;
        }

        public bool Delete(ComponentBindingModel model)
        {
            CheckModel(model, false);
            _logger.LogInformation("Delete. Id:{Id}", model.Id);
            if (_componentStorage.Delete(model) == null)
            {
                _logger.LogWarning("Delete Component operation failed");
                return false;
            }
            return true;
        }
        private void CheckModel(ComponentBindingModel 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));
            }
            var element = _componentStorage.GetElement(new ComponentSearchModel
            {
                Name = model.Name
            });
            if (element != null)
            {
                throw new InvalidOperationException("Компонент с таким названием уже есть");
            }
        }
    }
}