
package com.lianxin.pubqq.chat;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.lianxin.pubqq.R;
//import com.lianxin.pubqq.chat.AlertDialog;
import com.lianxin.panqq.main.EMChatManager;
import com.lianxin.panqq.chat.entity.EMMessage;
import com.lianxin.panqq.edit.MyAlertDialog;

public class ForwardMessageActivity extends PickFriendActivity {
	//private User selectUser;
	private String forward_msg_id;
	private int forward_msgid;
	private int src_destid;	
	private int src_type;		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		forward_msg_id = getIntent().getStringExtra("forward_msg_id");
		forward_msgid = getIntent().getIntExtra("forward_msgid",10);
		src_destid = getIntent().getIntExtra("src_destid",10000);	
		src_type = getIntent().getIntExtra("src_type",1);	
		deaultTitle = "转发至";	
		if(mType == 0){
		    TextView txt_title = (TextView) findViewById(R.id.txt_title);
		    txt_title.setText(deaultTitle);
		}		
	}

	@Override
	protected void onListItemSelected(int destid,int type) {
		if (destid != 0) {
			String msg = this.getString(R.string.confirm_forward_to, ""+destid);
			String title = "转发至?";	
			final int nDestid = destid;
			final int nType = type;			
			new MyAlertDialog(this,title,msg,
					new MyAlertDialog.OnSetListener() {
						@Override
						public void onSelect(int comm) {
							onForwardMessage(nDestid,nType);								
						}
					}).show();	
		}
	}	

	void onForwardMessage(int destid,int type) {
		/*if (destid != 0) {
			try {
				ChatActivity.activityInstance.finish();
			} catch (Exception e) {
			}
			Intent intent = new Intent(this, ChatActivity.class);
		//	if (selectUser == null)
		//		return;
		//	intent.putExtra("userId", selectUser.getUsername());
			intent.putExtra("forward_msg_id", forward_msg_id);
			startActivity(intent);
			finish();
		}*/
		if (destid != 0) {
			EMMessage emMessage = EMChatManager.getInstance().getMessage(src_destid,src_type, forward_msgid);	
			if(emMessage!=null)
			{
				EMChatManager.getInstance().forwardMessage(destid,type, emMessage);
			} 
		}
	}	
	
	/*@Override
	protected void onListItemClick(int position) {
		if (position != 0) {
//			selectUser = contactAdapter.getItem(position);
			Intent intent = new Intent(ForwardMessageActivity.this, AlertDialog.class);
			intent.putExtra("cancel", true);
			intent.putExtra("titleIsCancel", true);
	//		intent.putExtra("msg", getString(R.string.confirm_forward_to, selectUser.getUsername()));
			startActivityForResult(intent, 1);
		}
	}*/

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			try {
				EMChatActivity.activityInstance.finish();
			} catch (Exception e) {
			}
			Intent intent = new Intent(this, EMChatActivity.class);
//			if (selectUser == null)
//				return;

//			intent.putExtra("userId", selectUser.getUsername());
			intent.putExtra("forward_msg_id", forward_msg_id);
			startActivity(intent);
			finish();

		}

		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public void onClick(View arg0) {
		Intent intent;		
		int id = arg0.getId();//	super.onClick(arg0);
//		parpent.onClick(arg0);
		if (id == R.id.img_back) {
			finish(this);
		} else if (id == R.id.rl_pick_friend) {
			intent = new Intent(this, ForwardMessageActivity.class);
			intent.putExtra("type", PickFriendActivity.LOOKTYPE_FRIEND);
			startActivity(intent);
		} else if (id == R.id.rl_pick_depart) {
			intent = new Intent(this, ForwardMessageActivity.class);
			intent.putExtra("type", PickFriendActivity.LOOKTYPE_DEPART);
			//intent.putExtra("type", 2);
			startActivity(intent);
		} else if (id == R.id.rl_pick_crowd) {
			intent = new Intent(this, ForwardMessageActivity.class);
			intent.putExtra("type", PickFriendActivity.LOOKTYPE_CROWD);
			startActivity(intent);
		} else if (id == R.id.rl_pick_class) {
			intent = new Intent(this, ForwardMessageActivity.class);
			intent.putExtra("type", PickFriendActivity.LOOKTYPE_CLASS);
			startActivity(intent);
		}
		//super.onClick(arg0);		
	}	
}
