package com.lianxin.pubqq.ulive;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.lianxin.panqq.list.bean.Member;
import com.lianxin.panqq.main.EMGroupManager;
import com.lianxin.pubqq.R;
import com.lianxin.pubqq.ulive.adpter.MeetHeadGridAdapter;
import com.lianxin.panqq.widget.ExpandGridView;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.chat.BaseMeetingActivity;
import com.lianxin.panqq.chat.EMMeetingManager;
import com.lianxin.panqq.chat.entity.EMRoomStateChangeListener;
import com.lianxin.panqq.utils.ImagesUtils;

//语音会议(上线的在上面,没上线的在下面.各人控制视频开关语音开关)
public class MeetingAudioActivity extends BaseMeetingActivity implements
		OnClickListener {
	private TextView txt_title;	 //对话框标题
	private TextView cap_groupname; //群组标题
	private TextView tv_groupname; //群组名称	
	private TextView cap_groupid; //群组id标题
	private TextView tv_groupid; //群组id	
	// 修改群名称、置顶、、、、
	private RelativeLayout re_groupname;	
	//int m_total = 0;// 成员总数
	private ExpandGridView headview;// 成员列表	
	private HeadGridAdapter headadapter;
	// 状态变化
	private CheckBox check_camera, check_speaker;	
	private Button startBtn,exitBtn;
	private TextView roomStateTextView;	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);	
		setContentView(R.layout.live_activity_meetingaudio);	
		meetingMode = 1;	
		initControl();
		initMeeting(); //初始化会议环境
		initGroupData();//初始化会议成员数据
		initView();

		// 初始化控件
		initPageView();
		// 添加业务逻辑
		//initPageData();			
		setListener();
		// 设置房间监听
		addRoomStateListener();			
	}

	protected void initControl() {
		//注释性控件
		txt_title = (TextView) findViewById(R.id.txt_title);

		cap_groupname = (TextView) findViewById(R.id.cap_groupname);
		tv_groupname = (TextView) findViewById(R.id.txt_groupname);	
		cap_groupid = (TextView) findViewById(R.id.cap_groupid);
		tv_groupid = (TextView) findViewById(R.id.txt_groupid);
		//主控件
		headview = (ExpandGridView) findViewById(R.id.headview);
		
		//re_groupname = (RelativeLayout) findViewById(R.id.re_change_groupname);
		//rl_switch_camera = (RelativeLayout) findViewById(R.id.rl_switch_camera);
		//rl_switch_speaker = (RelativeLayout) findViewById(R.id.rl_switch_speaker);
		//re_state = (RelativeLayout) findViewById(R.id.re_state);
		check_camera = (CheckBox)findViewById(R.id.check_camera);
		check_speaker = (CheckBox)findViewById(R.id.check_speaker);
		
		exitBtn = (Button) findViewById(R.id.btn_exit_talk);
		startBtn = (Button) findViewById(R.id.btn_start_talk);		
		roomStateTextView = (TextView) findViewById(R.id.tv_room_state);		
	}

	private void initPageView() {
		// TODO Auto-generated method stub
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		ivGroup = (LinearLayout) findViewById(R.id.points);
		onLineLists = EMGroupManager.getInstance().getGroupMembers(groupId,groupType);
		mPageSize = 16; // 每页显示的最大的数量	
		initPageData();		

	}	
	


	protected GridView  initGridView( int n ) {
		GridView gridView = (GridView) View.inflate(this,
				R.layout.live_gridview_audio, null);
		//GridView gridView = (ExpandGridView) findViewById(R.id.gridview);		
		gridView.setAdapter(new MeetHeadGridAdapter(this, onLineLists, n,
				mPageSize));
		// 添加item点击监听
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

			}
		});
		return gridView;
	}	
	
	protected void initView() {
		//tv_groupname.setText(groupName);
		setCaption();
		// 显示群组成员头像和昵称
		//if (members != null)
	//	showMembersPlay(players);
		showMembersHead(offLineLists);
		// 保证每次进详情看到的都是最新的group
		//updateGroup();
	}

    //按照类别设置标题
	protected void setCaption() {
		tv_groupid.setText(""+groupId);		
		tv_groupname.setText(groupName);		
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			txt_title.setText("语音群聊");
			cap_groupid.setText("分类ID");
			cap_groupname.setText("分类名称");
			//cap_type.setText("关  键  字");
			//cap_enyear.setText("兴趣地域");	
			break;		
		case DefineUser.TYPE_CROWD:
			txt_title.setText("语音群聊");
			cap_groupid.setText("群ID");
			cap_groupname.setText("群名称");
			//cap_type.setText("关  键  字");
			//cap_enyear.setText("兴趣地域");			
			break;
		case DefineUser.TYPE_MYCLASS:
			txt_title.setText("班语音群聊");
			cap_groupid.setText("班ID");
			cap_groupname.setText("班名称");
			//cap_type.setText("所属院系");
			//cap_enyear.setText("入学年份");	
			break;
		case DefineUser.TYPE_DEPART:
			txt_title.setText("部门语音群聊");
			cap_groupid.setText("部门ID");
			cap_groupname.setText("部门名称");
			//cap_type.setText("所属父项");
			//cap_enyear.setText("主管部门");	
			break;		
		case DefineUser.TYPE_PARTY:
			txt_title.setText("会议室语音群聊");
			cap_groupid.setText("会议ID");
			cap_groupname.setText("会议名称");
			//cap_type.setText("所属父项");
			//cap_enyear.setText("主管部门");	
			break;
		case DefineUser.TYPE_GROUP:
			txt_title.setText("俱乐部语音群聊");
			cap_groupid.setText("俱乐部ID");
			cap_groupname.setText("俱乐部名");
			//cap_type.setText("所属省份");
			//cap_enyear.setText("所在城市");	
			break;
		case DefineUser.TYPE_CITY:
			txt_title.setText("地域语音群聊");
			cap_groupid.setText("地域ID");
			cap_groupname.setText("地域名称");
			//cap_type.setText("所属省份");
			//cap_enyear.setText("所在城市");	
			break;		
	
		default:
			txt_title.setText("语音群聊");
			cap_groupid.setText("群ID");
			cap_groupname.setText("群名称");
			//cap_type.setText("关  键  字");
			//cap_enyear.setText("兴趣地域");				
			break;
		}			
	}	
	// 显示群成员头像昵称的gridview
