using Knoks.Core.Data.Interfaces;
using Knoks.Core.Entities;
using Knoks.Core.Logic.Interfaces;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Knoks.Core.Logic.Managers
{
    public class GeneralManager : IGeneralManager
    {
        #region Fields
        private readonly ILogger<GeneralManager> _logger;
        private readonly IGeneralDao _generalDao;
        private readonly IExchangeDao _exchangeDao;
        private readonly IUidManager _uidManager;
        #endregion

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="generalDao"></param>
        /// <param name="exchangeDao"></param>
        /// <param name="uidManager"></param>
        public GeneralManager(ILogger<GeneralManager> logger, IGeneralDao generalDao, IExchangeDao exchangeDao, IUidManager uidManager)
        {
            _logger = logger;
            _generalDao = generalDao;
            _exchangeDao = exchangeDao;
            _uidManager = uidManager;
        }

        public async Task<IEnumerable<Country>> GetCountries(string countrySymbol)
        {
            if (!string.IsNullOrWhiteSpace(countrySymbol))
            {
                if (countrySymbol.Length != 2)
                    throw new ArgumentException("Invalid value length. The country symbol length must be 2 characters only.", countrySymbol);

                if (!Regex.IsMatch(countrySymbol, @"^[a-zA-Z]+$"))
                    throw new ArgumentException("Invalid characters. The valid country symbols character must be alphabetic only.", countrySymbol);
            }

            return await _generalDao.GetCountries(countrySymbol);
        }

        public async Task<IEnumerable<Language>> GetLanguages()
        {
            return await _generalDao.GetLanguages();
        }

        public Task<IEnumerable<Currency>> GetCurrencies(string currency)
        {
            return _exchangeDao.GetCurrencies(currency);
        }

        public Task<IEnumerable<Currency>> GetFilterCurrencies(long? userId, bool? availableKnoks, bool? activeKnoks, bool? endedKnoks)
        {
            return _exchangeDao.GetFilterCurrencies(userId, availableKnoks, activeKnoks, endedKnoks);
        }

        public Task<IEnumerable<Exchange>> GetExchanges(long? userId,bool? availableKnoks, bool? activeKnoks, bool? endedKnoks)
        {
            return _exchangeDao.GetExchanges(userId, availableKnoks, activeKnoks, endedKnoks);
        }

        public Task<IEnumerable<KnokStatus>> GetKnokStatuses()
        {
            return _generalDao.GetKnokStatuses();
        }

        public Task<ExchangeStart> GetKnokStartValues(long exchangeId, string currency1, string currency2)
        {
            return _generalDao.GetKnokStartValues(exchangeId, currency1, currency2);
        }

        public Task<IEnumerable<RankCount>> GetUserRankCount(long? userId)
        {
            return _generalDao.GetUserRank(userId);
        }

        public Task<IEnumerable<KnokLossCount>> GetKnoksLossCount(long? userId)
        {
            return _generalDao.GetKnoksLossCount(userId);
        }

        public string GetAvatarUrl(long userId)
        {
            string user = _uidManager.ToUid(userId, typeof(User), Constants.avatarCause);
            return $"/general/{user}/avatar";
        }

        public Task AddKnokFeedBack(Feedback feedback)
        {
            return _generalDao.AddKnokFeedBack(feedback);
        }

   
    }
}
