package com.appdynamics.demo.gasp.fragment;

import android.app.Fragment;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.GooglePlayServicesUtil;

/**
 * Copyright (c) 2013 Mark Prichard
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class LocationFragment extends Fragment implements LocationListener {
    private static final String TAG = LocationFragment.class.getName();
    private static LocationManager locationManager;
    private static String provider;

    public static Location getLocation(Context context) {
        Location location = null;

        try {
            if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == 0) {
                locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                criteria.setPowerRequirement(Criteria.POWER_LOW);
                criteria.setAltitudeRequired(false);
                criteria.setBearingRequired(false);
                criteria.setSpeedRequired(false);
                criteria.setCostAllowed(true);

                provider = locationManager.getBestProvider(criteria, true);
                location = locationManager.getLastKnownLocation(provider);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    @Override
    public void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 500, 50, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        try {
            String locationMessage = "Location: " + String.format("%.6f", location.getLatitude())
                    + ", " + String.format("%.6f", location.getLongitude())
                    + " (via " + location.getProvider() + ")";
            Log.d(TAG, locationMessage);
            //Toast.makeText(getActivity(), locationMessage, Toast.LENGTH_SHORT).show();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.i(TAG, "Enabled provider: " + provider);
        //Toast.makeText(getActivity(), "Enabled provider " + provider, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.i(TAG, "Disabled provider: " + provider);
        //Toast.makeText(getActivity(), "Disabled provider: " + provider, Toast.LENGTH_SHORT).show();
    }
}
