using System.Text.RegularExpressions;

namespace NetinaCMS.Common.Extensions;
public static class PhoneNumberExtensions
{
    public static bool CheckPhoneNumber(string phoneNumber)
    {
        var regex = new Regex(@"(^(989|0989|\+989|09|9)[0-9]{9}$)");
        return regex.IsMatch(phoneNumber);
    }
    public static string GetVerifyFromPhoneNumber(string phoneNumber)
    {
        var dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute / 5, 0);
        var timeStamp = ((DateTimeOffset)dateTime).ToUnixTimeMilliseconds() / 10000;

        int FTD = int.Parse(string.Format("{0}{1}{2}",
            GetSumDigit(int.Parse(string.Format("{0}{1}{2}", phoneNumber[5], phoneNumber[7], phoneNumber[9]))) % 10,
            GetSumDigit(int.Parse(string.Format("{0}{1}{2}", phoneNumber[4], phoneNumber[6], phoneNumber[8]))) % 10,
            GetSumDigit(int.Parse(string.Format("{0}{1}{2}", phoneNumber[10], phoneNumber[9], phoneNumber[8]))) % 10));
        int ATD = GetSumDigit(((int)timeStamp % 1000) + FTD);
        timeStamp = (int)timeStamp / 1000;
        int BTD = GetSumDigit(((int)timeStamp % 1000) + ATD);
        timeStamp = (int)timeStamp / 1000;
        int CTD = GetSumDigit(((int)timeStamp % 1000) + ATD);
        FTD = GetSumDigit(FTD);
        if (ATD % 2 == 0)
            return string.Format("{0}{1}{2}{3}", GetSumDigit(ATD) % 10, GetSumDigit(BTD) % 10, GetSumDigit(CTD) % 10, GetSumDigit(FTD) % 10);
        else
            return string.Format("{0}{1}{2}{3}", ATD % 10, BTD % 10, CTD % 10, FTD % 10);
    }
    private static int GetSumDigit(int number)
    {
        string sNumber = number.ToString();
        int total = 0;
        foreach (var s in sNumber)
            total += int.Parse(s.ToString());
        return total;
    }


}