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

namespace BarBusinessLogic.BusinessLogics
{
     public class CocktailLogic : ICocktailLogic
    {

        private readonly ILogger _logger;

        private readonly ICocktailStorage _cocktailStorage;

        public CocktailLogic(ILogger<CocktailLogic> Logger, ICocktailStorage CocktailStorage)
        {
            _logger = Logger;
            _cocktailStorage = CocktailStorage;
        }

        public List<CocktailViewModel>? ReadList(CocktailSearchModel? Model)
        {
            _logger.LogInformation("ReadList. CocktailName:{CocktailName}. Id:{Id}",
                Model?.CocktailName, Model?.Id);

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

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

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

        public CocktailViewModel? ReadElement(CocktailSearchModel? Model)
        {
            if (Model is null)
                throw new ArgumentNullException(nameof(Model));

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

            var Element = _cocktailStorage.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(CocktailBindingModel Model)
        {
            CheckModel(Model);

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

            return true;
        }

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

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

            return true;
        }

        public bool Delete(CocktailBindingModel Model)
        {
            CheckModel(Model);
            _logger.LogInformation("Delete. Id:{Id}", Model.Id);

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

            return true;
        }

        private void CheckModel(CocktailBindingModel Model, bool WithParams = true)
        {
            if (Model == null)
                throw new ArgumentNullException(nameof(Model));

            if (!WithParams)
                return;

            if (string.IsNullOrEmpty(Model.CocktailName))
                throw new ArgumentNullException("Нет названия коктеля", nameof(Model.CocktailName));

            if (Model.Price <= 0)
                throw new ArgumentNullException("Стоимость коктеля должна быть больше 0", nameof(Model.Price));

            _logger.LogInformation("Cocktail. CocktailName:{CocktailName}. Price:{Price}. Id: {Id}",
                Model.CocktailName, Model.Price, Model.Id);

            var Element = _cocktailStorage.GetElement(new CocktailSearchModel
            {
                CocktailName = Model.CocktailName
            });

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