package patrice;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import net.kerul.fstmdirektori.R;

public class ViewRecord extends Activity {
	private EditText txtnama, txtjbt, txttelefon, txtemel;
	private ImageButton btncall, btnemail;
	private DBHelper mHelper;
	private SQLiteDatabase dataBase;
	String id, telno,email,name;//to hold the data strings

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_record);
		
		//capture sent parameter from previous screen
		id=getIntent().getExtras().getString("stafid");
		
		//initiate all textbox container to hold the data for the staff
		txtnama=(EditText)findViewById(R.id.txtnama);
		txtjbt=(EditText)findViewById(R.id.txtjbt);
		txttelefon=(EditText)findViewById(R.id.txttelefon);
		txtemel=(EditText)findViewById(R.id.txtemel);
		
		//handling the call
		//be sure to add <uses-permission android:name="android.permission.CALL_PHONE" /> in manifest
		btncall=(ImageButton)findViewById(R.id.btncall);
		btncall.setOnClickListener( new OnClickListener(){
			public void onClick(View v){
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"+telno ));//provide the phone number
				startActivity(intent);//call
			}
		});//end btncall setOnCLickCListener
		
		//handling the email button
		btnemail=(ImageButton)findViewById(R.id.btnemail);
		btnemail.setOnClickListener( new OnClickListener(){
			public void onClick(View v){
				Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
						"mailto",email, null));
				emailIntent.putExtra(Intent.EXTRA_SUBJECT, "A query about FSTM");//title
				emailIntent.putExtra(Intent.EXTRA_TEXT, "Salaam, "+name);//message body
				startActivity(Intent.createChooser(emailIntent, "Send email..."));//invoke email client chooser
			}
		});//end btnemail setOnCLickCListener
	}
	
	@Override
	protected void onResume() {
		//refresh data for screen is invoked/displayed
		displayData();
		super.onResume();
	}
	
	//display single record of data from stafid
	private void displayData() {
		mHelper=new DBHelper(this);
		dataBase = mHelper.getWritableDatabase();
		//the SQL command to fetched all records from the table
		String sql="SELECT * FROM "
				+ DBHelper.TABLE_NAME +" WHERE stafid='"+id+"';";
		Cursor mCursor = dataBase.rawQuery(sql, null);
		
		//fetch the record
		if (mCursor.moveToFirst()) {
			//fetch each field and transfer to textbox
			telno=mCursor.getString(mCursor.getColumnIndex(DBHelper.TELEFON));
			email=mCursor.getString(mCursor.getColumnIndex(DBHelper.EMEL));
			name=mCursor.getString(mCursor.getColumnIndex(DBHelper.NAMA));
			//get data from field and transfer to EditText
			txtnama.setText(name);
			txtjbt.setText(mCursor.getString(mCursor.getColumnIndex(DBHelper.JBT)));
			txttelefon.setText(telno);
			txtemel.setText(email);
				
		}
		else{
			//do something here if no record fetched from database
			txtnama.setText(sql);
		}
	}//end displayData

}
