using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Implements;
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 MediaFileLogic : IMediaFileLogic
    {
        private readonly IMediaFileStorage _mediafileStorage;
        private readonly ILogger _logger;
        public MediaFileLogic(IMediaFileStorage mediafileStorage, ILogger<MediaFileLogic> logger)
        {
            _mediafileStorage = mediafileStorage;
            _logger = logger;
        }
        public bool Create(MediaFileBindingModel model)
        {
            CheckModel(model);
            if (_mediafileStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

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

        public MediaFileViewModel? ReadElement(MediaFileSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement .Id:{ Id}", model.Id);
            var element = _mediafileStorage.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<MediaFileViewModel>? ReadList(MediaFileSearchModel? model)
        {
            _logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
            var list = model == null ? _mediafileStorage.GetFullList() : _mediafileStorage.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(MediaFileBindingModel model)
        {
            CheckModel(model);
            if (_mediafileStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }

        private void CheckModel(MediaFileBindingModel 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.Location == null)
            {
                throw new ArgumentNullException("Нет пути", nameof(model.Name));
            }
            _logger.LogError("Проверка модели медиа файла выдала ошибку.");
        }
    }
}
