using Contracts.BusinessLogicContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusinessLogic.Tools
{
    public class TwoFactorAuthService : ITwoFactorAuthService
    {
        private string _code = string.Empty;
        private const int LENGTH_CODE = 5;

        public string GenerateCode()
        {
            _code = _getCode(LENGTH_CODE);
            return _code;
        }

        private string _getCode(int length)
        {
            string res = "";
            var rand = new Random();
            for (int i = 0; i < length; i++)
            {
                res += rand.Next(0, 9).ToString();
            }
            return res;
        }

        public bool Verify(string code)
        {
            if (_code == string.Empty)
            {
                throw new Exception("Source code is not generated.");
            }

            return _code == code;
        }
    }
}