package pingbits.com.emojikeyboard;

import android.content.Context;
import android.text.Spannable;
import android.util.SparseIntArray;

/**
 * Created by aagam on 6/2/15.
 */
public class EmojiManager {

    public static final SparseIntArray sEmojisMap = new SparseIntArray();

    static {
        sEmojisMap.put(0x1f60a, R.drawable.p1f60a);
        sEmojisMap.put(0x1f60b, R.drawable.p1f60b);
        sEmojisMap.put(0x1f60c, R.drawable.p1f60c);
        sEmojisMap.put(0x1f60d, R.drawable.p1f60d);
        sEmojisMap.put(0x1f60e, R.drawable.p1f60e);
        sEmojisMap.put(0x1f60f, R.drawable.p1f60f);
        sEmojisMap.put(0x1f61a, R.drawable.p1f61a);
        sEmojisMap.put(0x1f61b, R.drawable.p1f61b);
        sEmojisMap.put(0x1f61c, R.drawable.p1f61c);
        sEmojisMap.put(0x1f61d, R.drawable.p1f61d);
        sEmojisMap.put(0x1f61e, R.drawable.p1f61e);
        sEmojisMap.put(0x1f61f, R.drawable.p1f61f);
        sEmojisMap.put(0x1f62a, R.drawable.p1f62a);
        sEmojisMap.put(0x1f62b, R.drawable.p1f62b);
        sEmojisMap.put(0x1f62c, R.drawable.p1f62c);
        sEmojisMap.put(0x1f62d, R.drawable.p1f62d);
        sEmojisMap.put(0x1f62e, R.drawable.p1f62e);
        sEmojisMap.put(0x1f62f, R.drawable.p1f62f);
        sEmojisMap.put(0x1f63a, R.drawable.p1f63a);
        sEmojisMap.put(0x1f63b, R.drawable.p1f63b);
        sEmojisMap.put(0x1f63c, R.drawable.p1f63c);
        sEmojisMap.put(0x1f63d, R.drawable.p1f63d);
        sEmojisMap.put(0x1f63e, R.drawable.p1f63e);
    }

    private static int getEmojiResource(Context context, int codePoint) {
        return sEmojisMap.get(codePoint);
    }
    public static void addEmojis(Context context, Spannable text, int emojiSize) {
        addEmojis(context, text, emojiSize, 0, -1);
    }

    /**
     * Convert emoji characters of the given Spannable to the according emojicon.
     *
     * @param context
     * @param text
     * @param emojiSize
     * @param index
     * @param length
     */
    public static void addEmojis(Context context, Spannable text, int emojiSize, int index, int length) {
        int textLength = text.length();
        int textLengthToProcessMax = textLength - index;
        int textLengthToProcess = length < 0 || length >= textLengthToProcessMax ? textLength : (length+index);

        // remove spans throughout all text
        EmojiSpan[] oldSpans = text.getSpans(0, textLength, EmojiSpan.class);
        for (int i = 0; i < oldSpans.length; i++) {
            text.removeSpan(oldSpans[i]);
        }

        int skip;
        for (int i = index; i < textLengthToProcess; i += skip) {
            int icon = 0;
            char c = text.charAt(i);

            int unicode = Character.codePointAt(text, i);
            skip = Character.charCount(unicode);
                icon = getEmojiResource(context, unicode);

            if (icon > 0) {
                text.setSpan(new EmojiSpan(context, icon, emojiSize), i, i + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
}

@@ -1529,33 +1529,16 @@
 esource(
-Context context, 
 int code
@@ -1597,149 +1597,8 @@
    %7D
-%0A    public static void addEmojis(Context context, Spannable text, int emojiSize) %7B%0A        addEmojis(context, text, emojiSize, 0, -1);%0A    %7D
 %0A%0A  
@@ -1662,15 +1662,8 @@
 ble 
-to the 
 acco
@@ -1671,18 +1671,10 @@
 ding
- emojicon.
+ly
 %0A   
@@ -2111,235 +2111,8 @@
 );%0A%0A
-        // remove spans throughout all text%0A        EmojiSpan%5B%5D oldSpans = text.getSpans(0, textLength, EmojiSpan.class);%0A        for (int i = 0; i %3C oldSpans.length; i++) %7B%0A            text.removeSpan(oldSpans%5Bi%5D);%0A        %7D%0A%0A
     
@@ -2221,46 +2221,8 @@
  0;%0A
-            char c = text.charAt(i);%0A%0A
     
@@ -2364,25 +2364,16 @@
 esource(
-context, 
 unicode)

optimise the way retrieving emojis