package com.example.listviewcustom03;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class EditContact extends Activity 
{
	
	private static int RESULT_LOAD_IMAGE = 1;
	
	private ContactHandler handler;
	
	private String picturePath = "";
	
	EditText et_nom;
	EditText et_prenom;
	EditText et_adresse;
	EditText et_email;
	EditText et_portable;
	EditText et_fixe;
	ImageView iv_photograph;
	
	Bundle extras;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact);
		
		extras = getIntent().getExtras();
	
		handler = new ContactHandler(getApplicationContext());
		
		TextView tv_title = (TextView) findViewById(R.id.tv_new_contact_title);
		tv_title.setVisibility(View.GONE);		
		
		et_nom = (EditText) findViewById(R.id.et_nom);
		et_nom.setText(extras.getString("nom"));
		
		et_prenom = (EditText) findViewById(R.id.et_prenom);
		et_prenom.setText(extras.getString("prenom"));
		
		et_adresse = (EditText) findViewById(R.id.et_adresse);
		et_adresse.setText(extras.getString("adresse"));
		
		et_email = (EditText) findViewById(R.id.et_email);
		et_email.setText(extras.getString("email"));
		
		et_portable = (EditText) findViewById(R.id.et_portable);
		et_portable.setText(extras.getString("portable"));		
		
		et_fixe = (EditText) findViewById(R.id.et_fixe);
		et_fixe.setText(extras.getString("fixe"));
		
		iv_photograph = (ImageView) findViewById(R.id.iv_user_photo);
		iv_photograph.setImageBitmap(BitmapFactory.decodeFile(extras.getString("photograph")));
		
		Button btn_update = (Button) findViewById(R.id.btn_add);
		btn_update.setText("Update");
		
		
		btn_update.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				
					Contact contact = new Contact();

					
					int id = extras.getInt("id");
					String nom = et_nom.getText().toString();
					String prenom = et_prenom.getText().toString();
					String adresse = et_adresse.getText().toString();
					String email = et_email.getText().toString();
					String portable = et_portable.getText().toString();
					String fixe = et_fixe.getText().toString();
					

					contact.setID(extras.getInt("id")); // Update the data where id = extras.getInt("id").				
					contact.setNom(et_nom.getText().toString());				
					contact.setPrenom(et_prenom.getText().toString());				
					contact.setAdresse(et_adresse.getText().toString());				
					contact.setEmail(et_email.getText().toString());				
					contact.setPortable(et_portable.getText().toString());				
					contact.setFixe(et_fixe.getText().toString());				
					contact.setPhotograph(picturePath);

					//Toast.makeText(getApplicationContext(), "Contact update on going ...", Toast.LENGTH_LONG).show();
					Boolean updated = handler.editContact(contact);
					//Boolean updated = handler.editContact(id, nom, prenom, adresse, email, portable, fixe  ); 
					
					//Boolean updated = handler.editContact(); 
					if(updated)
					{
						Intent intent = new Intent(EditContact.this, MainActivity.class);
						startActivity(intent);
					}
					else
					{
						Toast.makeText(getApplicationContext(), "Contact data not updated. Please try again", Toast.LENGTH_LONG).show();
					}
								
				
			}
		});
		
		
		ImageView iv_user_photo = (ImageView) findViewById(R.id.iv_user_photo);
		iv_user_photo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				
				startActivityForResult(intent, RESULT_LOAD_IMAGE);				
				
			}
		});
		
	}
	/*
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri imageUri = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
 
            Cursor cursor = getContentResolver().query(imageUri,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
 
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();
             
            ImageView imageView = (ImageView) findViewById(R.id.iv_user_photo);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
         
        }
	}
	*/
}
