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.ReplenishBingBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;

public class ReplenishBingListAdapter extends BaseAdapter {

	private List<ReplenishBingBean> beans;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;

	public ReplenishBingListAdapter(List<ReplenishBingBean> beans) {
		this.beans = beans;
	}
	
	public void refreshData(List<ReplenishBingBean> beans){
		this.beans = beans;
	}


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

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

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		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);
			String tipsStr = "还没有" + HandsetOperation.OPERATION_REPLENISH_BING_CONSTANT.getOptionName()+"数据，快去创建吧^_^";
			tv_no_data_tips.setText(tipsStr);
		} else {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.adapter_sale_his_list_item, parent, false);
				holder = new ViewHolder();
				holder.lin_sale_adapter_item_root = convertView.findViewById(R.id.lin_sale_adapter_item_root);
				holder.tv_sale_his_serial = (TextView) convertView.findViewById(R.id.tv_sale_his_serial);
				holder.tv_sale_his_describe = (TextView) convertView.findViewById(R.id.tv_sale_his_describe);
				holder.tv_sale_his_count = (TextView) convertView.findViewById(R.id.tv_sale_his_count);
				holder.tv_sale_his_status = (TextView) convertView.findViewById(R.id.tv_sale_his_status);
				holder.tv_sale_area = (TextView) convertView.findViewById(R.id.tv_sale_area);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv_sale_his_serial.setText(beans.size() - position + "");
			ReplenishBingBean b = beans.get(position);
			
			holder.tv_sale_area.setText("原托袋码：" + b.getPackageLabel().replaceAll("\\d{4}(?!$)", "$0  "));
			holder.tv_sale_his_describe.setText("原有袋数：" + b.getOriginalCount() +" 袋");
			holder.tv_sale_his_count.setText("可补袋数：" + b.getCanReplenishCount()+" 袋");
			holder.tv_sale_his_status.setTextColor(context.getResources().getColor(R.color.lv_spinner_selected));
			holder.tv_sale_his_status.setText("已扫袋数：" + (position + 1) + " 袋");
			
		}
		return convertView;
	}

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

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

	private class ViewHolder {
		private View lin_sale_adapter_item_root;
		private TextView tv_sale_his_serial, tv_sale_his_describe, tv_sale_his_count, tv_sale_his_status, tv_sale_area;

	}

}
