package nl.hsleiden.utils;

import java.text.MessageFormat;

import twitter4j.EntitySupport;
import twitter4j.HashtagEntity;
import twitter4j.MediaEntity;
import twitter4j.URLEntity;
import twitter4j.UserMentionEntity;

public class TwitterUtils {
    
    private static final String TWEET_URL = "<a href={0} target=_blank>{1}</a>";
    private static final String TWEET_HASH_URL = "<a href=https://twitter.com/#!/search?q=%23{0}&src=typd target=_blank>{1}</a>";
    private static final String TWEET_MENTIONED_URL = "<a href=https://twitter.com/#!/search?q=from:{0}&src=typd target=_blank>{1}</a>";
    private static final String TWEET_IMG = "<img src={0} title={1}></img>";
    
    private TwitterUtils() {
        super();
    }


    public static String convertMessage(String message, EntitySupport entity) {
        MessageFormat urlFormatter = new MessageFormat(TWEET_URL);
        MessageFormat hashFormatter = new MessageFormat(TWEET_HASH_URL);
        MessageFormat userFormatter = new MessageFormat(TWEET_MENTIONED_URL);
        MessageFormat imgFormatter = new MessageFormat(TWEET_IMG);
            
        String formattedMessage = message;
        
        URLEntity[] urlEntities = entity.getURLEntities(); 
        if(urlEntities != null) {
            for(URLEntity urlEntity : urlEntities) {
                String url = urlEntity.getURL();
                formattedMessage = formattedMessage.replace(url, urlFormatter.format(new Object[]{url,url}));
            }
        }
        
        MediaEntity[] mediaEntities = entity.getMediaEntities();
        if(mediaEntities!=null){
            for (MediaEntity mediaEntity : mediaEntities) {
                if(mediaEntity.getType().equals("photo")){
                    String mediaUrl = mediaEntity.getURL();
                    
                    formattedMessage = formattedMessage.replace(mediaUrl, imgFormatter.format(new Object[]{mediaEntity.getMediaURL(),mediaUrl}));  
                }
            }
        }
        
        HashtagEntity[] hashEntities = entity.getHashtagEntities(); 
        if(hashEntities != null) {
            for(HashtagEntity hashEntity : hashEntities) {
                String query = hashEntity.getText();
                String tag = "#" + hashEntity.getText();
                formattedMessage = formattedMessage.replace(tag, hashFormatter.format(new Object[]{query,tag}));
            }
        }
        
        UserMentionEntity[] userEntities = entity.getUserMentionEntities(); 
        if(userEntities != null) {
            for(UserMentionEntity userEntity : userEntities) {
                String query = userEntity.getScreenName();
                String screenName = "@" + userEntity.getScreenName();
                formattedMessage = formattedMessage.replace(screenName, userFormatter.format(new Object[]{query,screenName}));
            }
        }
                        
        return formattedMessage;
    }
    
}

@@ -297,17 +297,8 @@
  %22%3Ca
- href=%7B0%7D
  tar
@@ -299,32 +299,46 @@
 %3Ca target=_blank
+ href=%5C%22%7B0%7D%5C%22 
 %3E%7B1%7D%3C/a%3E%22;%0A    p
@@ -377,37 +377,53 @@
 _HASH_URL = %22%3Ca 
+target=_blank 
 href=
+%5C%22
 https://twitter.
@@ -441,32 +441,36 @@
 ch?q=%2523%7B0%7D&
+amp;
 src=typd
  target=_bla
@@ -453,38 +453,27 @@
 amp;src=typd
- target=_blank
+%5C%22 
 %3E%7B1%7D%3C/a%3E%22;%0A 
@@ -529,21 +529,37 @@
 L = %22%3Ca 
+target=_blank 
 href=
+%5C%22
 https://
@@ -591,24 +591,28 @@
 rom:%7B0%7D&
+amp;
 src=typd
  target=
@@ -603,30 +603,19 @@
 src=typd
- target=_blank
+%5C%22 
 %3E%7B1%7D%3C/a%3E

validate html of urls used int tweet items