package com.sipai.ui.bizPopWindow;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.sipai.jspatrol.Bean.Biz;
import com.sipai.jspatrol.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义popuwindow
 * @author Alan <br/>
 */
public class CustomListPopupWindow extends PopupWindow implements OnDismissListener{

	private ListView mListView;
    TextView background;
	private List<Biz> mUserModels=new ArrayList<Biz>();
	private ItemClickCallBack mItemCallBack;
	private Context mContext;
	private String checkedUId = "";
	private View rootView;
	private DataAdapter mAdapter=null;

	/**
	 *
	 * @param context
	 * @param mUserModels 数据集
	 * @param checkedUId 被选中的item
	 * @param mItemCallBack item的点击事件
	 */
	public CustomListPopupWindow(Context context, List<Biz> mUserModels, String checkedUId, ItemClickCallBack mItemCallBack){
		super();
		this.mContext=context;
		this.mUserModels=mUserModels;
		this.checkedUId=checkedUId;
		this.mItemCallBack=mItemCallBack;
		initPop();
	}



	private void initPop() {
		// TODO Auto-generated method stub
		rootView = LayoutInflater.from(mContext).inflate(R.layout.bizlist_popupwindow, null);
		mListView= rootView.findViewById(R.id.lv_pop_datas);
        this.setContentView(rootView);
        this.setWidth(LayoutParams.WRAP_CONTENT);
        this.setHeight(LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOnDismissListener(this);
//        this.setBackgroundDrawable(new BitmapDrawable());
        //this.setAnimationStyle(animStyle);
        this.setContentView(rootView);
        this.mAdapter=new DataAdapter();
        this.mListView.setAdapter(mAdapter);
        this.mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {
                if (mItemCallBack != null) {
                	Biz biz=mUserModels.get(position);
                	mItemCallBack.callBack(biz);
                }
            }
        });
//        background.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dismiss();
//            }
//        });
	}

	private class DataAdapter extends BaseAdapter {

        class ViewHolder {
            TextView tv_name;
            ImageView iv_head;
        }



        @Override
        public int getCount() {
            if (mUserModels != null) {
                return mUserModels.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (mUserModels != null) {
                return mUserModels.get(position);
            }
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            Biz uModel=mUserModels.get(position);
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.biz_poplistview_item, null);
                viewHolder.tv_name= convertView.findViewById(R.id.moudle_bizpop_tv_name);
                viewHolder.iv_head= convertView.findViewById(R.id.moudle_bizpop_iv_head);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //绑定数据
            viewHolder.tv_name.setText(uModel.getName());
            return convertView;
        }
    }

    public interface ItemClickCallBack {
        void callBack(Biz biz);
    }





    private int dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

	@Override
	public void onDismiss() {
		// TODO Auto-generated method stub

	}

}
