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

namespace GarmentFactoryBusinessLogic.BusinessLogics
{
    public class TextileLogic : ITextileLogic
    {
        private readonly ILogger _logger;

        private readonly ITextileStorage _textileStorage;

        public TextileLogic(ILogger<TextileLogic> logger, ITextileStorage textileStorage)
        {
            _logger = logger;
            _textileStorage = textileStorage;
        }

        public List<TextileViewModel>? ReadList(TextileSearchModel? model)
        {
            _logger.LogInformation("ReadList. TextileName: {TextileName}. Id: {Id}", model?.TextileName, model?.Id);
            var list = model == null ? _textileStorage.GetFullList() : _textileStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count: {Count}", list.Count);
            return list;
        }

        public TextileViewModel? ReadElement(TextileSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. TextileName: {TextileName}. Id: {Id}", model.TextileName, model.Id);
            var element = _textileStorage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }
            _logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
            return element;
        }

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

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

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

        private void CheckModel(TextileBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            if (string.IsNullOrEmpty(model.TextileName))
            {
                throw new ArgumentNullException("Нет названия изделия", nameof(model.TextileName));
            }
            if (model.Price <= 0)
            {
                throw new ArgumentNullException("Цена ткани должна быть больше 0", nameof(model.Price));
            }
            if (model.TextileComponents == null || model.TextileComponents.Count == 0)
            {
                throw new ArgumentNullException("Изделие должно состоять хотя бы из одного компонента");
            }
            _logger.LogInformation("Textile. TextileName: {TextileName}. Price: {Price}. Id: {Id}", model.TextileName, model.Price, model.Id);
            var element = _textileStorage.GetElement(new TextileSearchModel
            {
                TextileName = model.TextileName
            });
            if (element != null && element.Id != model.Id)
            {
                throw new InvalidOperationException("Изделие с таким названием уже есть");
            }
        }
    }
}
