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

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.City;
import cn.net.caoyao.cpzshandset.bean.WarehouseBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.CityBll;
import cn.net.caoyao.cpzshandset.service.bll.SingleLabelBll;
import cn.net.caoyao.cpzshandset.service.tools.SaleAddressUtil;
import cn.net.caoyao.cpzshandset.ui.adapter.base.BaseArrayListAdapter;

public class WarehouseHisListArrayAdapter extends BaseArrayListAdapter<WarehouseBean> {
	private List<WarehouseBean> beans;
	private SingleLabelBll labelBll;
	private CityBll cityBll;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private int option_type;
	private int out_warehouse = HandsetOperation.OPERATION_OUT_WAREHOUSE_CONSTANT.getOption();

	public WarehouseHisListArrayAdapter(List<WarehouseBean> beans, SingleLabelBll labelBll,int option_type) {
		this.beans = beans;
		this.labelBll = labelBll;
		this.option_type = option_type;
	}

	public void refreshData(List<WarehouseBean> beans) {
		this.beans = beans;
		notifyDataSetChanged();
	}

	@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 int getItemViewType(int position) {
		return beans.size() == 0?NO_DATE_VIEW : NORMAL_VIEW;
	}
	
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public cn.net.caoyao.cpzshandset.ui.adapter.base.BaseArrayListAdapter.ViewHolder getViewHolder(View convertView,
                                                                                                   ViewGroup parent, int position) {
		Context context = parent.getContext();
		if (getItemViewType(position) == NO_DATE_VIEW) {
			// 显示无数据界面
			ViewHolder holder1 = ViewHolder.get(context, convertView, parent, R.layout.adapter_no_data_view);
			TextView tv_no_data_tips = holder1.findViewById(R.id.tv_no_data_tips);
			tv_no_data_tips.setText("目前还没有数据，快去创建吧^_^");
			return holder1;
		} else {
			ViewHolder holder2 = ViewHolder.get(context, convertView, parent, R.layout.adapter_warehouse_his_list_item);
			setAdapterDataView(holder2, position,context);
			return holder2;
		}
	}

	private void setAdapterDataView(cn.net.caoyao.cpzshandset.ui.adapter.base.BaseArrayListAdapter.ViewHolder holder2,
			int position,Context context) {
			View lin_warehouse_adapter_item_root = holder2.findViewById(R.id.lin_warehouse_adapter_item_root);
			TextView tv_warehouse_his_serial = (TextView) holder2.findViewById(R.id.tv_warehouse_his_serial);
			TextView tv_warehouse_his_describe = (TextView) holder2.findViewById(R.id.tv_warehouse_his_describe);
			TextView tv_warehouse_his_count = (TextView) holder2.findViewById(R.id.tv_warehouse_his_count);
			TextView tv_warehouse_his_status = (TextView) holder2.findViewById(R.id.tv_warehouse_his_status);
			TextView tv_warehouse_his_sale_area = (TextView) holder2.findViewById(R.id.tv_warehouse_his_sale_area);
		
		tv_warehouse_his_serial.setText(beans.size() - position + "");
		WarehouseBean b = beans.get(position);
		
		if (b.getOption_type() == out_warehouse) {
			tv_warehouse_his_sale_area.setVisibility(View.VISIBLE);
			if(cityBll == null) cityBll = new CityBll(context);
			City mCity = cityBll.queryCityByCode(b.getSale_area_code());
			
			String saleAreaStr = (mCity==null?"全国":SaleAddressUtil.getAddressStrByCity(mCity));
			
			tv_warehouse_his_sale_area.setText("销售区域："+saleAreaStr);
		} else {
			tv_warehouse_his_sale_area.setVisibility(View.GONE);
		}
		
		String describe = StringUtils.isEmpty(b.getDescribe()) ? "无" : b.getDescribe();
		tv_warehouse_his_describe.setText("描        述：" + describe);
		int count = labelBll.queryLabelCountById(b.getOption_type(), b.getId());// 该数据在数据库中去统计
		tv_warehouse_his_count.setText("数        量：" + count);
		if (b.getStatus() == 0) {
			lin_warehouse_adapter_item_root
					.setBackgroundColor(context.getResources().getColor(R.color.white));
			tv_warehouse_his_status.setText("状        态：未提交");
		} else {
			lin_warehouse_adapter_item_root
					.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
			tv_warehouse_his_status.setText("状        态：提交失败(错误码:" + b.getError_code() + ")\n原        因：");
		}
		
	}

}
