package com.example.listviewcustom05;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
 
public class debug02 extends Activity
{
 
	Bundle extras;
	private ListView ListViewCustom;
	protected TextView employeeNameText;
	protected TextView titleText;
	protected List<contact_action> actions;
	private DbAdapter handler;
 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.vue_infos_contact);
        setContentView(R.layout.test3);
        
        
    	extras = getIntent().getExtras();
    	
    	handler = new DbAdapter(getApplicationContext());
    	
    	String nom = extras.getString("nom");
		String prenom = extras.getString("prenom");
		String adresse = extras.getString("adresse");
		String email = extras.getString("email");
		String portable = extras.getString("portable");
		String fixe = extras.getString("fixe");
		
		employeeNameText = (TextView) findViewById(R.id.employeeName);
	    employeeNameText.setText(nom.toUpperCase());
	
	 
	    String firstLetter = prenom.substring(0,1).toUpperCase();
	    String restLetters = prenom.substring(1).toLowerCase();
	    String complete =  firstLetter + restLetters;
	    
	    titleText = (TextView) findViewById(R.id.title);
	    titleText.setText(complete);
	    
	    ListViewCustom = (ListView) findViewById(R.id.list);
 
        //Création de la ArrayList qui nous permettra de remplire la listView
        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
 
        //On déclare la HashMap qui contiendra les informations pour un item
        HashMap<String, String> map;
 
        //Création d'une HashMap pour insérer les informations du premier item de notre listView
        map = new HashMap<String, String>();
        map.put("titre", "Portable");
        map.put("description", portable);
        map.put("img", String.valueOf(R.drawable.smartphone));
        listItem.add(map);
 
        map = new HashMap<String, String>();
        map.put("titre", "Fixe");
        map.put("description",fixe );
        map.put("img", String.valueOf(R.drawable.fixe));
        listItem.add(map);
 
        map = new HashMap<String, String>();
        map.put("titre", "Email");
        map.put("description", email);
        map.put("img", String.valueOf(R.drawable.mail));
        listItem.add(map);
 
        map = new HashMap<String, String>();
        map.put("titre", "SMS");
        map.put("description", "SMS");
        map.put("img", String.valueOf(R.drawable.sms));
        listItem.add(map);
 
        //Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue item_infos_contact
        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.item_infos_contact,
               new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
 
        //On attribut à notre listView l'adapter que l'on vient de créer
        ListViewCustom.setAdapter(mSchedule);
 
        actions = new ArrayList<contact_action>();
	       
        actions.add(new contact_action("Call office", "fixe", contact_action.ACTION_CALL));
        actions.add(new contact_action("Call mobile", "portable", contact_action.ACTION_CALL));
        actions.add(new contact_action("SMS", "email", contact_action.ACTION_SMS));
        actions.add(new contact_action("Email", "email", contact_action.ACTION_EMAIL));
 
     
        //Enfin on met un écouteur d'évènement sur notre listView
        ListViewCustom.setOnItemClickListener(new OnItemClickListener() {
			@Override
        	@SuppressWarnings("unchecked")
         	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        		
        		
        		contact_action action = actions.get(position);

    		    Intent intent;
    		    switch (action.getType()) {

    		    	case contact_action.ACTION_CALL:  
    		        	Uri callUri = Uri.parse("tel:" + action.getData());  
    		        	intent = new Intent(Intent.ACTION_CALL, callUri); 
    		    		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    		    	    startActivity(intent);
    		    	    
    		    	    
    		    	    //Intent callIntent = new Intent(Intent.ACTION_CALL);
    					//callIntent.setData(Uri.parse("tel:0377778888"));
    					//startActivity(callIntent);
    		    	    
    		        	break;

    		    	case contact_action.ACTION_EMAIL:  
    	                intent = new Intent(Intent.ACTION_SEND);
    	                intent.setType("plain/text");
    	                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{action.getData()});
    	                startActivity(intent);
    	                break;
    	                
    		    	case contact_action.ACTION_SMS:  
    		        	Uri smsUri = Uri.parse("sms:" + action.getData());  
    		        	intent = new Intent(Intent.ACTION_VIEW, smsUri); 
    		    	    startActivity(intent);
    		        	break;

    		    	
    		    }
        		
        	}
         });
        
        
        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(debug02.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)
								{
									handler.open();

									if(handler.deleteContact(extras.getInt("id")))
									{
										//Toast.makeText(getApplicationContext(), "Contact has been deleted.", Toast.LENGTH_SHORT).show();
										Intent intent = new Intent(debug02.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(debug02.this, Contact_edition.class);
		intent.putExtras(extras);
		startActivity(intent);
	}
}
