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 ExcursionLogic : IExcursionLogic
    {
        private readonly ILogger _logger;

        private readonly IExcursionStorage _excursionStorage;

        public ExcursionLogic(ILogger<ExcursionLogic> logger, IExcursionStorage excursionStorage)
        {
            _logger = logger;
            _excursionStorage = excursionStorage;
        }

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

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

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

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

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

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

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

            var element = _excursionStorage.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<ExcursionViewModel>? ReadList(ExcursionSearchModel? model)
        {
            _logger.LogInformation("ReadList. ExcursionName:{ExcursionName}.Id:{ Id}", model?.ExcursionName, model?.Id);

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

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

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

            if (!withParams)
            {
                return;
            }

            if (string.IsNullOrEmpty(model.ExcursionName))
            {
                throw new ArgumentNullException("Нет названия экскурсии", nameof(model.ExcursionName));
            }
            if (string.IsNullOrEmpty(model.ExcursionDescrition))
            {
                throw new ArgumentNullException("Нет описания экскурсии", nameof(model.ExcursionDescrition));
            }
            if (model.Duration <=0)
            {
                throw new ArgumentNullException("Недопустимая длительность экскурсии", nameof(model.Duration));
            }

            _logger.LogInformation("Excursion. Id:{Id}.ExcursionName:{ExcursionName}.ExcursionDescrition:{ExcursionDescrition}.Duration:{Duration}.TouristId:{TouristId}.",
                model.Id, model.ExcursionName, model.ExcursionDescrition, model.Duration, model.TouristId);

            var element = _excursionStorage.GetElement(new ExcursionSearchModel
            {
                ExcursionName = model.ExcursionName
            });

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