package gr.mybook.myprovider;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	EditText textId, textName, textPhone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textId = (EditText)findViewById(R.id.studentID);
		textName = (EditText)findViewById(R.id.studentName);
		textPhone = (EditText)findViewById(R.id.studentPhone);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void addEntry(View v) {
		String serial = textId.getText().toString();
		String name = textName.getText().toString();
		String phone = textPhone.getText().toString();
		ContentValues values = new ContentValues();
		values.put(MyContentProvider.STUDENT_ID, serial);
		values.put(MyContentProvider.STUDENT_NAME, name);
		values.put(MyContentProvider.STUDENT_PHONE, phone);
		Uri uri = getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
		Toast.makeText(getBaseContext(), uri.toString(), Toast.LENGTH_LONG).show();
		textId.setText("");
		textName.setText("");
		textPhone.setText("");
	}
	
	public void deleteEntry(View v) {
		String name = textName.getText().toString();
		String selection = MyContentProvider.STUDENT_NAME + " = '" + name + "'";
		int rowsCount = getContentResolver().delete(MyContentProvider.CONTENT_URI, selection, null);
		if (rowsCount>0) {
			textName.setText("");
			Toast.makeText(this, "Student "+name+" deleted", Toast.LENGTH_SHORT).show();
		}
		else {
			Toast.makeText(this, "No match found", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void findEntry(View v) {
		String name = textName.getText().toString();
		String[] projection = {MyContentProvider.COLUMN_ID, MyContentProvider.STUDENT_ID, MyContentProvider.STUDENT_NAME, MyContentProvider.STUDENT_PHONE};
		String selection = MyContentProvider.STUDENT_NAME + " = '" + name + "'";
		Cursor cursor = getContentResolver().query(MyContentProvider.CONTENT_URI, projection, selection, null, null);
		if (cursor.moveToFirst()) {
			cursor.moveToFirst();
			String sId = cursor.getString(1);
			String sName = cursor.getString(2);
			String sPhone = cursor.getString(3);
			textId.setText(sId);
			textName.setText(sName);
			textPhone.setText(sPhone);
		}
		else {
			Toast.makeText(this, "No match found", Toast.LENGTH_SHORT).show();
		}
		cursor.close();
	}
	
	public void updateEntry(View v) {
		String serial = textId.getText().toString();
		String name = textName.getText().toString();
		String phone = textPhone.getText().toString();
		ContentValues values = new ContentValues();
		values.put(MyContentProvider.STUDENT_ID, serial);
		values.put(MyContentProvider.STUDENT_NAME, name);
		values.put(MyContentProvider.STUDENT_PHONE, phone);
		String selection = MyContentProvider.STUDENT_NAME + " = '" + name + "'";
		int rowsCount = getContentResolver().update(MyContentProvider.CONTENT_URI, values, selection, null);
		textId.setText("");
		textName.setText("");
		textPhone.setText("");
		if (rowsCount>0) {
			Toast.makeText(this, "Updated", Toast.LENGTH_SHORT).show();
		}
		else {
			Toast.makeText(this, "No match found", Toast.LENGTH_SHORT).show();
		}

	}
}

