package com.mobilevangelist.glass.kitchensink;

import android.content.Context;
import android.graphics.Canvas;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Date;

/**
 * Render the GPS view.
 */
public class GPSRenderer implements SurfaceHolder.Callback, LocationListener {
  private SurfaceHolder _holder;

  private RelativeLayout _gpsLayout;
  private TextView _waitingTextView;
  private RelativeLayout _coordinateLayout;
  private TextView _longitudeTextView;
  private TextView _latitudeTextView;
  private GPSThread _gps;

  private Context _context;

  public GPSRenderer(Context context) {
    android.util.Log.d("GPSRenderer", "In GPSRenderer constructor()");

    LayoutInflater inflater = LayoutInflater.from(context);
    _gpsLayout = (RelativeLayout) inflater.inflate(R.layout.layout_gps, null);

    _waitingTextView = (TextView) _gpsLayout.findViewById(R.id.waitingTextView);
    _coordinateLayout = (RelativeLayout) _gpsLayout.findViewById(R.id.coordinateLayout);
    _coordinateLayout.setVisibility(View.INVISIBLE);

    _longitudeTextView = (TextView) _gpsLayout.findViewById(R.id.longitudeTextView);
    _latitudeTextView = (TextView) _gpsLayout.findViewById(R.id.latitudeTextView);

    _context = context;
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    android.util.Log.d("GPSRenderer", "In surfaceCreated()");

    _holder = holder;

    _gps = new GPSThread(_context, this);
    _gps.start();
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    android.util.Log.d("GPSRenderer", "In surfaceChanged()");

    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

    _gpsLayout.measure(measuredWidth, measuredHeight);
    _gpsLayout.layout(0, 0, _gpsLayout.getMeasuredWidth(), _gpsLayout.getMeasuredHeight());

    draw();
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    _gps.haltUpdates();
  }

  @Override
  public void onLocationChanged(Location location) {
    long locationTime = location.getTime();
    android.util.Log.d("GPSRenderer", "location provider: " + location.getProvider());
    android.util.Log.d("GPSRenderer", "location.latitude: " + location.getLatitude());
    android.util.Log.d("GPSRenderer", "location.longitude: " + location.getLongitude());
    android.util.Log.d("GPSRenderer", "location time: " + new Date(locationTime));
    android.util.Log.d("GPSRenderer", "");

    _waitingTextView.setVisibility(View.INVISIBLE);
    _coordinateLayout.setVisibility(View.VISIBLE);

    _longitudeTextView.setText(String.valueOf(location.getLongitude()));
    _latitudeTextView.setText(String.valueOf(location.getLatitude()));

    draw();
  }

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

  }

  @Override
  public void onProviderEnabled(String provider) {

  }

  @Override
  public void onProviderDisabled(String provider) {

  }

  private  void draw() {
    Canvas canvas = null;

    try {
      canvas = _holder.lockCanvas();
    }
    catch (RuntimeException e) {
      android.util.Log.d("GPSRenderer", "RuntimeException", e);
    }

    if (canvas != null) {
      _gpsLayout.draw(canvas);

      try {
        _holder.unlockCanvasAndPost(canvas);
      }
      catch (RuntimeException e) {
        Log.d("GPSRenderer", "RuntimeException: ", e);
      }
    }
  }
}

@@ -708,19 +708,18 @@
 tView _l
-ong
+at
 itudeTex
@@ -738,34 +738,35 @@
 vate TextView _l
-at
+ong
 itudeTextView;%0A 
@@ -1294,35 +1294,34 @@
 ISIBLE);%0A%0A    _l
-ong
+at
 itudeTextView = 
@@ -1361,19 +1361,18 @@
 d(R.id.l
-ong
+at
 itudeTex
@@ -1377,34 +1377,35 @@
 extView);%0A    _l
-at
+ong
 itudeTextView = 
@@ -1437,34 +1437,35 @@
 dViewById(R.id.l
-at
+ong
 itudeTextView);%0A
@@ -2924,27 +2924,26 @@
 LE);%0A%0A    _l
-ong
+at
 itudeTextVie
@@ -2972,35 +2972,34 @@
 Of(location.getL
-ong
+at
 itude()));%0A    _
@@ -2995,26 +2995,27 @@
 ()));%0A    _l
-at
+ong
 itudeTextVie
@@ -3044,34 +3044,35 @@
 Of(location.getL
-at
+ong
 itude()));%0A%0A    

Put latitude first.