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.ActivateBean;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;

public class RelationPackHisListAdapter extends BaseAdapter {
	private List<ActivateBean> aBeans;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private boolean isBottle;

	public RelationPackHisListAdapter(List<ActivateBean> aBeans,boolean isBottle) {
		this.aBeans = aBeans;
		this.isBottle = isBottle;
	}

	public void refreshActivateHisData(List<ActivateBean> aBeans) {
		this.aBeans = aBeans;
		notifyDataSetChanged();
	}

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

	@Override
	public Object getItem(int position) {
		return aBeans.size() == 0 ? position : aBeans.get(aBeans.size());
	}

	@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 (getItemViewType(position) == NO_DATE_VIEW) {
			// 显示无数据界面
			convertView = LayoutInflater.from(context).inflate(R.layout.adapter_no_data_view, parent, false);
			TextView tv_no_data_tips = (TextView) convertView.findViewById(R.id.tv_no_data_tips);
			tv_no_data_tips.setText("目前还没有数据，快去创建吧^_^");
		} else {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.adapter_activate_his_list_item, parent,
						false);
				holder = new ViewHolder();
				holder.view_adapter_activate_his_root = convertView.findViewById(R.id.view_adapter_activate_his_root);
				holder.tv_activate_his_serial = (TextView) convertView.findViewById(R.id.tv_activate_his_serial);
				holder.tv_activate_his_p_number = (TextView) convertView.findViewById(R.id.tv_activate_activate_type);
				holder.tv_activate_his_p_name = (TextView) convertView.findViewById(R.id.tv_activate_his_p_name);
				holder.tv_activate_his_p_date = (TextView) convertView.findViewById(R.id.tv_activate_his_p_date);
				holder.tv_activate_his_label_count = (TextView) convertView
						.findViewById(R.id.tv_activate_his_label_count);
				holder.tv_activate_his_upload_status = (TextView) convertView
						.findViewById(R.id.tv_activate_his_upload_status);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv_activate_his_serial.setText((aBeans.size() - position) + "");
			// ActivateBean aBean = aBeans.get(aBeans.size()- position-1);
			ActivateBean aBean = aBeans.get(position);

			holder.tv_activate_his_label_count.setVisibility(View.GONE);
			// Product p = productBll.queryProductById(aBean.getProduct_id());
			holder.tv_activate_his_p_name.setText("产品名称：" + aBean.getProduct_id());
			String pDate = StringUtils.isEmpty(aBean.getProduct_date()) ? "无" : aBean.getProduct_date();
			holder.tv_activate_his_p_date.setText("生产日期：" + pDate);

			holder.tv_activate_his_p_number.setText("生产批号：" + aBean.getProduct_batch_number());

			if (isBottle) {
				holder.tv_activate_his_upload_status.setText("盒码数量：" + aBean.getPackCount());
			} else {
				holder.tv_activate_his_upload_status.setText("箱码数量：" + aBean.getPackCount());
			}
			
		}

		return convertView;
	}

	@Override
	public int getItemViewType(int position) {
		return aBeans.size() == 0 ? NO_DATE_VIEW : NORMAL_VIEW;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	private class ViewHolder {
		private View view_adapter_activate_his_root;
		private TextView tv_activate_his_serial, tv_activate_his_p_name, tv_activate_his_p_number,
				tv_activate_his_p_date, tv_activate_his_label_count, tv_activate_his_upload_status;
	}

}
