using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TravelCompanyContracts.BindingModels.OperatorBindingModels;
using TravelCompanyContracts.BusinessLogicsContracts.OperatorBusinessLogicsContracts;
using TravelCompanyContracts.SearchModels.OperatorSearchModels;
using TravelCompanyContracts.StoragesContracts.OperatorStoragesContracts;
using TravelCompanyContracts.ViewModels.OperatorViewModels;

namespace TravelCompanyBusinessLogic.BusinessLogic.OperatorLogics
{
    public class TourLogic:ITourLogic
    {
        private readonly ILogger _logger;

        private readonly ITourStorage _tourStorage;

        public TourLogic(ILogger<TourLogic> logger, ITourStorage tourStorage)
        {
            _logger = logger;
            _tourStorage = tourStorage;
        }

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

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

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

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

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

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

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

            var element = _tourStorage.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<TourViewModel>? ReadList(TourSearchModel? model)
        {
            _logger.LogInformation("ReadList. TourName:{TourName}.Id:{ Id}", model?.TourName, model?.Id);

            var list = model == null ? _tourStorage.GetFullList() : _tourStorage.GetFilteredList(model);

            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }

            _logger.LogInformation("ReadList. Count:{Count}", list.Count);

            return list;
        }

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

            if (!withParams)
            {
                return;
            }

            if (string.IsNullOrEmpty(model.TourName))
            {
                throw new ArgumentNullException("Нет названия тура", nameof(model.TourName));
            }

            _logger.LogInformation("Tour. Id:{Id}.TourName:{TourName}.OperatorId:{OperatorId}.",
                model.Id, model.TourName, model.OperatorId);

            var element = _tourStorage.GetElement(new TourSearchModel
            {
                TourName = model.TourName
            });

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