import os
import re
import sys
import emoji
# import emojify
from emojify import encode_string
from emojify import EMOTICONS

class EmojiProcessingNotInitialised(Exception):
    """we have not run the init_emoticon_to_emotion"""
    print("Error: Init_emoticon_to_emotion not executed", file=sys.stderr)
    pass

# import demoji

emoticons= {'emoticon' : [
  'o/',
  '</3',
  '<3',
  '8-D',
  '8D',
  ':-D',
  ':-3',
  ':3',
  ':D',
  'B^D',
  'X-D',
  'XD',
  'x-D',
  'xD',
  ":')",
  ":'-)",
  ':-))',
  '8)',
  ':)',
  ':-)',
  ':]',
  ':^)',
  ':c)',
  ':o)',
  ':}',
  ':っ)',
  '0:)',
  '0c[:-1] in list_of_emoticons',
  '0:-3',
  '0:3',
  '0;^)',
  'O:-)',
  '3:)',
  '3:-)',
  '}:)',
  '}:-)',
  '*)',
  '*-)',
  ':-,',
  ';)',
  ';-)',
  ';-]',
  ';D',
  ';]',
  ';^)',
  ':-|',
  ':|',
  ':(',
  ':-(',
  ':-<',
  ':-[',
  ':-c',
  ':<',
  ':[',
  ':c',
  ':{',
  ':っC',
  '%)',
  '%-)',
  ':-P',
  ':-b',
  ':-p',
  ':-Þ',
  ':-þ',
  ':P',
  ':b',
  ':p',
  ':Þ',
  ':þ',
  ';(',
  'X-P',
  'XP',
  'd:',
  'x-p',
  'xp',
  ':-||',
  ':@',
  ':-.',
  ':-/',
  ':/',
  ':L',
  ':S',
  ':\\',
  ":'(",
  ":'-(",
  '^5',
  '^<_<',
  'o/\\o',
  '|-O',
  '|;-)',
  ':###..',
  ':#',
  ':-###..',
  'D-\'',
  'D8',
  'D:',
  'D:<',
  'D;',
  'DX',
  'v.v',
  '8-0',
  ':-O',
  ':-o',
  ':O',
  ':o',
  'O-O',
  'O_O',
  'O_o',
  'o-o',
  'o_O',
  'o_ok',
  ':$',
  '#-)',
  ':&',
  ':-#',
  ':-&',
  ':-X',
  ':X',
  ':-J',
  ':*',
  ':^*',
  'ಠ_ಠ',
  '*\\0/*',
  '\\o/',
  ':>',
  '>.<',
  '>:(',
  '>:)',
  '>:-)',
  '>:/',
  '>:O',
  '>:P',
  '>:[',
  '>:\\',
  '>;)',
  '>_>^',
  '^^'
],
'emoji': [
'👋',
'💔',
'💗',
'😁',
'😁',
'😁',
'😁',
'😁',
'😁',
'😁',
'😁',
'😁',
'😁',
'😁',
'😂',
'😂',
'😃',
'😄',
'😊',
'😄',
'😄',
'😄',
'😄',
'😄',
'😄',
'😄',
'😇',
'😇',
'😇',
'😇',
'😇',
'😇',
'😈',
'😈',
'😈',
'😈',
'😉',
'😉',
'😉',
'😉',
'😉',
'😉',
'😉',
'😉',
'😉',
'😐',
'😐',
'😞',
'😒',
'😒',
'😒',
'😒',
'😒',
'😒',
'😒',
'😒',
'😒',
'😖',
'😖',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😜',
'😠',
'😠',
'😡',
'😡',
'😐',
'😡',
'😡',
'😡',
'😢',
'😢',
'😤',
'😤',
'😤',
'😫',
'😫',
'😰',
'😅',
'😰',
'😱',
'😱',
'😱',
'😱',
'😱',
'😱',
'😱',
'😲',
'😲',
'😲',
'😲',
'😲',
'😲',
'😲',
'😲',
'😲',
'😲',
'😲',
'😳',
'😵',
'😶',
'😶',
'😶',
'😶',
'😶',
'😼',
'😘',
'😽',
'🙅',
'🙆',
'🙆',
'😄',
'😡',
'😠',
'😈',
'😈',
'😡',
'😲',
'😜',
'😒',
'😡',
'😈',
'😤',
'😊'
]
}
list_of_emoticons=list(emoticons['emoticon'])
list_of_emoticons_emoji=list(emoticons['emoji'])

