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.UnpackBean;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;

public class UnpackHisListAdapter extends BaseAdapter {
	private List<UnpackBean> unpacks;

	public UnpackHisListAdapter(List<UnpackBean> unpacks) {
		this.unpacks = unpacks;
	}

	public void refreshData(List<UnpackBean> unpacks) {
		this.unpacks = unpacks;
		notifyDataSetChanged();
	}

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

	@Override
	public Object getItem(int position) {
		return unpacks.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Context context = parent.getContext();
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.adapter_unpack_his_list_item, parent, false);
			holder = new ViewHolder();
			holder.lin_unpack_adapter_item_root = convertView.findViewById(R.id.lin_unpack_adapter_item_root);
			holder.tv_unpack_his_serial = (TextView) convertView.findViewById(R.id.tv_unpack_his_serial);
			holder.tv_unpack_his_unpackage_label = (TextView) convertView
					.findViewById(R.id.tv_unpack_his_unpackage_label);
			holder.tv_unpack_his_status = (TextView) convertView.findViewById(R.id.tv_unpack_his_status);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.tv_unpack_his_serial.setText(unpacks.size() - position + "");
		UnpackBean b = unpacks.get(position);
		holder.tv_unpack_his_unpackage_label.setText(b.getLabel_id().replaceAll("\\d{4}(?!$)", "$0  "));

		if (b.getStatus() == 0) {
			holder.lin_unpack_adapter_item_root.setBackgroundColor(context.getResources().getColor(R.color.white));
			holder.tv_unpack_his_status.setText("状态：未提交");
//			holder.tv_unpack_his_status.setVisibility(View.GONE);
		} else {
			holder.lin_unpack_adapter_item_root
					.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
			holder.tv_unpack_his_status.setText("状态：提交失败(错误码：" + b.getError_code() + ")\n原因：" + ThreadReqTools.getErrorMsg(b.getError_code()));
//			holder.tv_unpack_his_status.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

	private class ViewHolder {
		private View lin_unpack_adapter_item_root;
		private TextView tv_unpack_his_serial;
		private TextView tv_unpack_his_unpackage_label;
		private TextView tv_unpack_his_status;
	}
}
