using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElectronicsShopBusinessLogic.BusinessLogic
{
    public class ProductLogic : IProductLogic
    {
        private readonly ILogger _logger;
        private readonly IProductStorage _storage;

        public ProductLogic(ILogger<ProductLogic> logger, IProductStorage productStorage) {
            _logger = logger;
            _storage = productStorage;
        }

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

        public bool Delete(ProductBindingModel model)
        {
            CheckModel(model, false);
            _logger.LogInformation($"Delete. ID:{model.ID}");
            if (_storage.Delete(model) == null) {
                _logger.LogWarning("Delete operation failed");
                return false;
            }
            return true;
        }

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

        public ProductViewModel? ReadElement(ProductSearchModel? model)
        {
            if (model == null) throw new ArgumentNullException(nameof(model));
            _logger.LogInformation($"ReadElement. ProductName:{model.ProductName}. ID:{model.ID}");
            var element = _storage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement. elementn not found");
                return null;
            }
            _logger.LogInformation($"ReadElement find. ID:{element.ID}");
            return element;
        }

        public List<ProductViewModel>? ReadList(ProductSearchModel? model)
        {
            _logger.LogInformation($"ReadList. ProductName:{model?.ProductName}. ID:{model?.ID}");
            var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation($"ReadList. Count:{list.Count}");
            return list;
        }

        private void CheckModel(ProductBindingModel model, bool withParams = true)
        {
            if (!withParams) {
                return;
            }
            if (string.IsNullOrEmpty(model.ProductName)) {
                throw new ArgumentNullException("Нет названия продукта", nameof(model.ProductName));
            }
            if (model.Price <= 0) {
                throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
            }
            _logger.LogInformation($"Product. ID:{model.ID}.ProductName:{model.ProductName}.Price:{model.Price}.CostItemID:{model.CostItemID}");
            var element = _storage.GetElement(new ProductSearchModel { ProductName = model.ProductName });
            if (element != null && element.ProductName == model.ProductName) {
                throw new InvalidOperationException("Продукт с таким названием уже есть");
            }
        }
    }
}