package cgeo.geocaching.maps;

import cgeo.geocaching.R;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.maps.google.GoogleMapProvider;
import cgeo.geocaching.maps.interfaces.MapProvider;
import cgeo.geocaching.maps.interfaces.MapSource;
import cgeo.geocaching.maps.mapsforge.MapsforgeMapProvider;

import android.view.Menu;
import android.view.SubMenu;

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

public class MapProviderFactory {

    private final static ArrayList<MapSource> mapSources = new ArrayList<MapSource>();

    static {
        // add GoogleMapProvider only if google api is available in order to support x86 android emulator
        if (isGoogleMapsInstalled()) {
            GoogleMapProvider.getInstance();
        }
        MapsforgeMapProvider.getInstance();
    }

    public static boolean isGoogleMapsInstalled() {
        boolean googleMaps = true;
        try {
            Class.forName("com.google.android.maps.MapActivity");
        } catch (ClassNotFoundException e) {
            googleMaps = false;
        }
        return googleMaps;
    }

    public static List<MapSource> getMapSources() {
        return mapSources;
    }

    public static boolean isSameActivity(final MapSource source1, final MapSource source2) {
        final MapProvider provider1 = source1.getMapProvider();
        final MapProvider provider2 = source2.getMapProvider();
        return provider1 == provider2 && provider1.isSameActivity(source1, source2);
    }

    public static void addMapviewMenuItems(Menu menu) {
        final SubMenu parentMenu = menu.findItem(R.id.menu_select_mapview).getSubMenu();

        final int currentSource = Settings.getMapSource().getNumericalId();
        for (int i = 0; i < mapSources.size(); i++) {
            final MapSource mapSource = mapSources.get(i);
            final int id = mapSource.getNumericalId();
            parentMenu.add(R.id.menu_group_map_sources, id, i, mapSource.getName()).setCheckable(true).setChecked(id == currentSource);
        }
        parentMenu.setGroupCheckable(R.id.menu_group_map_sources, true, true);
    }

    public static MapSource getMapSource(int id) {
        for (MapSource mapSource : mapSources) {
            if (mapSource.getNumericalId() == id) {
                return mapSource;
            }
        }
        return null;
    }

    public static void registerMapSource(final MapSource mapSource) {
        mapSources.add(mapSource);
    }

    public static MapSource getDefaultSource() {
        return mapSources.get(0);
    }

    /**
     * remove offline map sources after changes of the settings
     */
    public static void deleteOfflineMapSources() {
        final ArrayList<MapSource> deletion = new ArrayList<MapSource>();
        for (MapSource mapSource : mapSources) {
            if (mapSource instanceof MapsforgeMapProvider.OfflineMapSource) {
                deletion.add(mapSource);
            }
        }
        mapSources.removeAll(deletion);
    }
}

@@ -77,25 +77,23 @@
 ing.
-settings.Settings
+cgeoapplication
 ;%0Aim
@@ -305,16 +305,104 @@
 rovider;
+%0Aimport cgeo.geocaching.settings.Settings;%0A%0Aimport org.apache.commons.lang3.StringUtils;
 %0A%0Aimport
@@ -938,24 +938,65 @@
 stalled() %7B%0A
+        // Check if API key is available%0A
         bool
@@ -1016,13 +1016,133 @@
 s = 
+S
 tr
-ue;
+ingUtils.isNotBlank(cgeoapplication.getInstance().getString(R.string.maps_api_key));%0A%0A        // Check if API is available
 %0A   
@@ -1295,16 +1295,16 @@
  false;%0A
-
         
@@ -1297,32 +1297,33 @@
 alse;%0A        %7D%0A
+%0A
         return g

Disable Google Maps if no API key set