package com.example.listviewcustom03;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ContactsDetails extends Activity {	
	
	Bundle extras;
	
	private ContactHandler handler;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{		
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.contacts_details);	
		
		
		// Get intent data
		extras = getIntent().getExtras();
		
		handler = new ContactHandler(getApplicationContext());
		
		int alpha = extras.getInt("id");
		
		ImageView iv_photo = (ImageView) findViewById(R.id.iv_contact_photo);
		//iv_photo.setImageBitmap(BitmapFactory.decodeFile(extras.getString("photograph")));
		
		// ImageView imageView = (ImageView) findViewById(R.id.image);
		//  imageView.setImageResource(R.drawable.hello);
		
		//iv_photo.setImageBitmap(BitmapFactory.decodeFile("user_icon"));
		
		iv_photo.setImageResource(R.drawable.user_icon);
		
		TextView tv_nom = (TextView) findViewById(R.id.contact_nom);
		tv_nom.setText(extras.getString("nom"));
		
		TextView tv_prenom = (TextView) findViewById(R.id.contact_prenom);
		tv_prenom.setText(extras.getString("prenom"));
		
		TextView tv_adresse = (TextView) findViewById(R.id.contact_adresse);
		tv_adresse.setText(extras.getString("adresse"));
		
		TextView tv_email = (TextView) findViewById(R.id.contact_email);
		tv_email.setText(extras.getString("email"));
		
		TextView tv_portable = (TextView) findViewById(R.id.contact_portable);
		tv_portable.setText(extras.getString("portable"));
		
		TextView tv_fixe = (TextView) findViewById(R.id.contact_fixe);
		tv_fixe.setText(extras.getString("fixe"));
		
		Button btn_back = (Button) findViewById(R.id.btn_back_to_contact);
		btn_back.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) 
			{
					finish();
					Intent intent = new Intent(ContactsDetails.this, MainActivity.class);
					startActivity(intent);
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(1, 1, 0, "Edit");
		menu.add(1,2,1,"Delete");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		
		int itemid = item.getItemId();	
		
		switch(itemid)
		{
			case 1:				
				//Toast.makeText(getApplicationContext(), "Edit", Toast.LENGTH_LONG).show();
				EditContact();
				break;
			case 2:
				
				//Toast.makeText(getApplicationContext(), "On efface", Toast.LENGTH_LONG).show();
				
				/*if(handler.deleteContact(extras.getInt("id")))
				{
					//Toast.makeText(getApplicationContext(), "Contact has been deleted.", Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(ContactsDetails.this, MainActivity.class);
					startActivity(intent);
				}
				*/
				valide_delete_contact();
				break;
				
		}
		
		return super.onOptionsItemSelected(item);
		
	}
	
	
	public void valide_delete_contact()
    {
    	
							AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

							alertDialogBuilder.setTitle("Confirmation");
			
							// set dialog message
							alertDialogBuilder
							.setMessage("Click yes to delete!")
							.setCancelable(false)
							.setPositiveButton("Yes",new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,int id)
								{
									if(handler.deleteContact(extras.getInt("id")))
									{
										//Toast.makeText(getApplicationContext(), "Contact has been deleted.", Toast.LENGTH_SHORT).show();
										Intent intent = new Intent(ContactsDetails.this, MainActivity.class);
										startActivity(intent);
									}
								}
							})
							
							.setNegativeButton("No",new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,int id) {
									dialog.cancel();
								}
							});

							// create alert dialog
							AlertDialog alertDialog = alertDialogBuilder.create();

							// show it
							alertDialog.show();
    }
	
	public void EditContact()
	{		
		Intent intent = new Intent(ContactsDetails.this, EditContact.class);
		intent.putExtras(extras);
		startActivity(intent);
	}
}
