using BarContracts.BindingModels;
using BarContracts.BusinessLogicContracts;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;
using Microsoft.Extensions.Logging;

namespace BarBusinessLogic.BusinessLogics
{
    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. ComponentName:{ComponentName}. Id:{Id}",
                Model?.ComponentName, Model?.Id);

            var List = Model is null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(Model);

            if (List is null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }

            _logger.LogInformation("ReadList. Count:{Count}", List.Count);
            return List;
        }

        public ComponentViewModel? ReadElement(ComponentSearchModel? Model)
        {
            if (Model is null)
                throw new ArgumentNullException(nameof(Model));

            _logger.LogInformation("ReadElement. ComponentName:{ComponentName}. Id:{Id}",
                Model.ComponentName, Model.Id);

            var Element = _componentStorage.GetElement(Model);

            if (Element is null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }

            _logger.LogInformation("ReadElement element found. Id:{Id}", Element.Id);
            return Element;
        }

        public bool Create(ComponentBindingModel Model)
        {
            CheckModel(Model);

            if (_componentStorage.Insert(Model) is null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }

            return true;
        }

        public bool Update(ComponentBindingModel Model)
        {
            CheckModel(Model);

            if (_componentStorage.Update(Model) is null)
            {
                _logger.LogWarning("Update 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) is null)
            {
                _logger.LogWarning("Delete 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.ComponentName))
                throw new ArgumentNullException("Нет названия компонента", nameof(Model.ComponentName));

            if (Model.Cost <= 0)
                throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(Model.Cost));

            _logger.LogInformation("Cocktail. ComponentName:{ComponentName}. Cost:{Cost}. Id:{Id}",
                Model.ComponentName, Model.Cost, Model.Id);

            var Element = _componentStorage.GetElement(new ComponentSearchModel
            {
                ComponentName = Model.ComponentName
            });

            if (Element != null && Element.Id != Model.Id)
                throw new InvalidOperationException("Компонент с таким названием уже есть");
        }
    }
}
