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

import java.util.List;

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;

public class ChoiceSaleAddressAdapter extends BaseAdapter {
	public static interface IOnItemSelectListener {
		public void onItemClick(int pos);
	};

	private List<City> citys;
	private int currentLevel = 1; // 1:省 2：市 3：县
	private boolean hasCountry = true;

	public ChoiceSaleAddressAdapter(List<City> citys, int currentLevel,boolean hasCountry) {
		this.citys = citys;
		this.currentLevel = currentLevel;
		this.hasCountry = hasCountry;
	}

	public void setCountryArea(boolean hasCountry) {
		this.hasCountry = hasCountry;
	}

	public void refreshData(List<City> citys) {
		this.citys = citys;
		notifyDataSetChanged();
		/*
		 * if (selIndex < 0) { selIndex = 0; } if (selIndex >= citys.size()) {
		 * selIndex = citys.size() - 1; }
		 */
	}

	@Override
	public int getCount() {
		if (!hasCountry) {
			return citys.size();
		}
		return citys.size() + 1;
	}

	@Override
	public Object getItem(int pos) {
		if (!hasCountry) {
			return pos;
		}
		return pos == 0 ? pos : citys.get(pos - 1);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.spiner_item_layout, parent, false);
			holder = new ViewHolder();
			holder.tv_spinner_item = (TextView) convertView.findViewById(R.id.tv_spinner_item);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (!hasCountry) {
			showNoCountryView(holder.tv_spinner_item, position);
		} else {
			showHasCountryView(holder.tv_spinner_item, position);
		}
		return convertView;
	}

	private void showHasCountryView(TextView tv_spinner_item, int position) {
		if (currentLevel == 1) {
			if (position == 0) {
				tv_spinner_item.setText("全国");
			} else {

				String s = citys.get(position - 1).getProvinceName() == null ? ""
						: citys.get(position - 1).getProvinceName();
				tv_spinner_item.setText(s);
			}
		} else if (currentLevel == 2) {
			if (position == 0) {
				tv_spinner_item.setText("未选择");
			} else {
				String s = citys.get(position - 1).getCityName() == null ? "" : citys.get(position - 1).getCityName();
				tv_spinner_item.setText(s);
			}

		} else {
			if (position == 0) {
				tv_spinner_item.setText("未选择");
			} else {
				String s = citys.get(position - 1).getAreasName() == null ? "" : citys.get(position - 1).getAreasName();
				tv_spinner_item.setText(s);
			}
		}
	}

	private void showNoCountryView(TextView tv_spinner_item, int position) {
		if (currentLevel == 1) {

			String s = citys.get(position).getProvinceName() == null ? ""
					: citys.get(position).getProvinceName();
			tv_spinner_item.setText(s);
		} else if (currentLevel == 2) {
			String s = citys.get(position).getCityName() == null ? "" : citys.get(position).getCityName();
			tv_spinner_item.setText(s);

		} else {
			String s = citys.get(position).getAreasName() == null ? "" : citys.get(position).getAreasName();
			tv_spinner_item.setText(s);
		}

	}

	public static class ViewHolder {
		public TextView tv_spinner_item;
	}
}
