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.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;
import cn.net.cpzslibs.prot.handset.bean.res.ProtCheckLabelStatusResBean;

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

	@Override
	public int getCount() {
//		return labels.size() == 0?1:labels.size();
		return labels.size();
	}

	@Override
	public Object getItem(int position) {
//		return labels.size() == 0?position:labels.get(position);
		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(context).inflate(R.layout.adapter_pack_his_list_item, parent,false);
			holder = new ViewHolder();
			holder.lin_pack_adapter_item_root = convertView.findViewById(R.id.lin_pack_adapter_item_root);
			holder.tv_pack_his_serial = (TextView) convertView.findViewById(R.id.tv_pack_his_serial);
			holder.tv_pack_label_str = (TextView) convertView.findViewById(R.id.tv_pack_label_str);
			holder.tv_pack_his_package_label = (TextView) convertView.findViewById(R.id.tv_pack_his_package_label);
			holder.tv_pack_his_describe = (TextView) convertView.findViewById(R.id.tv_pack_his_describe);
			holder.tv_pack_his_count = (TextView) convertView.findViewById(R.id.tv_pack_his_count);
			holder.tv_pack_his_status = (TextView) convertView.findViewById(R.id.tv_pack_his_status);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_pack_his_serial.setText(labels.size() - position +"");
		ProtCheckLabelStatusResBean resLabel = labels.get(labels.size() - position -1);
		if (resLabel.getType() == 1) {
			//
			holder.lin_pack_adapter_item_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_label_invalid_color));
			holder.tv_pack_his_count.setText("验证失败");
			holder.tv_pack_his_package_label.setVisibility(View.GONE);
			holder.tv_pack_his_describe.setVisibility(View.GONE);
			holder.tv_pack_label_str.setVisibility(View.GONE);
			holder.tv_pack_his_status.setVisibility(View.VISIBLE);
			holder.tv_pack_his_status.setText("原    因："+ resLabel.getErrmsg());
		} else {
			holder.tv_pack_his_package_label.setVisibility(View.VISIBLE);
			holder.tv_pack_his_describe.setVisibility(View.VISIBLE);
			holder.tv_pack_label_str.setVisibility(View.VISIBLE);
			holder.tv_pack_label_str.setText("标    签：");
			holder.tv_pack_his_package_label.setText(resLabel.getLabel_id().replaceAll("\\d{4}(?!$)", "$0  "));
			holder.tv_pack_his_describe.setText("验证码：" + resLabel.getConsume_code());
			if (resLabel.getResult() == 0) {
				holder.lin_pack_adapter_item_root.setBackgroundColor(context.getResources().getColor(R.color.white));
				holder.tv_pack_his_count.setText("验证通过");
				holder.tv_pack_his_status.setVisibility(View.VISIBLE);
				String medicine_code = (StringUtils.isEmpty(resLabel.getMedicine_code())? "无" : resLabel.getMedicine_code());
				holder.tv_pack_his_status.setText("药监码：" + medicine_code);
			} else {
				holder.lin_pack_adapter_item_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
				holder.tv_pack_his_count.setText("验证不匹配，错误码：" + resLabel.getErrcode());
				holder.tv_pack_his_status.setVisibility(View.VISIBLE);
				holder.tv_pack_his_status.setText("原    因："+ThreadReqTools.getErrorMsg(resLabel.getErrcode()));
			}
		}
		
		
		
		return convertView;
	}
	

	private class ViewHolder{
		View lin_pack_adapter_item_root;
		TextView tv_pack_label_str;
		TextView tv_pack_his_serial;
		TextView tv_pack_his_package_label;
		TextView tv_pack_his_describe;
		TextView tv_pack_his_count;
		TextView tv_pack_his_status;
	}
}
