package tinyapps.apptemplate.base.activity;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.support.annotation.NonNull;
import android.util.Log;

import io.nlopez.smartlocation.OnLocationUpdatedListener;
import io.nlopez.smartlocation.SmartLocation;
import io.nlopez.smartlocation.location.providers.LocationGooglePlayServicesWithFallbackProvider;
import tinyapps.apptemplate.base.Constant;
import tinyapps.apptemplate.base.util.LocationUtil;

/**
 * Created by duongdt3 on 6/17/2016.
 */
public abstract class BaseGpsSupportActivity extends BaseActivity implements OnLocationUpdatedListener {
    SmartLocation.LocationControl locationControl;

    protected void checkGooglePlayServiceAvaiable() {
        LocationUtil.checkPlayServices(this, Constant.PLAY_SERVICES_RESOLUTION_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        checkResultLocationControl(requestCode, resultCode, data);
        checkResultGooglePlayService(requestCode, resultCode);
    }

    private void checkResultLocationControl(int requestCode, int resultCode, Intent data) {
        locationControl.onActivityResult(requestCode, resultCode, data);
    }

    private void checkResultGooglePlayService(int requestCode, int resultCode) {
        if (requestCode == Constant.PLAY_SERVICES_RESOLUTION_REQUEST) {
            if (resultCode == Activity.RESULT_OK) {
                onGooglePlayServiceResolutionOk();
            } else {
                onGooglePlayServiceResolutionFail();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        locationControl.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void onGooglePlayServiceResolutionFail() {
        //do nothing
    }

    private void onGooglePlayServiceResolutionOk() {
        //do nothing
    }

    @Override
    public void onLocationUpdated(Location location) {
        Log.d("BaseGpsSupportActivity", "onLocationUpdated " + location);
    }

    private void initGps() {
        locationControl = new SmartLocation.Builder(this)
                .logging(true)
                .build()
                .location(new LocationGooglePlayServicesWithFallbackProvider(this));
    }

    public void requestLocationOneFix() {
        locationControl.stop();
        locationControl.config(LocationUtil.BEST_QUANLITY)
                .oneFix().start(this);
    }

    public void requestLocationContinuous() {
        locationControl.stop();
        locationControl.config(LocationUtil.BEST_QUANLITY)
                .continuous().start(this);
    }

    @Override
    protected void initBeforeSetContentView() {
        super.initBeforeSetContentView();
        initGps();
    }

    @Override
    protected void onStop() {
        super.onStop();
        locationControl.stop();
    }
}

@@ -117,25 +117,88 @@
 oid.
-location.Location
+content.IntentSender;%0Aimport android.location.Location;%0Aimport android.os.Bundle
 ;%0Aim
@@ -264,16 +264,72 @@
 l.Log;%0A%0A
+import com.google.android.gms.common.ConnectionResult;%0A%0A
 import i
@@ -420,24 +420,110 @@
 rtLocation;%0A
+import io.nlopez.smartlocation.location.providers.LocationGooglePlayServicesProvider;%0A
 import io.nl
@@ -604,24 +604,89 @@
 ckProvider;%0A
+import io.nlopez.smartlocation.utils.GooglePlayServicesListener;%0A
 import tinya
@@ -916,16 +916,44 @@
 Listener
+, GooglePlayServicesListener
  %7B%0A    S
@@ -2636,24 +2636,173 @@
 initGps() %7B%0A
+        LocationGooglePlayServicesProvider provider = new LocationGooglePlayServicesProvider(this);%0A        provider.setCheckLocationSettings(true);%0A
         loca
@@ -2933,64 +2933,16 @@
 ion(
-new LocationGooglePlayServicesWithFallbackP
+p
 rovider
-(this)
 );%0A 
@@ -3507,24 +3507,24 @@
 r.onStop();%0A
-
         loca
@@ -3548,11 +3548,706 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void onConnected(Bundle bundle) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onConnectionSuspended(int i) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onConnectionFailed(ConnectionResult connectionResult) %7B%0A        if (connectionResult.hasResolution()) %7B%0A            try %7B%0A                connectionResult.startResolutionForResult(this, Constant.PLAY_SERVICES_RESOLUTION_REQUEST);%0A            %7D catch (IntentSender.SendIntentException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D else %7B%0A            checkGooglePlayServiceAvaiable();%0A            Log.e(%22BaseGpsSupportActivity%22, %22Google Play Service onConnectionFailed do not has Resolution%22);%0A        %7D%0A    %7D
 %0A%7D%0A

add support dialog error google play service