package com.lianxin.pubqq.sms;

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

import com.lianxin.pubqq.R;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;

import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SMSActivity extends Activity {
	String SENT = "SMS_SENT";
	String DELIVERED = "SMS_DELIVERED";
	PendingIntent sentPI, deliveredPI;
	BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;
	IntentFilter intentFilter;
	EditText message;
	static EditText number;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_smssend);
		
		number=(EditText)findViewById(R.id.number);
		message=(EditText)findViewById(R.id.message);

		sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);

		deliveredPI = PendingIntent.getBroadcast(this, 0,
				new Intent(DELIVERED), 0);

		// �-intent to filter for SMS messages received�-
		intentFilter = new IntentFilter();
		intentFilter.addAction("SMS_RECEIVED_ACTION");

	}

	@Override
	public void onResume() {
		super.onResume();

		smsSentReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "信息已发送",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};

		// ---create the BroadcastReceiver when the SMS is delivered---
		smsDeliveredReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "对方已收到信息",
							Toast.LENGTH_SHORT).show();
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};

		// ---register the two BroadcastReceivers---
		registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));
		registerReceiver(smsSentReceiver, new IntentFilter(SENT));
	}

	public void onClick(View v) {
		
		String con=message.getText().toString();
		String num=number.getText().toString();
		if(!con.equals("")&&!num.equals(""))			
		{
			ArrayList<String> list=new ArrayList<String>();
			list=toNumber(num);
			for(int i=0;i<list.size();i++)
				sendSMS(list.get(i), con);
		}
		
		else
			Toast.makeText(getBaseContext(), "请输入联系人和信息内容！", Toast.LENGTH_SHORT).show();
	}
	
	public void onNumber_List(View v) {
		
		String num=number.getText().toString();
		Intent intent=new Intent(SMSActivity.this,NumberActivity.class);
		
		
		if(!num.equals(""))			
		{	
			ArrayList<String> list=new ArrayList<String>();
			list=toNumber(num);
			if(list.size()>=1)
			{
				intent.putExtra("mode", true);  //需要加载所选联系人
				intent.putStringArrayListExtra("number", list);
			}
		
		}
	    startActivity(intent);

	}

	// �-sends an SMS message to another device�-
	private void sendSMS(String phoneNumber, String message) {
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

	}

	
	private ArrayList<String> toNumber(String sentence)
	{
		//jbkj,bghj,bhjk,bhj,
		//sentence.i
		 ArrayList<String> list=new ArrayList<String>();
		String Sentence="";
			for(int i=0;i<sentence.length();i++)
			{
				if(sentence.charAt(i)<='9'&&sentence.charAt(i)>='0')
					Sentence+=sentence.charAt(i);
					
			}
	     if(Sentence.length()%11!=0)
	    	 Toast.makeText(getBaseContext(), "号码输入错误", Toast.LENGTH_SHORT).show();
	     else
	    	 for(int i=0;i<Sentence.length()/11;i++){
	    		 String tmp="";
	    		 for(int j=i*11;j<(i+1)*11;j++) //0~10 11~21
	    			 tmp+=Sentence.charAt(j);
	    		 list.add(tmp);
	    	 }
	    			 
	    			 
		
		
		return list;
		
	}
	
}