package org.stepic.droid.util;

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

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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();
    }

    private static final Pattern urlPattern = Pattern.compile(
            "(?:^|[\\W])((ht|f)tp(s?):\\/\\/|www\\.)"
                    + "(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*"
                    + "[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);


    //Pull all links from the body for easy retrieval
    public static List<String> pullLinks(String textHtml) {
        String text = HtmlHelper.fromHtml(textHtml).toString();
        List<String> links = new ArrayList<>();

        Matcher m = urlPattern.matcher(text);
        while(m.find()) {
            String urlStr = m.group();
            if (urlStr.startsWith("(") && urlStr.endsWith(")")) {
                urlStr = urlStr.substring(1, urlStr.length() - 1);
            }
            urlStr = urlStr.trim();
            links.add(urlStr);
        }
        return links;
    }

}

@@ -25,16 +25,47 @@
 .util;%0A%0A
+import android.util.Patterns;%0A%0A
 import o
@@ -236,40 +236,8 @@
 her;
-%0Aimport java.util.regex.Pattern;
 %0A%0Apu
@@ -2104,24 +2104,26 @@
 g();%0A    %7D%0A%0A
+//
     private 
@@ -2173,16 +2173,18 @@
 ompile(%0A
+//
         
@@ -2229,16 +2229,18 @@
 ww%5C%5C.)%22%0A
+//
         
@@ -2294,16 +2294,18 @@
 %5C%5C/?)*%22%0A
+//
         
@@ -2369,16 +2369,18 @@
 ;'%5D*)%22,%0A
+//
         
@@ -2696,26 +2696,32 @@
 her m = 
-url
 Pattern
+s.WEB_URL
 .matcher

move to Android util parsing, it is more powerful way