package cn.hadcn.keyboard.emoticon.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.Spannable;

import java.util.ArrayList;

import cn.hadcn.keyboard.emoticon.db.EmoticonDBHelper;
import cn.hadcn.keyboard.emoticon.EmoticonBean;
import cn.hadcn.keyboard.utils.EmoticonLoader;
import cn.hadcn.keyboard.view.VerticalImageSpan;

/**
 * EmoticonHandler
 * Created by 90Chris on 2015/11/25.
 */
public class EmoticonHandler {
    private static ArrayList<EmoticonBean> mEmoticonBeans = new ArrayList<>();
    private static EmoticonHandler sEmoticonHandler = null;
    private Context mContext;
    EmoticonDBHelper emoticonDbHelper = null;

    public static EmoticonHandler getInstance( @NonNull Context context ) {
        if ( sEmoticonHandler == null ) {
            sEmoticonHandler = new EmoticonHandler( context );
        }
        return sEmoticonHandler;
    }

    private EmoticonHandler( Context context ) {
        mContext = context;
        emoticonDbHelper = new EmoticonDBHelper(context);
    }

    public EmoticonDBHelper getEmoticonDbHelper() {
        if ( emoticonDbHelper == null ) {
            emoticonDbHelper = new EmoticonDBHelper(mContext);
        }
        return emoticonDbHelper;
    }

    public ArrayList<EmoticonBean> loadEmoticonsToMemory() {
        mEmoticonBeans = emoticonDbHelper.queryAllEmoticonBeans();
        emoticonDbHelper.cleanup();

        return mEmoticonBeans;
    }

    public String getEmoticonUriByTag( String tag ) {
        return emoticonDbHelper.getUriByTag(tag);
    }

    public void setTextFace( String content, Spannable spannable, int start, int size ) {
        if ( mEmoticonBeans == null ) {
            mEmoticonBeans = emoticonDbHelper.queryAllEmoticonBeans();
            emoticonDbHelper.cleanup();
        }
        if ( content.length() <= 0 ) {
            return;
        }
        int keyIndex = start;
        for ( EmoticonBean bean : mEmoticonBeans ) {
            String key = bean.getTag();
            int keyLength = key.length();
            while ( keyIndex >= 0 ) {
                keyIndex = content.indexOf(key, keyIndex);  //when do not find, get -1
                if ( keyIndex < 0 ) {
                    break;
                }
                Drawable drawable = EmoticonLoader.getInstance(mContext).getDrawable(bean.getIconUri());
                drawable.setBounds(0, 0, size, size);
                VerticalImageSpan imageSpan = new VerticalImageSpan(drawable);
                spannable.setSpan(imageSpan, keyIndex, keyIndex + keyLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                keyIndex += keyLength;
            }
            keyIndex = start;
        }
    }
}

@@ -213,16 +213,87 @@
 ayList;%0A
+import java.util.List;%0A%0Aimport cn.hadcn.keyboard.emoticon.EmoticonBean;
 %0Aimport 
@@ -344,56 +344,8 @@
 er;%0A
-import cn.hadcn.keyboard.emoticon.EmoticonBean;%0A
 impo
@@ -548,20 +548,8 @@
 ate 
-static Array
 List
@@ -693,16 +693,24 @@
 xt;%0A    
+private 
 Emoticon
@@ -790,17 +790,16 @@
 nstance(
- 
 @NonNull
@@ -806,33 +806,32 @@
  Context context
- 
 ) %7B%0A        if (
@@ -830,17 +830,16 @@
     if (
- 
 sEmotico
@@ -846,33 +846,32 @@
 nHandler == null
- 
 ) %7B%0A            
@@ -909,25 +909,47 @@
 Handler(
- 
 context
- 
+.getApplicationContext()
 );%0A     
@@ -1021,17 +1021,16 @@
 Handler(
- 
 Context 
@@ -1036,17 +1036,16 @@
  context
- 
 ) %7B%0A    
@@ -1197,17 +1197,16 @@
     if (
- 
 emoticon
@@ -1213,33 +1213,32 @@
 DbHelper == null
- 
 ) %7B%0A            
@@ -1349,21 +1349,16 @@
  public 
-Array
 List%3CEmo
@@ -1574,17 +1574,16 @@
 riByTag(
- 
 String t
@@ -1584,17 +1584,16 @@
 ring tag
- 
 ) %7B%0A    
@@ -1673,17 +1673,16 @@
 extFace(
- 
 String c
@@ -1729,17 +1729,16 @@
 int size
- 
 ) %7B%0A    
@@ -1745,17 +1745,16 @@
     if (
- 
 mEmotico
@@ -1767,17 +1767,16 @@
  == null
- 
 ) %7B%0A    
@@ -1904,17 +1904,16 @@
     if (
- 
 content.
@@ -1925,17 +1925,16 @@
 h() %3C= 0
- 
 ) %7B%0A    
@@ -2002,17 +2002,16 @@
    for (
- 
 Emoticon
@@ -2036,17 +2036,16 @@
 conBeans
- 
 ) %7B%0A    
@@ -2141,17 +2141,16 @@
  while (
- 
 keyIndex
@@ -2154,17 +2154,16 @@
 dex %3E= 0
- 
 ) %7B%0A    
@@ -2265,17 +2265,16 @@
     if (
- 
 keyIndex
@@ -2277,17 +2277,16 @@
 ndex %3C 0
- 
 ) %7B%0A    
@@ -2411,24 +2411,49 @@
 rawable(bean
+%0A                        
 .getIconUri(
@@ -2675,16 +2675,41 @@
 pannable
+%0A                        
 .SPAN_EX

fix memory leak