package com.dreiri.stolpersteine.activities;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.dreiri.stolpersteine.R;
import com.dreiri.stolpersteine.api.model.Location;
import com.dreiri.stolpersteine.api.model.Person;
import com.dreiri.stolpersteine.api.model.Stolperstein;

public class InfoActivity extends Activity {

    ArrayList<Stolperstein> stolpersteine;
    Person person;
    Location location;
    String bioUrl;
    String bioData;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.fragment_info);
        
        Intent intent = getIntent();
        if (intent.hasExtra("stolpersteine")) {
            stolpersteine = intent.getParcelableArrayListExtra("stolpersteine");
            readProperties(stolpersteine.get(0));
        }
        
        TextView textViewName = (TextView) findViewById(R.id.textViewName);
        TextView textViewAddress = (TextView) findViewById(R.id.textViewAddress);
        final Button btnBio = (Button) findViewById(R.id.btnBio);
        
        textViewName.setText(person.getNameAsString());
        textViewAddress.setText(location.getAddressAsString());
//        UIEnhancer.insertImageIntoTextView(this, R.drawable.img_doughface, R.id.textViewBio);
        
        // pre-caching
        Thread downloadThread = new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Document document = Jsoup.parse(new URL(bioUrl).openStream(), "utf-8", bioUrl);
                    Elements elements = document.select("div#biografie_seite");
                    bioData = elements.toString();
                    btnBio.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(InfoActivity.this, BioActivity.class);
                            intent.putExtra("bioData", bioData);
                            intent.putExtra("bioUrl", bioUrl);
                            startActivity(intent);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        downloadThread.start();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.info, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
        }
        return true;
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList("stolpersteine", stolpersteine);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null && savedInstanceState.containsKey("stolpersteine")) {
            stolpersteine = savedInstanceState.getParcelableArrayList("stolpersteine");
            readProperties(stolpersteine.get(0));
        }
    }
    
    private void readProperties(Stolperstein stolperstein) {
        person = stolperstein.getPerson();
        location = stolperstein.getLocation();
        bioUrl = person.getBiographyUri().toString();
    }

}
@@ -3257,32 +3257,34 @@
 rue;%0A    %7D%0A    %0A
+//
     @Override%0A  
@@ -3273,32 +3273,34 @@
 //    @Override%0A
+//
     protected vo
@@ -3333,32 +3333,34 @@
 dle outState) %7B%0A
+//
         super.on
@@ -3380,32 +3380,96 @@
 tate(outState);%0A
+//        Log.i(%22Stolpersteine%22, %22onSaveInstanceState Info%22);%0A//
         outState
@@ -3525,27 +3525,33 @@
 teine);%0A
+//
     %7D%0A
+//
     %0A
+//
     @Ove
@@ -3548,32 +3548,34 @@
 //    @Override%0A
+//
     protected vo
@@ -3621,32 +3621,34 @@
 nstanceState) %7B%0A
+//
         super.on
@@ -3681,32 +3681,99 @@
 InstanceState);%0A
+//        Log.i(%22Stolpersteine%22, %22onRestoreInstanceState Info%22);%0A//
         if (save
@@ -3841,32 +3841,34 @@
 lpersteine%22)) %7B%0A
+//
             stol
@@ -3931,32 +3931,34 @@
 tolpersteine%22);%0A
+//
             read
@@ -3983,32 +3983,34 @@
 steine.get(0));%0A
+//
         %7D%0A    %7D%0A
@@ -3999,24 +3999,26 @@
 //        %7D%0A
+//
     %7D%0A    %0A 

Disable saving/restoring instance data as it doesn't seem necessary