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 TouristLogic:ITouristLogic
    {
        private readonly ILogger _logger;
        private readonly ITouristStorage _touristStorage;
        public TouristLogic(ILogger<TouristLogic> logger, ITouristStorage touristStorage)
        {
            _logger = logger;
            _touristStorage = touristStorage;
        }
        public List<TouristViewModel>? ReadList(TouristSearchModel? model)
        {
            _logger.LogInformation("ReadList.  Login:{Login}.Id:{ Id}", model?.Login, model?.Id);
            var list = model == null ? _touristStorage.GetFullList() : _touristStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count:{Count}", list.Count);
            return list;
        }
        public TouristViewModel? ReadElement(TouristSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. Login:{Login}.Id:{ Id}", model?.Login, model?.Id);
            var element = _touristStorage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }
            _logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
            return element;
        }
        public bool Create(TouristBindingModel model)
        {
            CheckModel(model);
            if (_touristStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }
        public bool Update(TouristBindingModel model)
        {
            CheckModel(model);
            if (_touristStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }
        private void CheckModel(TouristBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            if (string.IsNullOrEmpty(model.Login))
            {
                throw new ArgumentNullException("Нет логина туриста", nameof(model.Login));
            }
            if (string.IsNullOrEmpty(model.Email))
            {
                throw new ArgumentNullException("Нет почты туриста", nameof(model.Email));
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                throw new ArgumentNullException("Нет имени туриста", nameof(model.Name));
            }
            if (string.IsNullOrEmpty(model.Surname))
            {
                throw new ArgumentNullException("Нет фамилии туриста", nameof(model.Surname));
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                throw new ArgumentNullException("Нет пароля туриста", nameof(model.Password));
            }
            if ((DateTime.Now - model.BirthDay).Days/365<18)
            {
                throw new ArgumentNullException("Неверная дата рождения туриста", nameof(model.BirthDay));
            }
            _logger.LogInformation("Tourist. Id:{Id}.Surname:{Surname}.Name:{Name}.Patronomic:{Patronomic}.BirthDay:{BirthDay}.Email:{Email}.Login:{Login}.Password:{ Password}.",
                model.Id, model.Surname, model.Name, model.Patronymic, model.BirthDay, model.Email, model.Login, new string('*', model.Password.Length));
            var element = _touristStorage.GetElement(new TouristSearchModel
            {
                Login = model.Login
            });
            if (element != null && element.Id != model.Id)
            {
                throw new InvalidOperationException("Турист с таким логином уже есть");
            }
        }
    }
}
