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.City;
import cn.net.caoyao.cpzshandset.bean.DealerInfoBean;
import cn.net.caoyao.cpzshandset.bean.SaleBean;
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.DealerBll;
import cn.net.caoyao.cpzshandset.service.bll.SingleLabelBll;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;
import cn.net.caoyao.cpzshandset.service.tools.SaleAddressUtil;

public class SaleHisListAdapter extends BaseAdapter {

	private List<SaleBean> beans;
	private SingleLabelBll labelBll;
	private CityBll cityBll;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private int option_type = HandsetOperation.OPERATION_SALE_CONSTANT.getOption();
	private DealerBll dealerBll;
	private boolean showPurchaser = false;;

	public SaleHisListAdapter(List<SaleBean> beans,SingleLabelBll labelBll,CityBll cityBll) {
		this.beans = beans;
		this.labelBll = labelBll;
		this.cityBll = cityBll;
	}
	public SaleHisListAdapter(List<SaleBean> beans,SingleLabelBll labelBll,CityBll cityBll,boolean showPurchaser) {
		this.beans = beans;
		this.labelBll = labelBll;
		this.cityBll = cityBll;
		this.showPurchaser = showPurchaser;
	}

	public SaleHisListAdapter(List<SaleBean> beans,SingleLabelBll labelBll,DealerBll dealerBll) {
		this.beans = beans;
		this.labelBll = labelBll;
		this.dealerBll = dealerBll;
	}
	public void refreshData(List<SaleBean> 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 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);
			String tipsStr = "还没有" + HandsetOperation.OPERATION_SALE_CONSTANT.getOptionName()+"数据，快去创建吧^_^";
			tv_no_data_tips.setText(tipsStr);
		} else {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.adapter_sale_his_list_item, parent, false);
				holder = new ViewHolder();
				holder.lin_sale_adapter_item_root = convertView.findViewById(R.id.lin_sale_adapter_item_root);
				holder.tv_sale_his_serial = (TextView) convertView.findViewById(R.id.tv_sale_his_serial);
				holder.tv_sale_his_describe = (TextView) convertView.findViewById(R.id.tv_sale_his_describe);
				holder.tv_sale_his_count = (TextView) convertView.findViewById(R.id.tv_sale_his_count);
				holder.tv_sale_his_status = (TextView) convertView.findViewById(R.id.tv_sale_his_status);
				holder.tv_sale_area = (TextView) convertView.findViewById(R.id.tv_sale_area);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv_sale_his_serial.setText(beans.size() - position + "");
			SaleBean b = beans.get(position);

			if (cityBll != null) {
				City mCity = cityBll.queryCityByCode(b.getSales_area());
				String salesArea = (mCity == null ? "全国": SaleAddressUtil.getAddressStrByCity(mCity));
				holder.tv_sale_area.setText("销售区域：" + salesArea);
			}
			
			if (dealerBll != null) {
				DealerInfoBean dealerBean = dealerBll.queryDealerById(b.getDealer_id());
				String dealerName = (dealerBean == null ? "无" :dealerBean.getName());
				holder.tv_sale_area.setText("经  销  商：" + dealerName);
			}
			
			
			if (showPurchaser) {
				String purchaser = StringUtils.isEmpty(b.getPurchaser()) ? "无" : b.getPurchaser();
				holder.tv_sale_his_describe.setText("采  购  方：" + purchaser);
			} else {
				String describe = StringUtils.isEmpty(b.getDescribe()) ? "无" : b.getDescribe();
				holder.tv_sale_his_describe.setText("销售描述：" + describe);
			}
			int count = labelBll.queryLabelCountById(option_type, b.getId());
			if (b.getPlan_num() > 0) {
				holder.tv_sale_his_count.setText("标签数量：" + count+" / " + b.getPlan_num());
			} else {
				holder.tv_sale_his_count.setText("标签数量：" + count);
			}
			
			if (b.getStatus() == 0) {
				holder.lin_sale_adapter_item_root.setBackgroundColor(context.getResources().getColor(R.color.white));
				holder.tv_sale_his_status.setText("提交状态：未提交");
			} else {
				holder.lin_sale_adapter_item_root
						.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
				holder.tv_sale_his_status.setText("提交状态：提交失败(错误码：" + b.getError_code() + ")\n原        因：" + ThreadReqTools.getErrorMsg(b.getError_code()));
			}
		}
		return convertView;
	}

	@Override
	public int getItemViewType(int position) {
		return beans.size() == 0?NO_DATE_VIEW : NORMAL_VIEW;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	private class ViewHolder {
		private View lin_sale_adapter_item_root;
		private TextView tv_sale_his_serial, tv_sale_his_describe, tv_sale_his_count, tv_sale_his_status, tv_sale_area;

	}

}