# populated by init_emoticon_to_emotion

list_of_emoticons_emotion=[]


emoj = {'Emoji': ['👏 🏾',
 '👏 🏼',
 '👏 🏽',
 '👨 \u200d ❤ ️ \u200d 👨',
 '👩 \u200d ❤ ️ \u200d 👨',
 '👩 \u200d ❤ ️ \u200d 👩',
 '🤤',
 '😘',
 '😋',
 '😛',
 '😝',
 '😜',
 '😂',
 '💪',
 '💪 🏿',
 '💪 🏻',
 '💪 🏾',
 '💪 🏼',
 '💪 🏽',
 '🙏',
 '🙏 🏿',
 '🙏 🏻',
 '🙏 🏾',
 '🙏 🏼',
 '🙏 🏽',
 '🌝',
 '😀',
 '😁',
 '💗',
 '🤝',
 '💟',
 '♥',
 '💘',
 '💝',
 '✔',
 '🌺',
 '🤗',
 '💏',
 '👨 \u200d ❤ ️ \u200d 💋 \u200d 👨',
 '💋',
 '👩 \u200d ❤ ️ \u200d 💋 \u200d 👨',
 '👩 \u200d ❤ ️ \u200d 💋 \u200d 👩',
 '😽',
 '😗',
 '😚',
 '😙',
 '🕺',
 '🕺 🏿',
 '🕺 🏻',
 '🕺 🏾',
 '🕺 🏼',
 '🕺 🏽',
 '💁 \u200d ♂ ️',
 '💁 🏿 \u200d ♂ ️',
 '💁 🏻 \u200d ♂ ️',
 '💁 🏾 \u200d ♂ ️',
 '💁 🏼 \u200d ♂ ️',
 '💁 🏽 \u200d ♂ ️',
 '🤓',
 '👐',
 '👐 🏿',
 '👐 🏻',
 '👐 🏾',
 '👐 🏼',
 '👐 🏽',
 '🙌',
 '🙌 🏿',
 '🙌 🏻',
 '🙌 🏾',
 '🙌 🏼',
 '🙌 🏽',
 '😌',
 '🤣',
 '🤘',
 '🤘 🏿',
 '🤘 🏻',
 '🤘 🏾',
 '🤘 🏼',
 '🤘 🏽',
 '😴',
 '🙂',
 '😇',
 '😍',
 '😃',
 '😆',
 '😅',
 '😄',
 '😊',
 '😎',
 '👍',
 '👍 🏿',
 '👍 🏻',
 '👍 🏾',
 '👍 🏼',
 '👍 🏽',
 '💕',
 '✌',
 '✌ 🏿',
 '✌ 🏻',
 '✌ 🏾',
 '✌ 🏼',
 '✌ 🏽',
 '🖖',
 '🖖 🏿',
 '🖖 🏻',
 '🖖 🏾',
 '🖖 🏼',
 '🖖 🏽',
 '👋',
 '👋 🏿',
 '👋 🏻',
 '👋 🏾',
 '👋 🏼',
 '👋 🏽',
 '😉',
 '💃',
 '💃 🏿',
 '💃 🏻',
 '💃 🏾',
 '💃 🏼',
 '💃 🏽',
 '💛',
 '🤙',
 '🤙 🏿',
 '🤙 🏻',
 '🤙 🏾',
 '🤙 🏼',
 '🤙 🏽',
 '👏',
 '👏 🏿',
 '👏 🏻',
 '💓',
 '😥',
 '😞',
 '😓',
 '🤕',
 '😷',
 '🤒',
 '😶',
 '☹',
 '😦',
 '😭',
 '🤥',
 '😐',
 '😔',
 '😪',
 '🙁',
 '😫',
 '🙃',
 '😩',
 '😟',
 '🤐',
 '💔',
 '😑',
 '😤',
 '🤦 \u200d ♂ ️',
 '🤦 🏿 \u200d ♂ ️',
 '🤦 🏻 \u200d ♂ ️',
 '🤦 🏾 \u200d ♂ ️',
 '🤦 🏼 \u200d ♂ ️',
 '🤦 🏽 \u200d ♂ ️',
 '🙅 \u200d ♂ ️',
 '🙅 🏿 \u200d ♂ ️',
 '🇬 🇱',
 '🙅 🏾 \u200d ♂ ️',
 '🙅 🏼 \u200d ♂ ️',
 '🙅 🏽 \u200d ♂ ️',
 '🤷 \u200d ♂ ️',
 '🤷 🏿 \u200d ♂ ️',
 '🤷 🏻 \u200d ♂ ️',
 '🤷 🏾 \u200d ♂ ️',
 '🖕',
 '🖕 🏿',
 '🖕 🏻',
 '🖕 🏾',
 '🖕 🏼',
 '🖕 🏽',
 '👊',
 '👊 🏿',
 '👊 🏻',
 '👊 🏾',
 '👊 🏼',
 '👊 🏽',
 '😣',
 '🤦',
 '🤦 🏿',
 '🤦 🏻',
 '🤦 🏾',
 '🤦 🏼',
 '🤦 🏽',
 '🤷',
 '🤷 🏿',
 '🤷 🏻',
 '🤷 🏾',
 '🤷 🏼',
 '🤷 🏽',
 '😡',
 '😈',
 '😏',
 '👎',
 '👎 🏿',
 '👎 🏻',
 '👎 🏾',
 '👎 🏼',
 '👎 🏽',
 '😒',
 '🙅 \u200d ♀ ️',
 '🙅 🏿 \u200d ♀ ️',
 '🙅 🏻 \u200d ♀ ️',
 '🙅 🏾 \u200d ♀ ️',
 '🙅 🏼 \u200d ♀ ️',
 '🙅 🏽 \u200d ♀ ️',
 '😠',
 '👿',
 '😵',
 '😱',
 '😮',
 '🙄',
 '😯',
 '🙆 \u200d ♂ ️',
 '🙆 🏿 \u200d ♂ ️',
 '🙆 🏻 \u200d ♂ ️',
 '🙆 🏾 \u200d ♂ ️',
 '🙆 🏼 \u200d ♂ ️',
 '🙆 🏽 \u200d ♂ ️',
 '🙆 \u200d ♀ ️',
 '🙆 🏿 \u200d ♀ ️',
 '🙆 🏻 \u200d ♀ ️',
 '🙆 🏾 \u200d ♀ ️',
 '🙆 🏼 \u200d ♀ ️',
 '🙆 🏽 \u200d ♀ ️',
 '😧',
 '😲',
 '🤞',
 '🤞 🏿',
 '🤞 🏻',
 '🤞 🏾',
 '🤞 🏼',
 '🤞 🏽',
 '😰',
 '😨',
 '😳',
 '😬',
 '🤢',
 '🤧'],
            'Emotion': ['Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Happy',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Sad',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Angry',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Surprise',
 'Fear',
 'Fear',
 'Fear',
 'Fear',
 'Fear',
 'Fear',
 'Fear',
 'Fear',
 'Fear',
 'Fear',
 'Fear',
 'Fear']}
