package com.lianxin.pubqq.setup;

import java.util.List;
import android.content.Context;
//import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.list.bean.Address;
import com.lianxin.panqq.list.bean.Member;
import com.lianxin.panqq.common.utils.PowerUtils;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.utils.ImagesUtils;



public class ReplyListAdapter1 extends BaseAdapter{

	private List<Member> mList;
	private LayoutInflater mInflater;
//	private UserListActivity mContext;
	private Context mContext;	
	 
	//private TextView zan_tv;
//	private ZanDao zdao ;
//	public UserListAdapter(UserListActivity context,List<UserList> userList) {	
	public ReplyListAdapter1(Context context,List<Member> userList) {
		mInflater = LayoutInflater.from(context);
		mContext=context;
		this.mList=userList;
		//判断数据库中是否有这个赞
	//	zdao = new ZanDao(mContext);
	}

	@Override
	public int getCount() {
		return mList==null?0:mList.size();
	}

	@Override
	public Member getItem(int position) {
		return mList.get(position);
	}

 
	public String getUser(int position) {
		return mList.get(position).getName();
	}
	
	 @Override
	public long getItemId(int position) {
		//return getItem(position).id;
		//String s = mList.get(position).getId(); 
		//return Long.valueOf(s).longValue();
		return position;		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_memberlist, null);
			holder.avator=(ImageView)convertView.findViewById(R.id.avator);
			holder.name=(TextView)convertView.findViewById(R.id.name);
			holder.content = (TextView) convertView.findViewById(R.id.content);
//			holder.gridView=(NoScrollGridView)convertView.findViewById(R.id.gridView);
	//		holder.time = (TextView) convertView.findViewById(R.id.time);
	//		holder.zan = (TextView) convertView.findViewById(R.id.zan); 
			//zan_tv = holder.zan;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
	//	if(position<80){
		final Member bean = getItem(position);
		
	//	ImageLoader.getInstance().displayImage(bean.getAvator(), holder.avator,ImageOptions.getOptions());
		//ImageLoader.getInstance().displayImage(bean.getAvator(), holder.avator);
	//	holder.avator.setImageResource(R.drawable.h008);
		int imageid = bean.imageid%80;
		holder.avator.setImageResource(ImagesUtils.images[imageid]);	
		String strname = bean.getName()+"("+bean.getUserId()+")";
	/*	int userid = bean.getUserId();
		if(userid == GloableParams.m_szUserId) //如果是好友
			userid = bean.getClass();
		String strname = bean.getName()+"("+bean.getUserId()+")";	*/	
//		String strContent = ""+(position+1)+","+bean.sex+","+bean.age+"岁,"+getAddress(bean.address);
//		String strContent = ""+(position+1)+","+bean.power+","+bean.index;	
		String strContent = ""+(position+1)+","+PowerUtils.getPowerText(DefineUser.TYPE_CROWD,bean.getPower())+","+bean.index;		
		holder.name.setText(strname);
		holder.content.setText(strContent);
//		holder.name.setText(bean.getName());
//		holder.content.setText(bean.getContent());
	//	}
		//设置时间显示
		//holder.time.setText(jisuan(bean.getTime()));
	/*	if(bean.getTime()>0)
			holder.time.setText(TimeUtil.getMsgTime(bean.getTime()));
		else
			holder.time.setText(bean.getstrTime());		*/ 
		//判断当前故事是否是自己的
		final int pposition = position;
	/*	if(bean.getUser().equals(MyApp.getInstance().getUser())){
			if(CommonUtils.isNullOrEmpty(bean.getZan())){
				holder.zan.setText(bean.getZan()+"删除");
			}else{
				holder.zan.setText(bean.getZan()+"人赞    删除");
			}
			holder.zan.setOnClickListener(new View.OnClickListener() { 
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					mContext.del_gushi(pposition);
					//mContext.get
				}
			});
			
		}else{*/
			 //System.out.println(bean.getId());
			/*if(zdao.getZanGushi(bean.getId())){
				holder.zan.setText("♥人已赞");
			}else{
				holder.zan.setText("♡人赞");
			} */
		/*	holder.zan.setText("♥人已赞");		
			//点赞事件
			holder.zan.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) { 
					if(zdao.getZanGushi(bean.getId())){
						holder.zan.setText("♡人赞");
						zdao.deleteContact(bean.getId());
					}else{
						holder.zan.setText("♥人已赞");
						zdao.saveContact(bean.getId());
					} 
				}
			});*/
	//	}
		
		
	/*	if(bean.getUrls()!=null&&bean.getUrls().length>0){
			holder.gridView.setVisibility(View.VISIBLE);
			holder.gridView.setAdapter(new MyGridAdapter(bean.getUrls(), mContext));
			holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					imageBrower(position,bean.getUrls());
				}
			});
		}else{
			holder.gridView.setVisibility(View.GONE);
		}*/
		return convertView;
	}

	private void imageBrower(int position, String[] urls) {
	/*	Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);*/
	}
	private static class ViewHolder {

		public TextView name;
		public ImageView avator;
		TextView content;
//		TextView time;
//		TextView zan;
	//	NoScrollGridView gridView;
//		int position;
	}
	
	private String getAddress(int address){
	    List<Address> AddressList = GloableParams.AddressList;		
		String strprovince="";
		String strcity="";	
		int type = -1;
		for (int i = 0; i < AddressList.size(); i++) {
			if(AddressList.get(i).getId()==address)
			{
				strcity = AddressList.get(i).getName();
				type = AddressList.get(i).gettype();
			}
		}
		if(type<0) return "";
		if(type==0) return strcity;	
		for (int i = 0; i < AddressList.size(); i++) {
			if(AddressList.get(i).getId()==type)
			{
				strprovince = AddressList.get(i).getName();
				type = AddressList.get(i).gettype();
			}
		}	
		return strprovince+strcity;			
	}
	/*@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}*/
	
/*	@SuppressLint("SimpleDateFormat")
	private String jisuan(String shijianchuo){
		  long unixLong = 0;
		  StringBuffer sb = null;
		  try {
              unixLong = Long.parseLong(shijianchuo) * 1000;
           } catch(Exception ex) {
              //System.out.println("String转换Long错误，请确认数据可以转换！");
         }
		  
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	       java.util.Date now ;
	    try {
	        now = df.parse(df.format(new Date())); 
	      // java.util.Date date=df.parse("2004-01-02 11:30:24");
	       java.util.Date date =  df.parse(df.format(unixLong));//获取参数时间
	       long l=now.getTime()- date.getTime();
	       long day=l/(24*60*60*1000);
	       long hour=(l/(60*60*1000)-day*24);
	       long min=((l/(60*1000))-day*24*60-hour*60);
	       long s=(l/1000-day*24*60*60-hour*60*60-min*60);
	        
	        sb = new StringBuffer();
	       //sb.append("发表于：");
	       if(day > 0){
	           sb.append(day+"天前");
	       }else if(hour > 0 ){
	           sb.append(hour+"小时前");
	       }else if(min > 0 ){
	           sb.append(min+"分钟前");
	       }else{
	    	   sb.append("刚刚");
	       }
	      // sb.append(s+"秒 前");
	      // sb.append("前");
	       //System.out.println(sb.toString());
	    } catch (ParseException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	    
		return sb.toString();
	       
	}*/
}
