package org.stepic.droid.util;

import org.stepic.droid.base.MainApplication;
import org.stepic.droid.configuration.IConfig;

public class StringUtil {
    public static Double safetyParseString(String str) {
        Double doubleScore = null;
        try {
            doubleScore = Double.parseDouble(str);

        } catch (Exception ignored) {
        }
        return doubleScore;
    }

    public static String getUriForCourse(String baseUrl, String slug){
        StringBuilder stringBuilder =new StringBuilder();
        stringBuilder.append(baseUrl);
        stringBuilder.append(AppConstants.WEB_URI_SEPARATOR);
        stringBuilder.append("course");
        stringBuilder.append(AppConstants.WEB_URI_SEPARATOR);
        stringBuilder.append(slug);
        stringBuilder.append(AppConstants.WEB_URI_SEPARATOR);
        return stringBuilder.toString();
    }

    public static String getDynamicLinkForCourse(IConfig config, String slug){
        String firebaseDomain = config.getFirebaseDomain();
        if (firebaseDomain == null){
            return getUriForCourse(config.getBaseUrl(), slug);
        }


        StringBuilder stringBuilder =new StringBuilder();
        stringBuilder.append(firebaseDomain);
        stringBuilder.append("?link=");
        stringBuilder.append(config.getBaseUrl());
        stringBuilder.append(AppConstants.WEB_URI_SEPARATOR);
        stringBuilder.append("course");
        stringBuilder.append(AppConstants.WEB_URI_SEPARATOR);
        stringBuilder.append(slug);
        stringBuilder.append(AppConstants.WEB_URI_SEPARATOR);
        stringBuilder.append("&amv=650");
        stringBuilder.append("&apn=");
        String packageName = MainApplication.getAppContext().getPackageName();
        if (packageName == null){
            return getUriForCourse(config.getBaseUrl(), slug);
        }
        stringBuilder.append(packageName);

//        stringBuilder.append("&ibi=com.AlexKarpov.Stepic");
        return stringBuilder.toString();
    }

}

@@ -119,16 +119,132 @@
 onfig;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 public c
@@ -2109,10 +2109,637 @@
 %0A    %7D%0A%0A
+    //Pull all links from the body for easy retrieval%0A    public static List%3CString%3E pullLinks(String text) %7B%0A        List%3CString%3E links = new ArrayList%3C%3E();%0A%0A        String regex = %22%5C%5C(?%5C%5Cb(http://%7Cwww%5B.%5D)%5B-A-Za-z0-9+&@#/%25?=~_()%7C!:,.;%5D*%5B-A-Za-z0-9+&@#/%25=~_()%7C%5D%22;%0A        Pattern p = Pattern.compile(regex);%0A        Matcher m = p.matcher(text);%0A        while(m.find()) %7B%0A            String urlStr = m.group();%0A            if (urlStr.startsWith(%22(%22) && urlStr.endsWith(%22)%22)) %7B%0A                urlStr = urlStr.substring(1, urlStr.length() - 1);%0A            %7D%0A            links.add(urlStr);%0A        %7D%0A        return links;%0A    %7D%0A%0A
 %7D%0A

add pull links to util