package de.danoeh.antennapod.discovery;


import io.reactivex.Single;

import java.util.ArrayList;
import java.util.List;

public class PodcastSearcherRegistry {
    private static List<SearcherInfo> searchProviders;

    private PodcastSearcherRegistry() {
    }

    public static List<SearcherInfo> getSearchProviders() {
        if (searchProviders == null) {
            searchProviders = new ArrayList<>();
            searchProviders.add(new SearcherInfo(new CombinedSearcher(), 1.f));
            searchProviders.add(new SearcherInfo(new ItunesPodcastSearcher(), 1.f));
            searchProviders.add(new SearcherInfo(new FyydPodcastSearcher(), 1.f));
            searchProviders.add(new SearcherInfo(new GpodnetPodcastSearcher(), 0.0f));
            searchProviders.add(new SearcherInfo(new PodcastIndexPodcastSearcher(), 0.0f));
        }
        return searchProviders;
    }

    public static Single<String> lookupUrl(String url) {
        for (PodcastSearcherRegistry.SearcherInfo searchProviderInfo : getSearchProviders()) {
            if (searchProviderInfo.searcher.getClass() != CombinedSearcher.class
                    && searchProviderInfo.searcher.urlNeedsLookup(url)) {
                return searchProviderInfo.searcher.lookupUrl(url);
            }
        }
        return Single.just(url);
    }

    public static boolean urlNeedsLookup(String url) {
        for (PodcastSearcherRegistry.SearcherInfo searchProviderInfo : getSearchProviders()) {
            if (searchProviderInfo.searcher.getClass() != CombinedSearcher.class
                    && searchProviderInfo.searcher.urlNeedsLookup(url)) {
                return true;
            }
        }
        return false;
    }

    public static class SearcherInfo {
        public final PodcastSearcher searcher;
        public final float weight;

        public SearcherInfo(PodcastSearcher searcher, float weight) {
            this.searcher = searcher;
            this.weight = weight;
        }
    }
}

@@ -473,32 +473,33 @@
 edSearcher(), 1.
+0
 f));%0A           
@@ -544,14 +544,15 @@
 new 
-Itunes
+Gpodnet
 Podc
@@ -558,34 +558,35 @@
 castSearcher(), 
-1.
+0.0
 f));%0A           
@@ -652,16 +652,17 @@
 er(), 1.
+0
 f));%0A   
@@ -703,39 +703,38 @@
 earcherInfo(new 
-Gpodnet
+Itunes
 PodcastSearcher(
@@ -728,33 +728,33 @@
 castSearcher(), 
-0
+1
 .0f));%0A         
@@ -820,33 +820,33 @@
 castSearcher(), 
-0
+1
 .0f));%0A        %7D

Add Podcastindex into main search (#4873)