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.thread.ThreadReqTools;
import cn.net.caoyao.cpzshandset.service.tools.SaleAddressUtil;
import cn.net.caoyao.cpzshandset.ui.adapter.base.BaseArrayListAdapter;

/**
 * @author 作者 李扬:
 * @date 创建时间：2017-5-10 下午3:06:22
 * @version 1.0
 * @parameter
 * @since 中纺出库使用
 * @return
 */
public class HisListAdapter 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 HisListAdapter(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_hislist_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 hislist_item = holder2.findViewById(R.id.hislist_item);// 整个布局
		TextView hislist_item_tv_serial = (TextView) holder2
				.findViewById(R.id.hislist_item_tv_serial);// 标号
		TextView hislist_item_tv_describe = (TextView) holder2
				.findViewById(R.id.hislist_item_tv_describe);
		TextView hislist_item_tv_salearea = (TextView) holder2
				.findViewById(R.id.hislist_item_tv_salearea);
		TextView hislist_item_tv_opt_type = (TextView) holder2
				.findViewById(R.id.hislist_item_tv_opt_type);
		TextView hislist_item_tv_count = (TextView) holder2
				.findViewById(R.id.hislist_item_tv_count);
		TextView hislist_item_tv_status = (TextView) holder2
				.findViewById(R.id.hislist_item_tv_status);
		hislist_item_tv_serial.setText(beans.size() - position + "");
		WarehouseBean b = beans.get(position);

		if (b.getOption_type() == out_warehouse) {
			hislist_item_tv_salearea.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));

			hislist_item_tv_salearea.setText("销售区域：" + saleAreaStr);
		} else {
			hislist_item_tv_salearea.setVisibility(View.GONE);
		}

		String describe = StringUtils.isEmpty(b.getDescribe()) ? "无" : b
				.getDescribe();
		hislist_item_tv_describe.setText("描        述：" + describe);
		int count = labelBll.queryLabelCountById(b.getOption_type(), b.getId());// 该数据在数据库中去统计
		hislist_item_tv_count.setText("实际数量：" + count);
		if (b.getStatus() == 0) {
			hislist_item.setBackgroundColor(context.getResources().getColor(
					R.color.white));
			hislist_item_tv_status.setText("状        态：未提交");
		} else {
			hislist_item.setBackgroundColor(context.getResources().getColor(
					R.color.adapter_item_error_color));

			String str = "";
			switch (b.getError_code()) {
			case 1:
				str = "存在重复出库标签";
				break;
			case 2:
				str = "存在重复返库标签";
				break;

			default:
				str = ThreadReqTools.getErrorMsg(b.getError_code());
				break;
			}

			hislist_item_tv_status.setText("状        态：失败(错误码:"
					+ b.getError_code() + ")\n原        因：" + str);
		}

		if (b.getOption_type() == out_warehouse) {
			if (b.getOutWarehouseType() == 2) {
				hislist_item_tv_opt_type.setText("出库类型： 箱");
			} else {
				hislist_item_tv_opt_type.setText("出库类型： 托  盘");
			}
		} else {
			hislist_item_tv_opt_type.setVisibility(View.GONE);
		}
	}

}
