package cn.yyxx.commsdk.impl.channel.hnyy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.commsdk.impl.channel.user.UserSession;
import cn.yyxx.commsdk.impl.channel.user.UserSessionManager;


public class UserListAdapter extends BaseAdapter {
	private Context context;
	public ArrayList<UserSession> list;
	private LayoutInflater mInflater = null;



	public UserListAdapter(Context context, ArrayList<UserSession> list) {
		this.context = context;
		this.list = list;
		this.mInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

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

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(ResourceUtil.getLayoutId(context, "hnyy_user_list_item"), parent,false);
			holder = new ViewHolder();

			holder.accountName = convertView
					.findViewById(ResourceUtil.getId(context, "history_account"));
			holder.closeItem =  convertView
					.findViewById(ResourceUtil.getId(context, "item_close"));
			
			holder.closeItem.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					deleteItem(position);
				}
			});
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		holder.accountName.setText(list.get(position).uname);
		return convertView;
	}

	class ViewHolder {
		TextView accountName;
		ImageView closeItem;

	}
	private void deleteItem(int position){
		UserSessionManager.getInstance().removeSession(list.get(position).uname);
	}

}
