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

public class KIAActivateHisListAdapter extends BaseAdapter {
	private List<ActivateBean> aBeans;
	
	private SingleLabelBll singleLabelBll;
	private BatchLabelBll batchLabelBll;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private HandsetOperation option_type;

	public KIAActivateHisListAdapter(List<ActivateBean> aBeans,SingleLabelBll singleLabelBll,
			BatchLabelBll batchLabelBll,HandsetOperation option_type) {
		this.aBeans = aBeans;
		
		this.singleLabelBll = singleLabelBll;
		this.batchLabelBll = batchLabelBll;
		this.option_type = option_type;
	}

	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);
			// 0：未提交 1：提交失败
			if (aBean.getStatus() == 0) {
				holder.view_adapter_activate_his_root
						.setBackgroundColor(context.getResources().getColor(R.color.white));
				holder.tv_activate_his_upload_status.setText("状态：未提交");
			} else {
				holder.view_adapter_activate_his_root
						.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
				String errorMsg = aBean.getType() == 1 ? "状态：提交失败，请点击进入子页面查看原因" :"状态：提交失败(错误码：" + aBean.getError_code() + ")" + "\n原因："
						+ ThreadReqTools.getErrorMsg(aBean.getError_code());
				holder.tv_activate_his_upload_status.setText(errorMsg);
			}
			
			//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);*/

			int count = 0;
			if (aBean.getType() == 0) {
				if (aBean.getOption_mode() == 30) {
					holder.tv_activate_his_p_number.setText("激活方式：单标激活");
					count = singleLabelBll.queryLabelCountById(option_type.getOption(),
							aBean.getId());
					// LogUtil.d(TConfig.Echo, aBean.getId() +" 单标激活数量：" +
					// count); 
				} else if (aBean.getOption_mode() == 31) {
					holder.tv_activate_his_p_number.setText("激活方式：批量激活");
					count = batchLabelBll.queryBatchCountById(option_type.getOption(),
							aBean.getId());
				} else {
					holder.tv_activate_his_p_number.setText("激活方式：套标激活");
					count = singleLabelBll.queryLabelCountById(option_type.getOption(),
							aBean.getId());
				}
			} else {
				count = singleLabelBll.queryLabelCountById(
						HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption(), aBean.getId());
			}

			if (aBean.getType() == 1) {
				String productBatch = (StringUtils.isEmpty(aBean.getProduct_batch_number()) ? "无" : aBean.getProduct_batch_number());
				holder.tv_activate_his_p_number.setText("生产批号：" + productBatch);
			}

			holder.tv_activate_his_label_count.setText("数量：" + count);

			if (aBean.getType() == 1)
				holder.tv_activate_his_label_count.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;
	}

}