list_of_emojis=list(emoj['Emoji'])
list_of_emotions=list(emoj['Emotion'])



# once of preprocessing to generate a list of emoticons to text emotion
def init_emoticon_to_emotion():
    # print("initialisating emoticon to emotion table")
    # this generates the two separate lists
    for emoticon in list_of_emoticons:
        # get emoji from emoticon
        try:
            emoji_indx_in_emoticons=list_of_emoticons.index(emoticon)
            emoji_content=list_of_emoticons_emoji[emoji_indx_in_emoticons]
            # print("emoji: " + str(emoji_content))
            emotion_idx=list_of_emojis.index(emoji_content) 
            emotion=list_of_emotions[emotion_idx]
            list_of_emoticons_emotion.append(emotion)
        except ValueError:
            # if we dont have it listed empty item
            list_of_emoticons_emotion.append("")

init_emoticon_to_emotion()

# i can't seem to get this to work!?
def emojis_extractor(text):
    # import pdb; pdb.set_trace()
    a = " ".join(c for c in text if c in emoji.UNICODE_EMOJI).split()
    for i in a:
        try:
            text = text.replace(i, " " + emoj['Emotion'][emoj["Emoji"].index(i)] + " ")
        except:
            pass
    return text

# def extract_emojis_from_text(incoming_text):
#     text=incoming_text
#     for c in incoming_text.split():
#         if c in list_of_emojis:
#             indx=list_of_emojis.index(c) 
#             text=text.replace(c, " " + list_of_emotions[indx] + " ")
#     return text

