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

import java.util.List;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.StationManagerBean;
import cn.net.caoyao.cpzshandset.ui.adapter.SpinerStationAdapter.IOnItemSelectListener;

public class SpinerStationPopWindow extends PopupWindow implements OnItemClickListener {
	private Context mContext;
	private ListView mListView;
	private SpinerStationAdapter mAdapter;
	private IOnItemSelectListener mItemSelectListener;

	public SpinerStationPopWindow(Context context) {
		super(context);

		mContext = context;
		init();
	}
	

	public void setItemListener(IOnItemSelectListener listener) {
		mItemSelectListener = listener;
	}

	public void setAdatper(SpinerStationAdapter adapter) {
		mAdapter = adapter;
		mListView.setAdapter(mAdapter);
	}
	
	@Override
	public void dismiss() {
		super.dismiss();
	}

	private void init() {
		View view = LayoutInflater.from(mContext).inflate(R.layout.spinner_window_layout, null);
		setContentView(view);
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.WRAP_CONTENT);

		setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x00);
		setBackgroundDrawable(dw);

		mListView = (ListView) view.findViewById(R.id.lv_spinner);
		mListView.setOnItemClickListener(this);
	}

	public void refreshData(List<StationManagerBean> list, int selIndex) {
		if (list != null && selIndex != -1) {
			if (mAdapter != null) {
				mAdapter.refreshData(list, selIndex);
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
		dismiss();
		if (mItemSelectListener != null) {
			mItemSelectListener.onItemClick(pos);
		}
	}
	
}
