package cn.net.caoyao.cpzshandset.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.SingleLabelOptionBean;
import cn.net.caoyao.cpzshandset.common.config.Config;

public class SingleLabelWithIdentifyAdapter extends BaseAdapter{
	private List<SingleLabelOptionBean> labels;
	public SingleLabelWithIdentifyAdapter(List<SingleLabelOptionBean> labels) {
		this.labels = labels;
	}
	
	public void refreshData(List<SingleLabelOptionBean> labels){
		this.labels = labels;
		notifyDataSetChanged();
	}

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

	@Override
	public Object getItem(int position) {
//		return labels.get(labels.size() - position -1);
		return labels.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		Context context = parent.getContext();
		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_label_item, parent, false);
			holder = new ViewHolder();
			holder.tv_lv_serial = (TextView) convertView.findViewById(R.id.tv_lv_serial);
			holder.tv_content = (TextView) convertView.findViewById(R.id.tv_lvtext_content);
			holder.image_lv_label_identification = (ImageView) convertView
					.findViewById(R.id.image_lv_label_identification);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
//		SingleLabelOptionBean b = labels.get(labels.size() - position - 1);
		SingleLabelOptionBean b = labels.get(position);
		holder.tv_lv_serial.setText(labels.size() - position +"");
		holder.tv_content.setText(b.getLabel_id().replaceAll("\\d{4}(?!$)", "$0  "));
		
		if (b.getLabel_mode() == Config.LABEL_MODE_ALL_TRAY) {
			holder.image_lv_label_identification.setImageResource(R.drawable.liquor_tray_icon);
		} else {
			holder.image_lv_label_identification.setImageResource(R.drawable.liquor_label_icon);
		}
		
		return convertView;
	}

	private class ViewHolder{
		TextView tv_lv_serial;
		TextView tv_content;
		ImageView image_lv_label_identification;
	}
}
