package fr.neamar.kiss.dataprovider;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fr.neamar.kiss.loader.LoadSearchPojos;
import fr.neamar.kiss.pojo.Pojo;
import fr.neamar.kiss.pojo.SearchPojo;

public class SearchProvider extends Provider<SearchPojo> {
    private SharedPreferences prefs;
    public static final String URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";

    private static final Map<String,String> searchProviderUrls = new LinkedHashMap<>();

    static {
        searchProviderUrls.put("Bing", "https://www.bing.com/search?q=");
        searchProviderUrls.put("DuckDuckGo", "https://duckduckgo.com/?q=");
        searchProviderUrls.put("Google", "https://encrypted.google.com/search?q=");
        searchProviderUrls.put("Qwant", "https://www.qwant.com/?q=");
        searchProviderUrls.put("StartPage", "https://startpage.com/do/search?language=cat=web&query=");
        searchProviderUrls.put("Wikipedia", "https://en.wikipedia.org/wiki/");
        searchProviderUrls.put("Yahoo", "https://search.yahoo.com/search?p=");
    }

    @Override
    public void reload() {
        this.initialize(new LoadSearchPojos(this));
    }

    public ArrayList<Pojo> getResults(String query) {

        ArrayList<Pojo> pojos = new ArrayList<>();
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            Set<String> selectedProviders = new TreeSet<>();
            selectedProviders.addAll(PreferenceManager.getDefaultSharedPreferences(this).getStringSet("search-providers", new HashSet<>(Arrays.asList("Google"))));
            for (String searchProvider : selectedProviders) {
                SearchPojo pojo = new SearchPojo();
                pojo.query = query;
                pojo.relevance = 10;
                pojo.url = searchProviderUrls.get(searchProvider);
                pojo.name = searchProvider;
                pojos.add(pojo);
            }
        }
        else {

            SearchPojo pojo = new SearchPojo();
            pojo.query = query;
            pojo.relevance = 10;
            pojo.name="Google";
            pojo.url = searchProviderUrls.get("Google");
            pojos.add(pojo);
        }

        Pattern p = Pattern.compile(URL_REGEX);
        Matcher m = p.matcher(query);//replace with string to compare
        if(m.find()) {
            SearchPojo pojo = new SearchPojo();
            pojo.relevance = 11;
            pojo.name = query;
            pojo.url = query;
            pojo.direct = true;
            pojos.add(pojo);
        }
        return pojos;
    }

    public static String[] getSearchProviders() {
        return searchProviderUrls.keySet().toArray(new String[searchProviderUrls.size()]);
    }
}

@@ -115,24 +115,84 @@
 ceManager;%0A%0A
+import java.net.MalformedURLException;%0Aimport java.net.URL;%0A
 import java.
@@ -2647,16 +2647,30 @@
 compare%0A
+        try %7B%0A
         
@@ -2724,32 +2724,61 @@
 w SearchPojo();%0A
+            pojo.query = %22%22;%0A
             pojo
@@ -2791,17 +2791,17 @@
 ance = 1
-1
+0
 ;%0A      
@@ -2848,21 +2848,55 @@
 o.url = 
-query
+new URL (%22https://%22 + query).toString()
 ;%0A      
@@ -2942,32 +2942,123 @@
 ojos.add(pojo);%0A
+            %7D%0A        %7D catch (MalformedURLException e) %7B%0A            e.printStackTrace();%0A
         %7D%0A      

Fix url malformation