"""
Validators module

application.modules.core.utils.validators
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Validators module holds all validation that can be performed on input
and or resources
"""
import re
#import dns.resolver

def is_website(website_url):
    """
    Ensures that a given url is a valid domain name based on the well known
    naming formats. IP addresses now allowed

    :param website_url:     The website uri to be validated for correctness
                            :type <type, 'str'>
    """
    pass


def validate_domain_name(domain):
    """
    Ensures that a given domain name is a valid based on implicit formats
    IP addresses now allowed

    :param domain:  The website uri to be validated for correctness
                    :type <type, 'str'>
    
    :return url:    The well parsed protocol removed string representing a valid
                    domain name.
                    :type <type, 'str'>
    """
    HTTPS = 'https://'
    HTTP = 'http://'

    web = domain.strip('https://') if domain.startswith('https:') else domain.strip('http://')
    url = web.strip('www.')
    dots = url.count('.')
    if dots > 2:
        return False
    return url


def validate_txt_record(self, dns_record='TXT'):
    """
    Verifies its text record based on the random text key generated by
    the __generate_txt_token method.

    :param dns_record:      The type of dns record to verify for match with the randomly
                            generated txt record
                            :type <type, 'str'>

    :return matched:        Boolean value specifying the match condition of the provided txt_value
                            to the persisted txt value
                            :type <type, 'bool'>
    """
    try:
        dns_resolver = dns.resolver.Resolver()
        answer = dns_resolver.query(self.url, dns_record)
        for txt in answer:
            if txt.strings[0] == self.txt:
                return True
    except Exception as e:
        logger.error(e.message)
    return False

def is_phone_valid(phone_number):
    """
    Verifies if the phone number provided is a valid phone number for the countries
    that we support. Currently on Nigerian numbers are valid.

    :param phone_number: the phone number to be validated

    :return bool: True if the number is a valid number, False otherwise.
    """
    mtn_prefix = r'703|706|803|806|810|813|814|816|903|' 
    glo_prefix = r'705|805|807|811|815|905|' 
    airtel_prefix = r'701|708|802|808|812|902|'
    etisalat_prefix = r'809|817|818|909|'
    starcomms_prefix = r'7028|7029|819|' 
    visaphone_prefix = r'7025|7026|704|'
    multilinks_prefix = r'7027|709|'
    zoommobile_prefix = r'707|'
    ntel_prefix = r'804|'
    smile_prefix = r'702'

    phone_re = re.compile(r'^(?P<country_code>\+234)(?P<prefix>'+mtn_prefix+glo_prefix+airtel_prefix+etisalat_prefix+\
        starcomms_prefix+visaphone_prefix+multilinks_prefix+zoommobile_prefix+\
        ntel_prefix+smile_prefix + r')(?P<sufix>\d+)')

    match = phone_re.match(phone_number)
    if match:
        if len(match.group('sufix')) > 7:
            return False
        else:
            return True

    return False