 
package com.lianxin.pubqq.activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.edit.NewFriendTypeDialog;
import com.lianxin.panqq.client.EditClient;
import com.lianxin.panqq.client.callback.askCallBack;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.DefineChat;
import com.lianxin.panqq.common.EditResult;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.list.utils.FriendTypeUtils;
import com.lianxin.panqq.list.utils.FriendUtils;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.utils.ImagesUtils;
//import com.lianxin.panqq.chat.entity.EMMessage;
import com.lianxin.panqq.common.bean.AskInfo;
import com.lianxin.panqq.common.bean.GroupInfo;

//好友分类界面
public class FriendTypeInfoActivity extends BaseActivity implements OnClickListener {
	private TextView txt_title,txt_right;
	private ImageView img_back, img_right; 
	private ImageView img_photo;  //用户头像
	private TextView txt_typeid; //用户id
	private TextView txt_typename;  //用户名	
	private TextView txt_number;   //用户职业
	private TextView txt_index;   //用户签名

	private Button btn_edittype;
	private Button btn_newtype;
	private Button btn_deltype;
	private Button btn_listmember;
	
	private int nType = 10100;//用户唯一id
	private int nTypeId = 1;//用户唯一id
	private String strTypeName = "情已停机";

	private int groupType = DefineUser.TYPE_FRIEND;	
	private int image = 10;
	private int iNumber = 10;
	
	public static final int REQUEST_CODE_ADD_TO_CONTACT = 226;
	public static final int REQUEST_CODE_ADD_TO_BLACKLIST = 225;
	//private ProgressDialog progressDialog;
	
