package com.example.listviewcustom05;

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 Contact_edition extends Activity 
{
	
	private static int RESULT_LOAD_IMAGE = 1;
	
	private DbAdapter 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_edition);
		
		extras = getIntent().getExtras();
	
		handler = new DbAdapter(getApplicationContext());
		
		handler.open();
		
		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) 
			{
					
					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();
					
					Cursor cursoraaa  = handler.query("nom" , extras.getString("nom"), "prenom", extras.getString("prenom") );
					
					cursoraaa.moveToFirst();
					
					String [] toto = cursoraaa.getColumnNames();
					
					int n = cursoraaa.getCount();
					
					//if (cursor.getCount() > 0)
					//    cursor.moveToFirst(); // Calls to cursor.getType() will work from now on
					
					
					String pipo = cursoraaa.getString(cursoraaa.getColumnIndex("fixe"));
					Integer index = cursoraaa.getInt(cursoraaa.getColumnIndex("_id"));
					
					//String index = cursor.getString(cursor.getColumnIndex("fixe"));
					//String ex = cursor.getString(cursor.getColumnIndex("_id"));
					//String item = cursor.getString(cursor.getColumnIndex(KEY_ROWID));
					//Integer item = cursor.getInt(cursor.getColumnIndex("_id"));
					
					
					Contact contact = new Contact();

					contact.setID(index); 
					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);
					
					if(nom.isEmpty())
					{	
							Toast.makeText(getApplicationContext(), "Champs vide ?", Toast.LENGTH_LONG).show();
					}
					else
					{	
						
						Boolean updated = handler.updateContact(contact);

						if(updated)
						{
							Intent intent = new Intent(Contact_edition.this, MainActivity.class);
							startActivity(intent);
						}
						else
						{
							Toast.makeText(getApplicationContext(), "Contact data not updated. Please try again", Toast.LENGTH_LONG).show();
						}
						
						/*Boolean test = handler.Check_contact_doublon("nom", nom, "prenom" , prenom );
		 				if(test)
						{
		 					Toast.makeText(getApplicationContext(), "Contact exist in database", Toast.LENGTH_LONG).show();
						}
		 				else
		 				{
		 					Boolean updated = handler.editContact(contact);

							if(updated)
							{
								Intent intent = new Intent(Contact_edition.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));
         
        }
	}
	*/
}
