package cgeo.geocaching.utils;

import cgeo.calendar.CalendarAddon;
import cgeo.contacts.ContactsAddon;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.capability.ILogin;
import cgeo.geocaching.sensors.OrientationProvider;
import cgeo.geocaching.sensors.RotationProvider;
import cgeo.geocaching.sensors.Sensors;
import cgeo.geocaching.settings.Settings;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;

import android.content.Context;
import android.os.Build;
import android.os.Build.VERSION;

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

public final class SystemInformation {

    @NonNull
    public static String getSystemInformation(final Context context) {
        final boolean googlePlayServicesAvailable = CgeoApplication.getInstance().isGooglePlayServicesAvailable();
        final StringBuilder body = new StringBuilder("--- System information ---")
                .append("\nDevice: ").append(Build.MODEL).append(" (").append(Build.PRODUCT).append(", ").append(Build.BRAND).append(")")
                .append("\nAndroid version: ").append(VERSION.RELEASE)
                .append("\nAndroid build: ").append(Build.DISPLAY)
                .append("\nCgeo version: ").append(Version.getVersionName(context))
                .append("\nGoogle Play services: ").append(googlePlayServicesAvailable ? (Settings.useGooglePlayServices() ? "enabled" : "disabled") : "unavailable")
                .append("\nLow power mode: ").append(Settings.useLowPowerMode() ? "active" : "inactive")
                .append("\nCompass capabilities: ").append(Sensors.getInstance().hasCompassCapabilities() ? "yes" : "no")
                .append("\nRotation sensor: ").append(SystemInformation.presence(RotationProvider.hasRotationSensor(context)))
                .append("\nGeomagnetic rotation sensor: ").append(SystemInformation.presence(RotationProvider.hasGeomagneticRotationSensor(context)))
                .append("\nOrientation sensor: ").append(SystemInformation.presence(OrientationProvider.hasOrientationSensor(context)))
                .append("\nHide own/found: ").append(Settings.isExcludeMyCaches())
                .append("\nMap strategy: ").append(Settings.getLiveMapStrategy().toString().toLowerCase(Locale.getDefault()))
                .append("\nHW acceleration: ").append(Settings.useHardwareAcceleration() ? "enabled" : "disabled")
                .append(" (").append(Settings.useHardwareAcceleration() != Settings.HW_ACCEL_DISABLED_BY_DEFAULT ? "default state" : "manually changed").append(")");
        final StringBuilder connectors = new StringBuilder();
        int connectorCount = 0;
        for (final ILogin connector : ConnectorFactory.getActiveLiveConnectors()) {
            connectorCount++;
            connectors.append("\n - ").append(connector.getName()).append(": ").append(connector.isLoggedIn() ? "logged in" : "not logged in")
                    .append(" (").append(connector.getLoginStatusString()).append(')');
            if (connector.getName().equals("geocaching.com") && connector.isLoggedIn()) {
                connectors.append(" / ").append(Settings.getGCMemberStatus());
            }
        }
        body.append("\nGeocaching sites enabled:").append(connectorCount > 0 ? connectors : " none")
                .append("\nSystem language: ").append(Locale.getDefault());
        if (Settings.useEnglish()) {
            body.append(" (cgeo forced to English)");
        }
        appendAddons(body);
        body.append("\n--- End of system information ---\n");
        return body.toString();
    }

    private static void appendAddons(@NonNull final StringBuilder body) {
        final List<String> addons = new ArrayList<>();
        if (CalendarAddon.isAvailable()) {
            addons.add("calendar");
        }
        if (ContactsAddon.isAvailable()) {
            addons.add("contacts");
        }
        body.append("\nInstalled cgeo plugins: ");
        if (CollectionUtils.isNotEmpty(addons)) {
            body.append(StringUtils.join(addons, ", "));
        } else {
            body.append(" none");
        }
    }

    public static String presence(final boolean present) {
        return present ? "present" : "absent";
    }
}

@@ -188,16 +188,61 @@
 actory;%0A
+import cgeo.geocaching.connector.IConnector;%0A
 import c
@@ -285,16 +285,16 @@
 ILogin;%0A
-
 import c
@@ -881,16 +881,25 @@
 rmation(
+@NonNull 
 final Co
@@ -2771,742 +2771,8 @@
 %22)%22)
-;%0A        final StringBuilder connectors = new StringBuilder();%0A        int connectorCount = 0;%0A        for (final ILogin connector : ConnectorFactory.getActiveLiveConnectors()) %7B%0A            connectorCount++;%0A            connectors.append(%22%5Cn - %22).append(connector.getName()).append(%22: %22).append(connector.isLoggedIn() ? %22logged in%22 : %22not logged in%22)%0A                    .append(%22 (%22).append(connector.getLoginStatusString()).append(')');%0A            if (connector.getName().equals(%22geocaching.com%22) && connector.isLoggedIn()) %7B%0A                connectors.append(%22 / %22).append(Settings.getGCMemberStatus());%0A            %7D%0A        %7D%0A        body.append(%22%5CnGeocaching sites enabled:%22).append(connectorCount %3E 0 ? connectors : %22 none%22)
 %0A   
@@ -2937,32 +2937,64 @@
 h)%22);%0A        %7D%0A
+        appendConnectors(body);%0A
         appendAd
@@ -3063,24 +3063,24 @@
 on ---%5Cn%22);%0A
-
         retu
@@ -3102,24 +3102,1077 @@
 g();%0A    %7D%0A%0A
+    private static void appendConnectors(@NonNull final StringBuilder body) %7B%0A        final StringBuilder connectors = new StringBuilder();%0A        int connectorCount = 0;%0A        for (final IConnector connector : ConnectorFactory.getConnectors()) %7B%0A            if (connector.isActive()) %7B%0A                connectorCount++;%0A                connectors.append(%22%5Cn - %22).append(connector.getName());%0A                if (connector instanceof ILogin) %7B%0A                    final ILogin login = (ILogin) connector;%0A                    connectors.append(%22: %22).append(login.isLoggedIn() ? %22logged in%22 : %22not logged in%22)%0A                            .append(%22 (%22).append(login.getLoginStatusString()).append(')');%0A                    if (login.getName().equals(%22geocaching.com%22) && login.isLoggedIn()) %7B%0A                        connectors.append(%22 / %22).append(Settings.getGCMemberStatus());%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        body.append(%22%5CnGeocaching sites enabled:%22).append(connectorCount %3E 0 ? connectors : %22 none%22);%0A    %7D%0A%0A
     private 

fix #4856: opencaching.com not listed in system information