package de.evosec.fotilo;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;

public class ReviewPicturesActivity extends AppCompatActivity
        implements View.OnClickListener {

	private static final Logger LOG =
	        LoggerFactory.getLogger(ReviewPicturesActivity.class);

	private ArrayList<String> pictureUris;
	private Button btn_ok;
	private Button btn_delete;
	private Button btn_fertig;
	private ImageAdapter imageAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_review_pictures);
		Bundle bundle = getIntent().getBundleExtra("data");
		pictureUris = bundle.getStringArrayList("pictures");
		final GridView pictureGrid = (GridView) findViewById(R.id.pictureGrid);
		this.btn_delete = (Button) findViewById(R.id.btn_delete);
		this.btn_ok = (Button) findViewById(R.id.btn_ok);
		this.btn_fertig = (Button) findViewById(R.id.btn_fertig);
		btn_delete.setOnClickListener(this);
		btn_ok.setOnClickListener(this);
		btn_fertig.setOnClickListener(this);
		imageAdapter = new ImageAdapter(this, getContentResolver(), pictureUris,
		    btn_delete);
		pictureGrid.setAdapter(imageAdapter);
		pictureGrid.setOnItemClickListener(imageAdapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_delete:
			deletePictures();
			break;
		case R.id.btn_ok:
			returnPictures(Activity.RESULT_OK);
			break;
		case R.id.btn_fertig:
			returnPictures(Activity.RESULT_FIRST_USER);
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			returnPictures(Activity.RESULT_OK);
		}
		return super.onKeyDown(keyCode, event);
	}

	private void returnPictures(int resultCode) {
		Intent resIntent = new Intent();
		Bundle resBundle = new Bundle();
		resBundle.putStringArrayList("pictures", this.pictureUris);
		resIntent.putExtra("data", resBundle);
		setResult(resultCode, resIntent);
		finish();
	}

	private void deletePictures() {
		ArrayList<String> selectedPictureUris =
		        (ArrayList<String>) imageAdapter.getSelectedUris();
		for (String uri : selectedPictureUris) {
			LOG.debug("Picture deleted: " + uri);
			getContentResolver().delete(Uri.parse(uri), null, null);
		}
		this.pictureUris.removeAll(selectedPictureUris);
		imageAdapter.setSelectedUris(new ArrayList<String>());
		imageAdapter.notifyDataSetChanged();
	}
}

@@ -649,18 +649,17 @@
 tton btn
-_o
+O
 k;%0A%09priv
@@ -668,26 +668,25 @@
 e Button btn
-_d
+D
 elete;%0A%09priv
@@ -699,18 +699,17 @@
 tton btn
-_f
+F
 ertig;%0A%09
@@ -1089,26 +1089,25 @@
 ;%0A%09%09this.btn
-_d
+D
 elete = (But
@@ -1152,18 +1152,17 @@
 this.btn
-_o
+O
 k = (But
@@ -1199,26 +1199,25 @@
 ;%0A%09%09this.btn
-_f
+F
 ertig = (But
@@ -1257,18 +1257,17 @@
 );%0A%09%09btn
-_d
+D
 elete.se
@@ -1295,18 +1295,17 @@
 );%0A%09%09btn
-_o
+O
 k.setOnC
@@ -1329,18 +1329,17 @@
 );%0A%09%09btn
-_f
+F
 ertig.se
@@ -1446,18 +1446,17 @@
 %09    btn
-_d
+D
 elete);%0A

rename button-variables