"""
Project: Phone Number and Email Address Extractor
Page 179
"""

import re
import pyperclip


def to_int(string):
    string = re.sub(r"\D", "", string)
    if string:
        return int(string)
    else:
        return None


def rgx_phones():
    return re.compile(
        r"((\+(\d+)\s*)?(([ |\d|\-|\.|\(|\)]){7,}\d)(\s+ext\S*\s+(\d+))?)",
        re.I,
    )


def rgx_emails():
    return re.compile(r"[\w\-]+@[\w\-]+\.\w{2,}", re.I)


def parse_phone(phone):
    lbraces = len(re.findall(r".*\(.*", phone[0]))
    rbraces = len(re.findall(r".*\).*", phone[0]))
    if lbraces == rbraces and re.match(r"^[\+|\d]", phone[0]):
        code = to_int(phone[2])
        ext = to_int(phone[6])
        res = to_int(phone[3])
        res = to_int(phone[3])
        res = f"+{code} {res}" if code else res
        res = f"{res} ext. {ext}" if ext else res
        return res


def get_phones(content):
    return list(filter(None, map(parse_phone, rgx_phones().findall(content))))


content = pyperclip.paste()
phones = get_phones(content)
emails = rgx_emails().findall(content)

for phone in phones:
    print(phone)

for email in emails:
    print(email)
