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.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.SingleLabelOptionBean;
import cn.net.caoyao.cpzshandset.common.config.InternalOptionConstant;

public class SingleLabelAdapter extends BaseAdapter{
	private List<SingleLabelOptionBean> labels;
	public SingleLabelAdapter(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_single_label_item, parent, false);
			holder = new ViewHolder();
			holder.lin_single_label_root = convertView.findViewById(R.id.lin_single_label_root);
			holder.tv_single_label_serial = (TextView) convertView.findViewById(R.id.tv_single_label_serial);
			holder.tv_single_label_item_content = (TextView) convertView.findViewById(R.id.tv_single_label_item_content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
//		SingleLabelOptionBean b = labels.get(labels.size() - position - 1);
		SingleLabelOptionBean b = labels.get(position);
		holder.tv_single_label_serial.setText(labels.size() - position +"");
		holder.tv_single_label_item_content.setText(b.getLabel_id().replaceAll("\\d{4}(?!$)", "$0  "));
		if (b.getLabelStatus() == InternalOptionConstant.SINGLE_LABEL_STATUS_INVALID) {
			holder.lin_single_label_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_label_invalid_color));
		} else if (b.getLabelStatus() == InternalOptionConstant.SINGLE_LABEL_STATUS_NO_ACTIVATE ) {
			holder.lin_single_label_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_label_no_activate_color));
		} else if (b.getLabelStatus() == InternalOptionConstant.SINGLE_LABEL_STATUS_REPEAT_ACTIVATE) {
			holder.lin_single_label_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
		} else if( b.getLabelStatus() == InternalOptionConstant.SINGLE_LABEL_STATUS_NOT_PACKAGE_LABEL){
			holder.lin_single_label_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_label_not_package_color));
		}else if( b.getLabelStatus() == InternalOptionConstant.SINGLE_LABEL_STATUS_STACK_OUT_REPEAT){
			holder.lin_single_label_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
		}else {
			holder.lin_single_label_root.setBackgroundColor(context.getResources().getColor(R.color.white));
		}
		return convertView;
	}

	private class ViewHolder{
		private TextView tv_single_label_serial;
		private TextView tv_single_label_item_content;
		private View lin_single_label_root;
	}
}
