package com.usai.apex;

import java.util.List;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
//import android.provider.Contacts;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LocationDetailActivity extends Activity
{
	LocationDetail	detail;

	boolean isIntentAvailable(Context context, Intent intent)
	{
		final PackageManager packageManager = context.getPackageManager();
		List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
				PackageManager.GET_ACTIVITIES);
		return list.size() > 0;
	}

	public int getContactid(String name)
	{
		int _id = -1;

		Cursor cursor = getContentResolver()
				.query(Data.CONTENT_URI,
						new String[] { Data._ID, Data.DISPLAY_NAME,
								Data.DISPLAY_NAME_ALTERNATIVE,
								Data.DISPLAY_NAME_SOURCE }, Data.DISPLAY_NAME+"='"+name+"'", null, null);

		Log.d("", "contact " + Data.DISPLAY_NAME + name);

		// Uri uri =
		// Uri.parse("content://com.android.contacts/data/name/filter/" +
		// number);
		// ContentResolver resolver = getContentResolver();
		// Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null,
		// null);
		while (cursor.moveToNext())
		{
			Log.d("", "cursor=" + cursor.getString(1) + cursor.getString(2)
					+ cursor.getString(3));
			_id = cursor.getInt(0);

		}
		cursor.close();
		return _id;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location_detail);
		detail = (LocationDetail) getIntent().getSerializableExtra("detail");
		TextView tv_name = (TextView) findViewById(R.id.tv_name);
		tv_name.setText(detail.Name);
		// tv_name.setMovementMethod(LinkMovementMethod.getInstance());
		TextView tv_addr = (TextView) findViewById(R.id.tv_addr);
		tv_addr.setText(getString(R.string.str_address)+detail.Addr);
		// tv_name.setMovementMethod(LinkMovementMethod.getInstance());
		TextView tv_tel = (TextView) findViewById(R.id.tv_tel);
		tv_tel.setText(getString(R.string.str_tel)+detail.Tel);
		// tv_name.setMovementMethod(LinkMovementMethod.getInstance());
		TextView tv_fax = (TextView) findViewById(R.id.tv_fax);
		tv_fax.setText(getString(R.string.str_fax)+detail.Fax);
		// tv_name.setMovementMethod(LinkMovementMethod.getInstance());
		TextView tv_contact = (TextView) findViewById(R.id.tv_contact);
		tv_contact.setText(getString(R.string.str_contact)+detail.Contact);
		// tv_name.setMovementMethod(LinkMovementMethod.getInstance());
		TextView tv_email = (TextView) findViewById(R.id.tv_email);
		tv_email.setText(getString(R.string.str_email)+detail.Email);
		// tv_name.setMovementMethod(LinkMovementMethod.getInstance());

		Button btn = (Button) findViewById(R.id.btn_addcontact);
		btn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{

				int _id = getContactid(detail.Name);
				Log.d("", "contact id = " + _id);
				Intent it = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, 
		                ContactsContract.Contacts.CONTENT_URI); 
				it.setData(Uri.parse("tel:"+detail.Tel));
				it.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true);
//				it.setType(ContactsContract.Contacts.CONTENT_TYPE);// .People.CONTENT_TYPE);
				it.putExtra(ContactsContract.Intents.Insert.COMPANY,
						detail.Name);
				
				
				String tel[] = detail.Tel.split("\n");
				if(tel.length>0)
				{
					it.putExtra(ContactsContract.Intents.Insert.PHONE, tel[0]);
					it.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, Phone.TYPE_WORK);
				}
				if(tel.length>1)
				{
					it.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, tel[1]);
					it.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, Phone.TYPE_WORK);
				}
				if(tel.length>2)
				{
					it.putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE, tel[2]);
					it.putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE_TYPE, Phone.TYPE_WORK);
				}
				String email[] = detail.Email.split("\n");
				if(email.length>0)
				{
					it.putExtra(ContactsContract.Intents.Insert.EMAIL, email[0]);
					it.putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE, Email.TYPE_WORK);
				}
				if(email.length>1)
				{
					it.putExtra(ContactsContract.Intents.Insert.SECONDARY_EMAIL, email[1]);
					it.putExtra(ContactsContract.Intents.Insert.SECONDARY_EMAIL_TYPE, Email.TYPE_WORK);
				}
				if(email.length>2)
				{
					it.putExtra(ContactsContract.Intents.Insert.TERTIARY_EMAIL, email[2]);
					it.putExtra(ContactsContract.Intents.Insert.TERTIARY_EMAIL_TYPE, Email.TYPE_WORK);
				}
				//
//				intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE,
//						Contacts.PhonesColumns.TYPE_MOBILE);
//				it.putExtra(ContactsContract.Intents.Insert.EMAIL, detail.Email);
//				it.putExtra(ContactsContract.Intents.Insert.COMPANY,
//						detail.Name);
				it.putExtra(ContactsContract.Intents.Insert.POSTAL, detail.Addr);

				if (!isIntentAvailable(LocationDetailActivity.this, it))
				{
					Toast.makeText(LocationDetailActivity.this,
							"failed to add contact", Toast.LENGTH_LONG).show();
					return;
				}

				startActivity(it);

			}
		});
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu)
//	{
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.location_detail, menu);
//		return true;
//	}

}
