using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TravelCompanyContracts.BindingModels.TouristBindingModels;
using TravelCompanyContracts.BusinessLogicsContracts.TouristBusinessLogicsContracts;
using TravelCompanyContracts.SearchModels.TouristSearchModels;
using TravelCompanyContracts.StoragesContracts.TouristStoragesContracts;
using TravelCompanyContracts.ViewModels.TouristViewModels;

namespace TravelCompanyBusinessLogic.BusinessLogic.TouristLogics
{
    public class TravelLogic : ITravelLogic
    {
        private readonly ILogger _logger;

        private readonly ITravelStorage _travelStorage;

        public TravelLogic(ILogger<TravelLogic> logger, ITravelStorage travelStorage)
        {
            _logger = logger;
            _travelStorage = travelStorage;
        }

        public bool Create(TravelBindingModel model)
        {
            CheckModel(model);

            if (_travelStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }

            return true;
        }
        public bool Delete(TravelBindingModel model)
        {
            CheckModel(model, false);

            _logger.LogInformation("Delete. Id:{Id}", model.Id);

            if (_travelStorage.Delete(model) == null)
            {
                _logger.LogWarning("Delete operation failed");
                return false;
            }

            return true;
        }
        public TravelViewModel? ReadElement(TravelSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            _logger.LogInformation("ReadElement. TravelName:{TravelName}.Id:{ Id}", model.TravelName, model.Id);

            var element = _travelStorage.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<TravelViewModel>? ReadList(TravelSearchModel? model)
        {
            _logger.LogInformation("ReadList. TravelName:{TravelName}.Id:{ Id}", model?.TravelName, model?.Id);

            var list = model == null ? _travelStorage.GetFullList() : _travelStorage.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(TravelBindingModel model)
        {
            CheckModel(model);

            if (_travelStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }

            return true;
        }

        private void CheckModel(TravelBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (!withParams)
            {
                return;
            }

            if (string.IsNullOrEmpty(model.TravelName))
            {
                throw new ArgumentNullException("Нет названия путешествия", nameof(model.TravelName));
            }
            if (string.IsNullOrEmpty(model.TravelDescription))
            {
                throw new ArgumentNullException("Нет описания путешествия", nameof(model.TravelDescription));
            }
            if (model.DateStart == default)
            {
                throw new ArgumentNullException("Нет даты начала путешествия", nameof(model.DateStart));
            }
            if (model.TourTravels == null || model.TourTravels.Count == 0)
            {
                throw new ArgumentException("Нет туров путешествия", nameof(model.TourTravels));
            }

            _logger.LogInformation("Travel. Id:{Id}.TravelName:{TravelName}.TravelDescription:{TravelDescription}.DateStart:{DateStart}.TouristId:{TouristId}.",
                model.Id, model.TravelName, model.TravelDescription, model.DateStart, model.TouristId);

            var element = _travelStorage.GetElement(new TravelSearchModel
            {
                TravelName = model.TravelName
            });

            if (element != null && element.Id != model.Id && element.TouristId == model.TouristId)
            {
                throw new InvalidOperationException("Путешествие с таким названием уже есть");
            }
        }
    }
}
