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.PackPackageInfoBean;

public class BottleBoxListAdapter extends BaseAdapter{
	private List<PackPackageInfoBean> packs;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private boolean isBottle;
	public BottleBoxListAdapter(List<PackPackageInfoBean> packs,boolean isBottle) {
		this.packs = packs;
		this.isBottle = isBottle;
	}
	
	public void refreshData(List<PackPackageInfoBean> packs){
		this.packs = packs;
		notifyDataSetChanged();
	}

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

	@Override
	public Object getItem(int position) {
		return packs.size() == 0?position:packs.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 (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_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_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);
				holder.tv_pack_label_str = (TextView) convertView.findViewById(R.id.tv_pack_label_str);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv_pack_his_serial.setText(packs.size() - position +"");
			PackPackageInfoBean p = packs.get(position);
			if (isBottle) {
				holder.tv_pack_label_str.setText("盒码：");
				holder.tv_pack_his_package_label.setText(p.getBox_label().replaceAll("\\d{4}(?!$)", "$0  "));
				
				holder.tv_pack_his_describe.setText("瓶码："+p.getBottle_label().replaceAll("\\d{4}(?!$)", "$0  "));
			} else {
				holder.tv_pack_label_str.setText("箱        码：");
				holder.tv_pack_his_package_label.setText(p.getBox_label().replaceAll("\\d{4}(?!$)", "$0  "));
				holder.tv_pack_his_describe.setText("盒码个数：" + p.getCount());
			}
			
			holder.tv_pack_his_count.setVisibility(View.GONE);
			holder.tv_pack_his_status.setVisibility(View.GONE);
		}
		
		
		
		return convertView;
	}
	
	@Override
	public int getItemViewType(int position) {
		return packs.size() == 0?NO_DATE_VIEW : NORMAL_VIEW;
	}
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	private class ViewHolder{
		TextView tv_pack_label_str;
		View lin_pack_adapter_item_root;
		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;
	}
}
