package com.novoda.sqliteprovider.demo.ui;

import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.novoda.sqliteprovider.demo.R;
import com.novoda.sqliteprovider.demo.domain.Firework;
import com.novoda.sqliteprovider.demo.provider.FireworkProvider;
import com.novoda.sqliteprovider.demo.ui.base.NovodaActivity;

public class AddFireworkActivity extends NovodaActivity {

	private EditText nameEditText;
	private EditText colorEditText;
	private EditText noiseEditText;
	private EditText typeEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_firework);
		
		nameEditText = (EditText) findViewById(R.id.add_firework_input_name);
		colorEditText = (EditText) findViewById(R.id.add_firework_input_color);
		noiseEditText = (EditText) findViewById(R.id.add_firework_input_noise);
		typeEditText = (EditText) findViewById(R.id.add_firework_input_type);
	}
	
	public void onAddFireworkClick(View button){
		if(TextUtils.isEmpty(nameEditText.getText()) || TextUtils.isEmpty(colorEditText.getText()) || 
				TextUtils.isEmpty(noiseEditText.getText()) || TextUtils.isEmpty(typeEditText.getText())) {
			Toast.makeText(this, "Fill in the firework", Toast.LENGTH_SHORT).show();
			return;
		}

		String name = "";
		String color = "";
		String noise = "";
		String type = "";
		
		name = nameEditText.getText().toString();
		color = colorEditText.getText().toString();
		noise = noiseEditText.getText().toString();
		type = typeEditText.getText().toString();
		
		Firework firework = new Firework(name, color, type, noise);
		
		ContentValues values = new ContentValues();
		values.put("name", name);
		values.put("color", color);
		values.put("noise", noise);
		values.put("type", type);
		
		getContentResolver().insert(Uri.parse(FireworkProvider.AUTHORITY + "fireworks"), values);
		
		Toast.makeText(this, "Firework that goes "+ noise +" added.", Toast.LENGTH_SHORT).show();
		
		nameEditText.setText("");
		colorEditText.setText("");
		noiseEditText.setText("");
		typeEditText.setText("");
	}
	
}

@@ -1166,70 +1166,52 @@
 %09if(
-TextUtils.isEmpty(nameEditText.getText()) %7C%7C TextUtils.isEmpty
+checkForInput(nameEditText) %7C%7C checkForInput
 (col
@@ -1224,91 +1224,62 @@
 Text
-.getText()
 ) %7C%7C
- 
 %0A%09%09%09%09
-TextUtils.isEmpty(noiseEditText.getText()) %7C%7C TextUtils.isEmpty
+checkForInput(noiseEditText) %7C%7C checkForInput
 (typ
@@ -1287,26 +1287,16 @@
 EditText
-.getText()
 )) %7B%0A%09%09%09
@@ -2188,13 +2188,117 @@
 (%22%22);%0A%09%7D
+%0A%0A%09private boolean checkForInput(EditText editText) %7B%0A%09%09return TextUtils.isEmpty(editText.getText());%0A%09%7D
 %0A%09%0A%7D%0A

refactor to reduce duplication