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

namespace BusinessLogic.BusinessLogic
{
    public class SupplierLogic : ISupplierLogic
    {
        private readonly ISupplierStorage _supplierStorage;
        private readonly ILogger _logger;
        public SupplierLogic(ISupplierStorage supplierStorage, ILogger<SupplierLogic> logger)
        {
            _supplierStorage = supplierStorage;
            _logger = logger;
        }
        public bool Create(SupplierBindingModel model)
        {
            CheckModel(model);
            if (_supplierStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

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

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

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

        public bool Update(SupplierBindingModel model)
        {
            CheckModel(model);
            if (_supplierStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }
        private void CheckModel(SupplierBindingModel 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.Deals < 0)
            {
                throw new Exception("Кол-во сделок не может быть меньше нуля");
            }
            foreach(var item in model.AvailibleProducts.Values)
            {
                if (item.Item2 < 0)
                {
                    throw new Exception("Кол-во доступных продуктов не должно быть отрицательным");
                }
            }
        }
    }
}
