package com.alwaysallthetime.adnlibutils;

import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;

import com.alwaysallthetime.adnlib.data.AbstractPost;
import com.alwaysallthetime.adnlib.data.Entities;

import java.util.List;

public class EntityStyler {

    public static CharSequence getStyledText(AbstractPost post, List<CharacterStyle> mentionStyles, List<CharacterStyle> hashtagStyles, List<CharacterStyle> linkStyles) {
        SpannableStringBuilder string = new SpannableStringBuilder(post.getText());

        Entities entities = post.getEntities();

        if(mentionStyles != null) {
            applyStylesToEntities(string, entities.getMentions(), mentionStyles);
        }
        if(hashtagStyles != null) {
            applyStylesToEntities(string, entities.getHashtags(), hashtagStyles);
        }
        if(linkStyles != null) {
            applyStylesToEntities(string, entities.getLinks(), linkStyles);
        }

        return string;
    }

    private static void applyStylesToEntities(SpannableStringBuilder spannableString, List<? extends Entities.Entity> entities, List<CharacterStyle> styles) {
        for(Entities.Entity e : entities) {
            for(CharacterStyle s : styles) {
                spannableString.setSpan(CharacterStyle.wrap(s), e.getPos(), e.getPos() + e.getLen(), 0);
            }
        }
    }
}

@@ -585,17 +585,51 @@
 ties();%0A
-%0A
+        if(entities != null) %7B%0A    
         
@@ -648,32 +648,36 @@
 yles != null) %7B%0A
+    
             appl
@@ -742,34 +742,42 @@
 tyles);%0A        
-%7D%0A
+    %7D%0A    
         if(hasht
@@ -788,32 +788,36 @@
 yles != null) %7B%0A
+    
             appl
@@ -890,26 +890,33 @@
         
-%7D%0A
+    %7D%0A   
         
 if(linkS
@@ -907,16 +907,17 @@
         
+ 
 if(linkS
@@ -925,32 +925,36 @@
 yles != null) %7B%0A
+    
             appl
@@ -1005,32 +1005,46 @@
 ), linkStyles);%0A
+            %7D%0A
         %7D%0A%0A     

check for null Entities