
package com.lianxin.pubqq.sms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.PingYinUtil;
import com.lianxin.panqq.sms.Contact;
import com.lianxin.panqq.sms.ContactAdapter;
import com.lianxin.panqq.sms.Sidebar;

public class ContactActivity extends BaseActivity {

	private ListView listView;
	private Sidebar sidebar;
	private TextView mDialogText;	
	private WindowManager mWindowManager;	
	protected ContactAdapter contactAdapter;
	private List<Contact> contactList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		listView = (ListView) findViewById(R.id.list);
		sidebar = (Sidebar) findViewById(R.id.sideBar);
		sidebar.setListView(listView);
		mDialogText = (TextView) LayoutInflater.from(this).inflate(
				R.layout.list_position, null);
		mDialogText.setVisibility(View.INVISIBLE);
		mWindowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);		
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.TYPE_APPLICATION,
				WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
						| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
				PixelFormat.TRANSLUCENT);
		mWindowManager.addView(mDialogText, lp);
		sidebar.setTextView(mDialogText);	
		contactList = new ArrayList<Contact>();
		// 获取设置contactlist
		getContactList();
		// 设置adapter
	//	contactAdapter = new ContactAdapter(this, R.layout.row_contact, contactList, sidebar);
		contactAdapter = new ContactAdapter(this, contactList);		
		
		listView.setAdapter(contactAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				onListItemClick(position);
			}
		});

	}

	protected void onListItemClick(int position) {
	/*	if (position != 0) {
			setResult(RESULT_OK, new Intent().putExtra("username", contactAdapter.getItem(position)
					.getUsername()));
			finish();
		}*/
		contactList.get(position).setChecked(!contactList.get(position).isChecked());		
	
	}

	public void back(View view) {
		finish();
	}
	public void getContactList1() {
		contactList.clear();
		contactList.add(new Contact("name","130"));
		contactList.add(new Contact("abc","130"));	
		contactList.add(new Contact("xyz","130"));		
	}
	private void getContactList() {
		contactList.clear();		
		Cursor cur=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		if(cur.moveToFirst())
		{
			int idcolum=cur.getColumnIndex(ContactsContract.Contacts._ID);
			int namecolum=cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); //获取本地联系人表中 联系人姓名的 column
			while(cur.moveToNext())
			{
				String contectId=cur.getString(idcolum);
				//displayname
				int phonecount=cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
				
				if(phonecount>0)
				{
	
					Cursor phone=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = " + contectId, null, null);
					if(phone.moveToFirst())
					{  
						int n = 0;
						do
						{
							String phonenumber=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
							String name=phone.getString(phone.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
					//		arrylist.add(name+" <"+phonenumber+">");
					//		contactList.add(new User(name,phonenumber));							
							Contact user = new Contact(name,phonenumber);
							user.setHeader(PingYinUtil.converterToFirstLetter(name).substring(0, 1).toUpperCase());
							char header = user.getHeader().toLowerCase().charAt(0);
							if (header < 'a' || header > 'z') {
								user.setHeader("#");
							}							
							contactList.add(user);							
							n++;
							if(n>10) break;
						}while(phone.moveToNext());
					}
				}
			 }
		}
		// 排序
		Collections.sort(contactList, new Comparator<Contact>() {

			@Override
			public int compare(Contact lhs, Contact rhs) {
				return lhs.getUserName().compareTo(rhs.getUserName());
			}
		});	

	}

	/*protected void setUserHearder(String username, User user) {
		String headerName = null;
		if (!TextUtils.isEmpty(user.getNick())) {
			headerName = user.getNick();
		} else {
			headerName = user.getUsername();
		}
		if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
			user.setHeader("");
		} else if (Character.isDigit(headerName.charAt(0))) {
			user.setHeader("#");
		} else {
			user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1)).get(0).target.substring(0, 1).toUpperCase());
			char header = user.getHeader().toLowerCase().charAt(0);
			if (header < 'a' || header > 'z') {
				user.setHeader("#");
			}
		}
	}*/	
}
