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

namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
    public class BuildLogic : IBuildLogic
    {
        private readonly ILogger _logger;
        private readonly IBuildStorage _buildStorage;

        public BuildLogic(ILogger<BuildLogic> logger, IBuildStorage buildStorage)
        {
            _logger = logger;
            _buildStorage = buildStorage;
        }

        public List<BuildViewModel>? ReadList(BuildSearchModel? model)
        {
            _logger.LogInformation("ReadList Build. Name:{Name}. Id:{ Id}", model?.Name, model?.Id);
            var list = model == null ? _buildStorage.GetFullList() :
                _buildStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList Build return null list");
                return null;
            }
            _logger.LogInformation("ReadList Build. Count:{Count}", list.Count);
            return list;
        }

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

        public bool Create(BuildBindingModel model)
        {
            CheckModel(model);
            if (_buildStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert Build operation failed");
                return false;
            }
            return true;
        }

        public bool Update(BuildBindingModel model)
        {
            CheckModel(model);
            if (_buildStorage.Update(model) == null)
            {
                _logger.LogWarning("Update Build operation failed");
                return false;
            }
            return true;
        }

        public bool Delete(BuildBindingModel model)
        {
            CheckModel(model, false);
            _logger.LogInformation("Delete. Id:{Id}", model.Id);
            if (_buildStorage.Delete(model) == null)
            {
                _logger.LogWarning("Delete Build operation failed");
                return false;
            }
            return true;
        }
        private void CheckModel(BuildBindingModel 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.Price <= 0)
            {
                throw new ArgumentNullException("Цена сборки должна быть больше 0", nameof(model.Price));
            }
            var element = _buildStorage.GetElement(new BuildSearchModel
            {
                Name = model.Name
            });
            if (element != null)
            {
                throw new InvalidOperationException("Сборка с таким названием уже есть");
            }
        }
    }
}