package com.lianxin.pubqq.sms;

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

import com.lianxin.panqq.sms.Contact;
import com.lianxin.panqq.sms.NumberAdapter;
import com.lianxin.pubqq.R;




import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class NumberActivity extends Activity {
	ListView listview;
	ArrayList<String> numberlist;
	ArrayList<HashMap<String, Object>> arr;
	List<Contact> users;
	boolean[] ischecked;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_list);

		listview=(ListView)findViewById(R.id.listView1);
		
		numberlist=getContentConract();
		
		ischecked=new boolean[numberlist.size()];
		for(int i=0;i<ischecked.length;i++)
			ischecked[i]=false;

		
		boolean bool=this.getIntent().getBooleanExtra("mode", false);
		ArrayList<String> arra;
		
		if(bool)
		{
			arra=this.getIntent().getStringArrayListExtra("number");
			for(int i=0;i<arra.size();i++)
			{
				for(int j=0;j<numberlist.size();j++)
					if(arra.get(i).equals(numberlist.get(j).
				substring(numberlist.get(j).indexOf("<")+1,numberlist.get(j).lastIndexOf(">"))))
					{
						ischecked[j]=true;
					}
			}
			
		}
			

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				ischecked[position]=!users.get(position).isChecked();
				onResume();
					
			}
			
		});
		
		
	}
	
    private ArrayList<String> getContentConract(){

		ArrayList arrylist = new ArrayList();

		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())
					{   
						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+">");
							
						}while(phone.moveToNext());
					}
				}
			 }
		}
		return arrylist;
	}
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	
    	
		users= new ArrayList<Contact>();
		
		
		
		for(int i=0;i<numberlist.size();i++) {

			users.add(new Contact(numberlist.get(i),ischecked[i]));
		}

		NumberAdapter usadpter=new NumberAdapter(getBaseContext(), users);
		listview.setAdapter(usadpter);
		
	

/*		arr=new ArrayList<HashMap<String,Object>>();
		
		for(int i=0;i<numberlist.size();i++)
		{
			HashMap<String, Object> hash=new HashMap<String, Object>();
			hash.put("number", numberlist.get(i));
			hash.put("ischecked", false);
			arr.add(hash);
		}
		
		SimpleAdapter sim=new SimpleAdapter(getBaseContext(), arr, R.layout.number_list_adapter, new String[]{"number","ischecked"}, new int[]{R.id.name_number,R.id.checkbox});

		listview.setAdapter(sim);*/
    }
    public void onFinish(View v)
    {
    	
    	String contact = "";
		for(int i=0;i<numberlist.size();i++) {
			if(users.get(i).isChecked())
				//contact+=numberlist.get(i).substring(numberlist.get(i).indexOf("<")+1,numberlist.get(i).lastIndexOf(">"))+"+";
				contact+=numberlist.get(i)+" + ";
			
		}
		SMSActivity.number.setText(contact);
		//Toast.makeText(getBaseContext(),contact+"",Toast.LENGTH_SHORT).show();
		finish();
    }

    

}
