package com.example.tracker;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    private Location currentLocation;
    private LocationListener locationListener;
    private LocationManager locationManager;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                if (location != null) {
                    currentLocation = location;
                    displayLocation();
                }
            }
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
        };

        Button startTracking = (Button) findViewById(R.id.buttonStartTracking);
        startTracking.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = getResources().getString(
                        R.string.startTrackingMessage);
                setStatusText(text);
            }
        });

        Button stopTracking = (Button) findViewById(R.id.buttonStopTracking);
        stopTracking.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = getResources().getString(
                        R.string.stopTrackingMessage);
                setStatusText(text);
            }
        });

        Button clearTracking = (Button) findViewById(R.id.buttonClearTracking);
        clearTracking.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = getResources().getString(
                        R.string.clearTrackingMessage);
                setStatusText(text);
            }
        });

        Button totalDistance = (Button) findViewById(R.id.buttonTotalDistanceTraveled);
        totalDistance.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                float totalMeters = 0.0F;
                String text = getMetersFormattedString(totalMeters);
                setStatusText(text);
            }
        });

        Button mapMovements = (Button) findViewById(R.id.buttonMapMovements);
        mapMovements.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void displayLocation() {
        if (currentLocation != null) {
            Resources resource = getResources();
            TextView tvLocation = 
                    (TextView) findViewById(R.id.textViewLocation);
            tvLocation.setText(resource.getString(R.string.location)
                    + " " + currentLocation.getLatitude()
                    + ", " + currentLocation.getLongitude());
        }
    }

    private String getMetersFormattedString(float meters) {
        String pattern = "###,###,##0.00";
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        String metersString = myFormatter.format(meters);

        Resources resources = getResources();
        String text = resources.getString(R.string.distance) + " "
                + metersString + " "
                + resources.getString(R.string.meters);
        return text;
    }

    private void setStatusText(String status) {
        TextView tvStatus = 
                (TextView) findViewById(R.id.textViewStatus);
        tvStatus.setText(status);
    }
}