/*	private void showMembersPlay(List<Member> members) {
		playadapter = new  PlayGridAdapter(this, members);
		gridview.setAdapter(playadapter);

	}*/

	// 显示群成员头像昵称的gridview
	private void showMembersHead(List<Member> members) {
		headadapter = new HeadGridAdapter(this, members);
		headview.setAdapter(headadapter);

	}

	protected void setListener() {
		check_camera.setChecked(false);
		check_speaker.setChecked(false);		
		//re_groupname.setOnClickListener(this);
		//rl_switch_camera.setOnClickListener(this);
		//rl_switch_speaker.setOnClickListener(this);
		//re_state.setOnClickListener(this);
		exitBtn.setOnClickListener(this);
		startBtn.setOnClickListener(this);		
		check_camera.setOnClickListener(this);
		check_speaker.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			postHanderMessage(MSG_MEETING_EXITROOM, 10000);
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
			}
			Utils.finish(MeetingAudioActivity.this);
		} else if (id == R.id.btn_exit_talk) {
			postHanderMessage(MSG_MEETING_EXITROOM, 10000);
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
			}
			Utils.finish(MeetingAudioActivity.this);
		} else if (id == R.id.btn_start_talk) {
			postHanderMessage(MSG_MEETING_JOINROOM, 10000);
		} else if (id == R.id.check_camera) {
			isOpenCamera = ((CheckBox) v).isChecked();
		} else if (id == R.id.check_speaker) {
			isOpenSpeaker = ((CheckBox) v).isChecked();
		}
	}

	@Override
	protected void onDestroy() {
		
		stHandler.sendEmptyMessage(MSG_MEETING_EXITROOM);
   
		super.onDestroy();        

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		//moveTaskToBack(true);		
	}

	// 群组成员gridadapter
	private class HeadGridAdapter extends BaseAdapter {

		//public boolean isInDeleteMode;
		private List<Member> Members;		
		Context context;

		public HeadGridAdapter(Context context, List<Member> objects) {

			this.Members = objects;
			this.context = context;
			//isInDeleteMode = false;
		}
		@Override
		public View getView(final int position, View convertView,
				final ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_meethead_gridview, null);
			}
			ImageView iv_avatar = (ImageView) convertView
					.findViewById(R.id.iv_avatar);
			TextView tv_username = (TextView) convertView
					.findViewById(R.id.tv_username);

				final Member user = Members.get(position);
				final String usernick = user.getName();
				final int userid = user.getUserId();
				int imageid = user.getHeadImg();
			if(userid<=1000){
				iv_avatar.setImageResource(R.drawable.empty_photo);	
				
			}else{
				if(imageid<0||imageid>80)
					imageid = imageid%80;
				final String useravatar = user.getHeadUrl();
				tv_username.setText(usernick);
	
				iv_avatar.setImageResource(ImagesUtils.images[imageid]);							
		//		iv_avatar.setImageResource(R.drawable.empty_photo);	
				iv_avatar.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						addPlayerFromMembers(user);

					}
				});
			}
			return convertView;
		}

		@Override
		public int getCount() {
			return Members.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return Members.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
	}	
	
	public void refresh() {
    	headadapter.notifyDataSetChanged(); 
    	//playadapter.notifyDataSetChanged();
	}
	
	/**
	 * 设置房间状态监听
	 */
	void addRoomStateListener() {
		EMMeetingManager.getInstance().addRoomStateChangeListener(
			new EMRoomStateChangeListener() {
				@Override
				public void onError(int errorCode, String errorMessage) {
					final String str = errorMessage;
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
								// callStateTextView.setText(str);
						}
					});
				}

				@Override
				public void onProgress(int progress, String status) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							String str = "发送"
										+ GloableParams.n_MediaSendCount
										+ " 接收"
										+ GloableParams.n_MediaRecvCount;
								// callStateTextView.setText(str);
						}
					});
				}

				@Override
				public void onCallStateChanged(CallState callState,
							CallError error) {

				}

				@Override
				public void onUserTextCome(int userid, String message) {

				}

				@Override
				public void onUserCustomCome(int userid, byte[] data,
							int length) {

				}

				@Override					
			    public void onUserJoinRoom(int userid, String nickname) {
					final int userid1 = userid;
					final String nickname1 = nickname;						
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							String str = ""+nickname1+"("+userid1+")进入了";								
							roomStateTextView.setText(str);
						}
					});
			    }
				@Override					
			    public void onUserExitRoom(int userid, String nickname) {
					final int userid1 = userid;
					final String nickname1 = nickname;						
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							String str = ""+nickname1+"("+userid1+")离开了";								
							roomStateTextView.setText(str);
						}
					});
			    }
				@Override					
			    public void onOtherJoinRoom(int userid, String nickname) {
					final int userid1 = userid;
					final String nickname1 = nickname;						
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							String str = ""+nickname1+"("+userid1+")进入了";								
							roomStateTextView.setText(str);
						}
					});
			    }
				@Override					
			    public void onOtherExitRoom(int userid, String nickname) {
					final int userid1 = userid;
					final String nickname1 = nickname;						
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							String str = ""+nickname1+"("+userid1+")离开了";								
							roomStateTextView.setText(str);
						}
					});
			    }
			});
	}	
}