def replace_emojis_within_text(incoming_text):
    text=incoming_text
    indx=None
    print("incoming text: " + str(incoming_text.split()))
    print("list_of_emojis: " + str(list_of_emojis))
    for c in incoming_text.split():
        if c in list_of_emojis:
            indx=list_of_emojis.index(c) 
        elif c[:-1] in list_of_emojis:
            indx=list_of_emojis.index(c[:-1]) 
        elif c[1:] in list_of_emoticons:
            indx=list_of_emojis.index(c[1:]) 
        elif c[1:-1] in list_of_emojis:
            indx=list_of_emojis.index(c[1:-1]) 
        if indx==None: continue
        text=text.replace(c, " " + list_of_emojis[indx] + " ")
    return text

# replaces :-) with 🤤
def convert_multi_char_emoji_to_emoji(incoming_text):
    text=incoming_text
    indx=None
    for c in incoming_text.split():
        indx=None
        if c in list_of_emoticons:
            indx=list_of_emoticons.index(c) 
        elif c[:-1] in list_of_emoticons:
            indx=list_of_emoticons.index(c[:-1]) 
        elif c[1:] in list_of_emoticons:
            indx=list_of_emoticons.index(c[1:]) 
        elif c[1:-1] in list_of_emoticons:
            indx=list_of_emoticons.index(c[1:-1]) 
        if indx==None: continue
        text=text.replace(c, list_of_emoticons_emoji[indx])
    return text

# so we can mock it
def is_list_of_emoticons_emotion_empty():
    return(list_of_emoticons_emotion == [])

# replaces :-) with Happy
# the init_emoticon_to_emotion must be run first
def convert_multi_char_emoji_to_emotion(incoming_text):
    if is_list_of_emoticons_emotion_empty(): raise EmojiProcessingNotInitialised()
    text=incoming_text
    indx=None
    for c in incoming_text.split():
        # NOTE this is not perfect we shoudl be finding emojis within the string, we are just taking
        # out basic punctuation
        # TODO whittle down left and right of c until its nothing
        indx=None
        if c in list_of_emoticons:
            indx=list_of_emoticons.index(c) 
        elif c[:-1] in list_of_emoticons:
            indx=list_of_emoticons.index(c[:-1]) 
        elif c[1:] in list_of_emoticons:
            indx=list_of_emoticons.index(c[1:]) 
        elif c[1:-1] in list_of_emoticons:
            indx=list_of_emoticons.index(c[1:-1]) 
        if indx==None: continue
        emotion=list_of_emoticons_emotion[indx]
        if list_of_emoticons_emotion[indx] == '':
            text=text.replace(c, '')
        else:
            text=text.replace(c, " " + emotion + " ")
    return text




