import emoji
import os.path
from re import escape

username_re = "[a-z0-9_]{3,40}"
display_name_re = None
emoji_re = None


def get_display_name_regex():
    global display_name_re
    if display_name_re:
        return display_name_re
    emoji_regex = get_emoji_regex()
    display_name_re = u'(' + emoji_regex + u'|([a-zA-Z0-9_/-/() ])){3,40}'
    return get_display_name_regex()


def get_username_regex():
    return username_re


def get_emoji_regex():
    global emoji_re
    if emoji_re:
        return emoji_re

    if os.path.exists("Rest/emoji.regex"):
        with open("Rest/emoji.regex", 'r') as file:
            emoji_re = file.read()
        return emoji_re

    emojis = sorted(emoji.unicode_codes.EMOJI_DATA, key=len, reverse=True)
    emoji_re = u'(' + u'|'.join(escape(u) for u in emojis) + u')'
    with open("Rest/emoji.regex", 'w') as file:
        file.write(emoji_re)
    return emoji_re
