using ComputersShopContracts.BindingModels;
using ComputersShopContracts.BisnessLogicsContracts;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using Microsoft.Extensions.Logging;

namespace ComputersShopBusinessLogic.BusinessLogics
{
    public class ComputerLogic : IComputerLogic
    {
        private readonly ILogger _logger;
        private readonly IComputerStorage _ComputerStorage;
        public ComputerLogic(ILogger<ComputerLogic> logger, IComputerStorage ComputerStorage)
        {
            _logger = logger;
            _ComputerStorage = ComputerStorage;
        }

        public List<ComputerViewModel>? ReadList(ComputerSearchModel? model)
        {
            _logger.LogInformation("ReadList. ComputerName:{ComputerName}. Id:{ Id}", model?.ComputerName, model?.Id);
            var list = model == null ? _ComputerStorage.GetFullList() : _ComputerStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count:{Count}", list.Count);
            return list;
        }
        public ComputerViewModel? ReadElement(ComputerSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. ComputerName:{ComputerName}.Id:{ Id}", model.ComputerName, model.Id);
            var element = _ComputerStorage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }
            _logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
            return element;
        }

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

        public bool Update(ComputerBindingModel model)
        {
            CheckModel(model);
            if (_ComputerStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }
        public bool Delete(ComputerBindingModel model)
        {
            CheckModel(model, false);
            _logger.LogInformation("Delete. Id:{Id}", model.Id);
            if (_ComputerStorage.Delete(model) == null)
            {
                _logger.LogWarning("Delete operation failed");
                return false;
            }
            return true;
        }

        private void CheckModel(ComputerBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            if (string.IsNullOrEmpty(model.ComputerName))
            {
                throw new ArgumentNullException("Нет названия компьютера",
               nameof(model.ComputerName));
            }
            if (model.Price <= 0)
            {
                throw new ArgumentNullException("Цена компьютера должна быть больше 0", nameof(model.Price));
            }
            _logger.LogInformation("Computer. Computer:{Computer}. Price:{ Price }. Id: { Id}", model.ComputerName, model.Price, model.Id);
            var element = _ComputerStorage.GetElement(new ComputerSearchModel
            {
                ComputerName = model.ComputerName
            });
            if (element != null && element.Id != model.Id)
            {
                throw new InvalidOperationException("Компонент с таким названием уже есть");
            }
        }
    }
}
