package mobi.tarantio.tools.sharinger.app;

import android.content.Intent;

import static android.content.Intent.EXTRA_SUBJECT;
import static android.content.Intent.EXTRA_TEXT;

/**
 * Created by kolipass on 23.02.14.
 */
public class IntentHandler {

    private String divider;

    public IntentHandler(String divider) {
        this.divider = divider;
    }

    public String handleSendText(Intent intent) {
        String sharedBodyText = intent.getStringExtra(EXTRA_TEXT);
        String sharedTitleText = intent.getStringExtra(EXTRA_SUBJECT);

        String rawText = "";
        if (sharedBodyText != null || sharedTitleText != null) {
            String title = sharedTitleText != null && sharedBodyText != null && !sharedBodyText.contains(sharedTitleText) ? sharedTitleText : null;
            String body = sharedBodyText != null ? sharedBodyText : null;
            body = checkBracketsCloseUrl(body);

            rawText = title != null ? title : "";
            if (rawText.length() > 0) {
                rawText += divider;
            }
            rawText += body != null ? body : "";
        }
        return rawText;
    }

    /**
     * Метод нужен для добавления пробела перед закрывающей скобкой
     *
     * @param body текст сообщения
     * @return верент измененный body
     */

    public String checkBracketsCloseUrl(String body) {
        if (body != null && body.length() > 0) {
            body =  addDivider(body,"(", false);
            body =  addDivider(body,")", true);
        }
        return body;
    }

    private String addDivider(String body, String brackets, boolean before) {
        int pos = body.indexOf(brackets);
        while (pos != -1) {
            if (before) {
                body = body.substring(0, pos) + divider + body.substring(pos, body.length());
            } else {
                body = body.substring(0, pos + 1) + divider + body.substring(pos + 1, body.length());
            }

            pos = body.indexOf(brackets, pos + divider.length() + 1);
        }
        return body;
    }
}

@@ -69,16 +69,81 @@
 ntent;%0A%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 import s
@@ -1425,24 +1425,25 @@
 body) %7B%0A
+%0A
         
 if (body
@@ -1438,49 +1438,327 @@
     
-if (body != null && body.length() %3E 0
+String regex = %22%5C%5C(?%5C%5Cb(http://%7Cwww%5B.%5D)%5B-A-Za-z0-9+&amp;@#/%25?=~_()%7C!:,.;%5D*%5B-A-Za-z0-9+&amp;@#/%25=~_()%7C%5D%22;%0A        Pattern p = Pattern.compile(regex);%0A        Matcher m = p.matcher(body);%0A        while (m.find()) %7B%0A            String urlStr = m.group();%0A            if (urlStr.startsWith(%22(%22) && urlStr.endsWith(%22)%22)
 ) %7B%0A
+%0A
     
@@ -1769,92 +1769,184 @@
     
-body =  addDivider(body,%22(%22, false);%0A            body =  addDivider(body,%22)%22, true);
+    String urlWithoutBrackets = urlStr.substring(1, urlStr.length() - 1);%0A                body = body.replace(urlWithoutBrackets, addDivider(urlWithoutBrackets));%0A            %7D
 %0A   
@@ -1944,32 +1944,33 @@
     %7D%0A        %7D%0A
+%0A
         return b
@@ -2026,198 +2026,39 @@
 body
-, String brackets, boolean before) %7B%0A        int pos = body.indexOf(brackets);%0A        while (pos != -1) %7B%0A            if (before) %7B%0A                body = body.substring(0, pos) + divider +
+) %7B%0A        String tempString =
  bod
@@ -2069,19 +2069,17 @@
 bstring(
-pos
+1
 , body.l
@@ -2077,32 +2077,36 @@
 1, body.length()
+ - 1
 );%0A            %7D
@@ -2104,237 +2104,45 @@
     
- 
-   %7D else %7B%0A                body = body.substring(0, pos + 1) + divider + body.substring(pos + 1, body.length());%0A            %7D%0A%0A            pos = body.indexOf(brackets, pos + divider.length() + 1);%0A        %7D%0A        return body
+return divider + tempString + divider
 ;%0A  

fix #12