package cn.net.caoyao.cpzshandset.ui.vus;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.InvoiceBean;
import cn.net.caoyao.cpzshandset.service.bll.InvoiceBll;

public class TrayLabelItemView implements Vu{
	View rootView;
	TextView tv_lv_serial;
	TextView tv_content;
	ImageView image_lv_label_identification;
	private int trayCount = -1;
	private InvoiceBll invoiceBll;

	@Override
	public void init(LayoutInflater inflater, ViewGroup container) {
		rootView = inflater.inflate(R.layout.adapter_label_item, container, false);
		tv_lv_serial = (TextView) rootView.findViewById(R.id.tv_lv_serial);
		tv_content = (TextView) rootView.findViewById(R.id.tv_lvtext_content);
		image_lv_label_identification = (ImageView) rootView
				.findViewById(R.id.image_lv_label_identification);
	}

	@Override
	public View getView() {
		return rootView;
	}
	
	public void setOnlineInvoiceView(List<InvoiceBean> invoices,int position,Context context){
//		if(position == 2) invoices.get(position).setLabelStatus(1);//测试使用
		tv_lv_serial.setVisibility(View.VISIBLE);
		if (invoices.get(position).getLabelStatus() == 1) {
			rootView.setBackgroundColor(rootView.getContext().getResources().getColor(R.color.adapter_item_label_invalid_color));
		} else {
			rootView.setBackgroundColor(rootView.getContext().getResources().getColor(R.color.white));
		}
		
		tv_lv_serial.setVisibility(View.VISIBLE);
		image_lv_label_identification.setImageResource(R.drawable.label_icon);
		tv_lv_serial.setBackgroundResource(R.drawable.tv_serial_label_number_bg);
//		tv_lv_serial.setTextColor(context.getResources().getColor(R.color.tv_serial_label_number_text_color));
		tv_lv_serial.setTextColor(tv_lv_serial.getContext().getResources().getColor(R.color.white));
		tv_lv_serial.setText("" + (invoices.size() - position));
		
		tv_content.setText(invoices.get(position).getLabelid().replaceAll("\\d{4}(?!$)", "$0  "));
	}
	public void setInvoiceView(List<InvoiceBean> invoices,int position,Context context, int transid,int transmode){
		
		if (invoices.get(position).getElemtype() == 1) {
			// 托盘
			tv_lv_serial.setText("" + (invoices.size() - position));
			image_lv_label_identification.setImageResource(R.drawable.tray_icon);
			tv_lv_serial.setBackgroundResource(R.drawable.tv_serial_number_bg);
			tv_lv_serial.setTextColor(tv_lv_serial.getContext().getResources().getColor(R.color.tv_serial_tray_number_text_color));
		} else if (invoices.get(position).getElemtype() == 2) {
			// 标签
			image_lv_label_identification.setImageResource(R.drawable.label_icon);
			if (trayCount < 0) {
				trayCount = getInvoiceBll(context).queryTrayCount(transid, transmode);
//				LogUtil.d(TConfig.Echo, "trayCount:" + trayCount);
			}
			tv_lv_serial.setBackgroundResource(R.drawable.tv_serial_label_number_bg);
//			tv_lv_serial.setTextColor(context.getResources().getColor(R.color.tv_serial_label_number_text_color));
			tv_lv_serial.setTextColor(tv_lv_serial.getContext().getResources().getColor(R.color.white));
			tv_lv_serial.setText("" + (invoices.size() - position - trayCount));
		}

		tv_content.setText(invoices.get(position).getLabelid().replaceAll("\\d{4}(?!$)", "$0  "));
	}
	
	private InvoiceBll getInvoiceBll(Context context) {
		return invoiceBll == null ? new InvoiceBll(context) : invoiceBll;
	}

}
