using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.BusinessLogicsContracts;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JewelryStoreBusinessLogic.BusinessLogics
{
    public class JewelLogic : IJewelLogic // TODO реализовать интерфейс идентично componentLogic
    {
        private readonly ILogger _logger;
        private readonly IJewelStorage _jewelStorage;

        public JewelLogic(ILogger<JewelLogic> logger, IJewelStorage jewelStorage)
        {
            _logger = logger;
            _jewelStorage= jewelStorage;
        }

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

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

        public JewelViewModel? ReadElement(JewelSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. JewelName:{JewelName}.Id:{ Id}", model.JewelName, model.Id);
            var element = _jewelStorage.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<JewelViewModel>? ReadList(JewelSearchModel? model)
        {
            _logger.LogInformation("ReadList. JewelName:{JeweltName}.Id:{ Id}", model?.JewelName, model?.Id);
            var list = model == null ? _jewelStorage.GetFullList() : _jewelStorage.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(JewelBindingModel model)
        {
            CheckModel(model);
            if (_jewelStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }
        private void CheckModel(JewelBindingModel model, bool withParams =
     true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            if (string.IsNullOrEmpty(model.JewelName))
            {
                throw new ArgumentNullException("Нет названия драгоценности",
               nameof(model.JewelName));
            }
            if (model.Price <= 0)
            {
                throw new ArgumentNullException("Цена драгоценности должна быть больше 0", nameof(model.Price));
            }
            _logger.LogInformation("Jewel. JewelName:{JewelName}. Price:{ Price}. Id: { Id}", model.JewelName, model.Price, model.Id);
            var element = _jewelStorage.GetElement(new JewelSearchModel
            {
                JewelName = model.JewelName
            });
            if (element != null && element.Id != model.Id)
            {
                throw new InvalidOperationException("Драгоценность с таким названием уже есть");
            }
        }
    }
}