using Contracts.BindingModels;
using Contracts.BusinessLogicsContracts;
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using Microsoft.Extensions.Logging;
using System.Xml.Linq;

namespace BusinessLogic.BusinessLogic
{
    public class DetailLogic : IDetailLogic
    {
        private readonly ILogger _logger;
        private readonly IDetailStorage _detailStorage;
        public DetailLogic(ILogger<DetailLogic> logger, IDetailStorage detailStorage)
        {
            _logger = logger;
            _detailStorage = detailStorage;
        }
        public List<DetailViewModel>? ReadList(DetailSearchModel? model)
        {
            _logger.LogInformation("ReadList. DetailName:{name}. Id:{Id}. UserId:{UserId}", model?.Name, model?.Id, model?.UserId);
            var list = model == null ? _detailStorage.GetFullList() : _detailStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogWarning("ReadList. Count:{Count}", list.Count);
            return list;
        }
        public DetailViewModel? ReadElement(DetailSearchModel? model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. DetailName:{Name}. Id:{Id}", model.Name, model.Id);
            var elem = _detailStorage.GetElement(model);
            if (elem == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }
            _logger.LogInformation("ReadElement find. Id:{Id}", elem.Id);
            return elem;
        }


        private void CheckModel(DetailBindingModel? 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.Cost <= 0)
            {
                throw new ArgumentNullException("Цена детали должна быть больше 0", nameof(model.Cost));
            }
            if (model.DateCreate > DateTime.Now)
            {
                throw new ArgumentNullException("Деталь не может быть создана в будущем", nameof(model.DateCreate));
            }
            _logger.LogInformation("Detail. DetailName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id);
            var elem = _detailStorage.GetElement(new DetailSearchModel
            {
                Name = model.Name
            });
            if (elem != null && elem.Id != model.Id)
            {
                throw new InvalidOperationException("Деталь с таким названием уже существует");
            }
        }
        public bool Create(DetailBindingModel? model)
        {
            CheckModel(model);
            if (_detailStorage.Insert(model!) == null)
            {
                _logger.LogWarning("Insert error");
                return false;
            }
            return true;
        }
        public bool Update(DetailBindingModel? model)
        {
            CheckModel(model);
            if (_detailStorage.Update(model!) == null)
            {
                _logger.LogWarning("Update error");
                return false;
            }
            return true;
        }
        public bool Delete(DetailBindingModel? model)
        {
            CheckModel(model, false);
            _logger.LogInformation("Delete Id:{Id}", model!.Id);
            if (_detailStorage.Delete(model!) == null)
            {
                _logger.LogWarning("Delete error");
                return false;
            }
            return true;
        }
    }
}
