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 PlaceLogic : IPlaceLogic
    {
        private readonly ILogger _logger;

        private readonly IPlaceStorage _placeStorage;

        public PlaceLogic(ILogger<PlaceLogic> logger, IPlaceStorage placeStorage)
        {
            _logger = logger;
            _placeStorage = placeStorage;
        }

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

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

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

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

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

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

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

            var element = _placeStorage.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<PlaceViewModel>? ReadList(PlaceSearchModel? model)
        {
            _logger.LogInformation("ReadList. PlaceName:{PlaceName}.Id:{ Id}", model?.PlaceName, model?.Id);

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

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

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

            if (!withParams)
            {
                return;
            }

            if (string.IsNullOrEmpty(model.PlaceName))
            {
                throw new ArgumentNullException("Нет названия места", nameof(model.PlaceName));
            }
            if (string.IsNullOrEmpty(model.PlaceAddress))
            {
                throw new ArgumentNullException("Нет адрес места", nameof(model.PlaceAddress));
            }
            if(model.ExcursionId < 0)
            {
                throw new ArgumentNullException("Нет экскурсии места", nameof(model.PlaceAddress));
            }

            _logger.LogInformation("Place. Id:{Id}.PlaceName:{PlaceName}.PlaceAddress:{PlaceAddress}.ExcursionId:{ExcursionId}.TouristId:{TouristId}.",
                model.Id, model.PlaceName, model.PlaceAddress, model.ExcursionId, model.TouristId);

            var element = _placeStorage.GetElement(new PlaceSearchModel
            {
                PlaceName = model.PlaceName
            });

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