package net.tevp.postcode;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.widget.TextView;
import android.location.Location;
import android.widget.Button;
import android.view.View;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Postcode extends Activity implements PostcodeListener {	
	TextView tv;
	PostcodeBackend pb;
	String lastPostcode = null;
	PostcodeState ps = null;
	boolean acquiring = false;

	private class PostcodeState
	{
		public PostcodeBackend pb;
		public String text;
		public Date timestamp;
		public boolean acquiring;
	}

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.Postcode);

		ps = (PostcodeState) getLastNonConfigurationInstance();
		final Postcode self = this;
		((Button) findViewById(R.id.btnUpdate)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				self.newPostcode(true);
			}
		});
    }

	private void newPostcode(boolean mustBeNew)
	{
		if (lastPostcode!= null)
			setText(lastPostcode+"\n(updating...)");
		else
			setText("Finding location..");
		acquiring = true;
		pb.getPostcode(this,this,mustBeNew);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		if (ps == null || ps.pb == null)
			pb = new PostcodeBackend();
		else
			pb = ps.pb;
		
		if (ps == null
				|| ps.acquiring
				|| (new Date().getTime()-ps.timestamp.getTime()) > 300 * 1000 // more than 5 minutes old
				)
			newPostcode(false);
		else
			setText(ps.text);
	}

	public static void main(String[] args) throws Exception {
		String[] coords = args[0].split(",");
		assert coords.length == 2;
		System.out.println(PostcodeBackend.get(Double.parseDouble(coords[0]),Double.parseDouble(coords[1])));
	}

	private void setText(final CharSequence text)
	{
		final TextView myTV = tv;		
		runOnUiThread(new Runnable()
		{
			public void run()
			{
				myTV.setText(text);
			}
		});
	}

	public void postcodeChange(String postcode)
	{
		lastPostcode = postcode;
		setText(String.format("%s\n(@ %s)", postcode, new SimpleDateFormat("HH:mm:ss").format(new Date())));
		acquiring = false;
	}

	public void updatedLocation(Location l)
	{
		if (lastPostcode!= null)
			setText(lastPostcode+"\nFound new location, looking for postcode...");
		else
			setText("Found location, looking for postcode...");
	}

	public void locationFindFail()
	{
		acquiring = false;
		setText("Can't find location. Enable GPS, or wait until you're outside");
	}

	public void postcodeLookupFail()
	{
		acquiring = false;
		setText("Failed to lookup postcode. Please check you've got a working internet connection");
	}

	@Override
	public Object onRetainNonConfigurationInstance()
	{
		PostcodeState ps = new PostcodeState();
		ps.pb = pb;
		ps.text = tv.getText().toString();
		ps.timestamp = new Date();
		ps.acquiring = acquiring;
		return ps;
	}
}

@@ -540,16 +540,46 @@
 end pb;%0A
+%09%09public String lastPostcode;%0A
 %09%09public
@@ -1695,32 +1695,71 @@
 (false);%0A%09%09else%0A
+%09%09%7B%0A%09%09%09lastPostcode = ps.lastPostcode;%0A
 %09%09%09setText(ps.te
@@ -1763,16 +1763,20 @@
 .text);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09pub
@@ -3108,16 +3108,50 @@
 uiring;%0A
+%09%09ps.lastPostcode = lastPostcode;%0A
 %09%09return

Save last postcode in PostcodeState