	private LinearLayout lay_right;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friendtypeinfo);
		
		initControl();
		initView();
		initData();
		setListener();			
	}

	protected void initControl() {
		txt_title = (TextView) findViewById(R.id.txt_title);
		//txt_title.setText("用户查找");
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setVisibility(View.VISIBLE);	
		txt_typeid = (TextView)findViewById(R.id.tv_typeid);
		txt_typename = (TextView)findViewById(R.id.tv_typename);		
		img_photo = (ImageView)findViewById(R.id.iv_image);
		txt_number = (TextView)findViewById(R.id.tv_number);
		txt_index = (TextView)findViewById(R.id.tv_index);		
		
		btn_edittype = (Button)findViewById(R.id.btn_edittype);
		btn_newtype = (Button)findViewById(R.id.btn_newtype);
		btn_deltype = (Button)findViewById(R.id.btn_deltype);		
		btn_listmember = (Button)findViewById(R.id.btn_listmember);
		
		lay_right = (LinearLayout)findViewById(R.id.layout_right);
		txt_right = (TextView)findViewById(R.id.tv_right);
	}

	protected void initView() {
	
	}
	
	protected void initData() {

		nType = getIntent().getIntExtra("TypeType", DefineChat.ChatType_DEPART);
		nTypeId = getIntent().getIntExtra("TypeID", 1);		
		strTypeName = getIntent().getStringExtra("TypeName");

		image = getIntent().getIntExtra("Info_Image", 23);		

		txt_typeid.setText(""+nTypeId); 
		txt_typename.setText(strTypeName);	
		iNumber = FriendUtils.getFriendCount(nTypeId);
		txt_number.setText(""+iNumber); 		
		
        if(image<0) image = -image;		
        if(image>80) image = image%80;
        img_photo.setImageResource(ImagesUtils.images[image]);	

	}	

	protected void setListener() {
		img_back.setOnClickListener(this);		
		lay_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
			}
		});
		btn_edittype.setOnClickListener(this);
		btn_newtype.setOnClickListener(this);
		btn_deltype.setOnClickListener(this); 
		btn_listmember.setOnClickListener(this);		

	}	
	
	@Override
	public void onClick(View arg0) {
		Intent intent1 = new Intent();
		int id = arg0.getId();
		if (id == R.id.img_back) {
			Utils.finish(FriendTypeInfoActivity.this);
			//finish();
			//finish(MytypeInfoActivity.this);
		} else if (id == R.id.btn_edittype) {
			GroupInfo uinfo3 = new GroupInfo(nTypeId, strTypeName, image);
			new NewFriendTypeDialog(FriendTypeInfoActivity.this, uinfo3, DefineUser.ASK_FRIEND_EDITTYPE,
					new NewFriendTypeDialog.OnSetListener() {
						@Override
						public void onSelect(GroupInfo uinfo) {
							String strname = uinfo.nickname;
							txt_typename.setText(strname);
							int newid = uinfo.groupid;
							txt_typeid.setText("" + newid);
							//       txt_index.setText(""+uinfo.index);
							editTypename(uinfo, DefineUser.ASK_FRIEND_EDITTYPE);
						}
					}).show();
		} else if (id == R.id.btn_newtype) {
			GroupInfo uinfo2 = new GroupInfo(nTypeId, strTypeName, image);
			new NewFriendTypeDialog(FriendTypeInfoActivity.this, uinfo2, DefineUser.ASK_FRIEND_ADDTYPE,
					new NewFriendTypeDialog.OnSetListener() {
						@Override
						public void onSelect(GroupInfo uinfo) {
							String strname = uinfo.nickname;
							txt_typename.setText(strname);
							int newid = uinfo.groupid;
							txt_typeid.setText("" + newid);
							editTypename(uinfo, DefineUser.ASK_FRIEND_ADDTYPE);
						}
					}).show();
		} else if (id == R.id.btn_deltype) {
			GroupInfo uinfo1 = new GroupInfo(nTypeId, strTypeName, image);
			new NewFriendTypeDialog(FriendTypeInfoActivity.this, uinfo1, DefineUser.ASK_FRIEND_DELETETYPE,
					new NewFriendTypeDialog.OnSetListener() {
						@Override
						public void onSelect(GroupInfo uinfo) {
							String strname = uinfo.nickname;
							txt_typename.setText(strname);
							//      txt_index.setText(""+uinfo.index);
							editTypename(uinfo, DefineUser.ASK_FRIEND_DELETETYPE);
						}
					}).show();
		} else if (id == R.id.btn_listmember) {
			Intent intent_list = new Intent(this, MemberListActivity.class);
			intent_list.putExtra("TypeID", nTypeId);
			intent_list.putExtra("TypeType", DefineUser.TYPE_FRIEND);
			startActivity(intent_list);
		} else if (id == R.id.rl_user_nickname) {
		} else if (id == R.id.rl_user_power) {
		} else if (id == R.id.rl_user_index) {
		}
	} 

	public void editTypename(GroupInfo uinfo,int command){
		int nTypeid = uinfo.groupid;
	
		if(nTypeid<0||nTypeid>=100){//
			Utils.showLongToast(FriendTypeInfoActivity.this, "好友分类ID只能在1-99之间!");
			return;
		}
		if(nTypeid==0&&command==DefineUser.ASK_FRIEND_EDITTYPE){//
			Utils.showLongToast(FriendTypeInfoActivity.this, "好友分类ID只能在1-99之间!");
			return;
		}		
		if(nTypeid!=nTypeId && FriendTypeUtils.isTypeExist(nTypeid)){//
			Utils.showLongToast(FriendTypeInfoActivity.this, "该分类ID已经被使用,\n不能重复使用!");
			return;
		}		
		if(command==DefineUser.ASK_FRIEND_DELETETYPE && FriendUtils.getFriendCount(nTypeid)>0){//
			Utils.showLongToast(FriendTypeInfoActivity.this, "该分类不为空,不能被删除!\n如果你已经删空该分类,\n请刷新该分类好友列表后再试试!");
			return;
		}
		String typeName = uinfo.groupname;
		if (TextUtils.isEmpty(typeName)||typeName.length() < 3){
			Utils.showShortToast(FriendTypeInfoActivity.this, "类型名至少要3个英文字符或者两个汉字！");				
            return;
	    }	
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			EditResult.EditTypeName(DefineUser.TYPE_FRIEND,uinfo); //修改本地数据			
			Utils.showLongToast(FriendTypeInfoActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在发送请求...").show();		
		EditClient.EditFriendType(uinfo,command,new askCallBack(){	
			@Override
			public void onSuccess(int statusCode, String message) {
				getLoadingDialog("正在发送请求...").dismiss();	
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(FriendTypeInfoActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在发送请求...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(FriendTypeInfoActivity.this, str);
					}
				});	
			}
		});	 
	}	
}