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.PackBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;

public class PackHisListAdapter extends BaseAdapter{
	private List<PackBean> packs;
//	private SingleLabelBll labelBll;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private int option_type = HandsetOperation.OPERATION_PACK_CONSTANT.getOption();
	public PackHisListAdapter(List<PackBean> packs) {
		this.packs = packs;
//		this.labelBll = labelBll;
	}
	
	public void refreshData(List<PackBean> 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.lin_pack_package_label = convertView.findViewById(R.id.lin_pack_package_label);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv_pack_his_serial.setText(packs.size() - position +"");
			PackBean p = packs.get(position);
//			String packageLabel = StringUtils.isEmpty(p.getPackage_label_id())?"无" :p.getPackage_label_id();
//			holder.tv_pack_his_package_label.setText(packageLabel.replaceAll("\\d{4}(?!$)", "$0  "));
			holder.lin_pack_package_label.setVisibility(View.GONE);
			String describeStr = StringUtils.isEmpty(p.getDescribe())?"无":p.getDescribe();
			holder.tv_pack_his_describe.setText("描    述："+describeStr);
			int count = (p.getCount()==0 ? 2000:p.getCount());
//			int currentCount = labelBll.queryLabelCountById(option_type, p.getId());//该项需要去数据库统计子标签的个数
			
			holder.tv_pack_his_count.setText("数    量："+count);
			
			if (p.getStatus() == 0) {
				holder.tv_pack_his_status.setText("状    态：未提交");
				
				holder.lin_pack_adapter_item_root.setBackgroundColor(context.getResources().getColor(R.color.white));
			} else {
				holder.tv_pack_his_status.setText("状    态：提交失败(错误码："+p.getError_code()+")\n原因：" + ThreadReqTools.getErrorMsg(p.getError_code()));
				holder.lin_pack_adapter_item_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
			}
			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{
		View lin_pack_adapter_item_root,lin_pack_package_label;
		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;
	}
}
