package io.rumors.reactnativesettings.handlers;

import android.location.LocationManager;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import io.rumors.reactnativesettings.Constants;

public class LocationSettingsHandler implements SettingsHandler<String> {
  private Context mContext;

  public LocationSettingsHandler(Context context) {
    this.mContext = context;
  }

  public static int getLocationMode(Context context) {
    int locationMode = 0;
    String locationProviders;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      try {
        locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

      } catch (Settings.SettingNotFoundException e) {
        e.printStackTrace();
      }


    } else {
      locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

      if (TextUtils.isEmpty(locationProviders)) {
        locationMode = Settings.Secure.LOCATION_MODE_OFF;
      }
      else if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)) {
        locationMode = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
      }
      else if (locationProviders.contains(LocationManager.GPS_PROVIDER)) {
        locationMode = Settings.Secure.LOCATION_MODE_SENSORS_ONLY;
      }
      else if (locationProviders.contains(LocationManager.NETWORK_PROVIDER)) {
        locationMode = Settings.Secure.LOCATION_MODE_BATTERY_SAVING;
      }

    }

    return locationMode;
  }

  public String getSetting() {
    if(getLocationMode(mContext) == Settings.Secure.LOCATION_MODE_OFF)
    {
      return Constants.DISABLED;
    }
    else
      return Constants.ENABLED;

  }
}

@@ -526,9 +526,41 @@
 e = 
-0
+Settings.Secure.LOCATION_MODE_OFF
 ;%0A  
@@ -771,17 +771,16 @@
 _MODE);%0A
-%0A
       %7D 
@@ -862,18 +862,16 @@
       %7D%0A
-%0A%0A
     %7D el

initialize location mode with const value