package com.sjkj.appthreefloor_tsgz.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.sjkj.appthreefloor_tsgz.R;

public class PopWindowUtil {

    private static PopWindowUtil instance;

    public static   PopupWindow mPopupWindow;

    // 私有化构造方法，变成单例模式
    private PopWindowUtil() {

    }

    // 对外提供一个该类的实例，考虑多线程问题，进行同步操作
    public static PopWindowUtil getInstance() {
        if (instance == null) {
            synchronized (PopWindowUtil.class) {
                if (instance == null) {
                    instance = new PopWindowUtil();
                }
            }
        }
        return instance;
    }

    /**
     * @param cx
     *            activity
     *            传入需要显示在什么控件下
     * @param view1
     *            传入内容的view
     * @return
     */
    public PopWindowUtil makePopupWindow(Context cx, View view1) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager manager = (WindowManager) cx.getSystemService(Context.WINDOW_SERVICE);

        mPopupWindow = new PopupWindow(cx);

        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
       /* view1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));*/
        // 设置PopupWindow的大小（宽度和高度）
        mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        int height = manager.getDefaultDisplay().getHeight();
        mPopupWindow.setHeight(height / 2);
        // 设置PopupWindow的内容view
        mPopupWindow.setContentView(view1);
        mPopupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
        mPopupWindow.setTouchable(true); // 设置PopupWindow可触摸
        mPopupWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸

        return instance;
    }

    /**
     *
     * @param cx 此处必须为Activity的实例
     * @return
     */
    public PopupWindow showLocationWithAnimation(final Context cx,View V) {
        // 弹出动画
        mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);

        // 弹出PopupWindow时让后面的界面变暗
        WindowManager.LayoutParams parms = ((Activity) cx).getWindow().getAttributes();
        parms.alpha =0.5f;
        ((Activity) cx).getWindow().setAttributes(parms);



        mPopupWindow.showAtLocation(V, Gravity.BOTTOM, 0, 0);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // PopupWindow消失后让后面的界面变亮
                WindowManager.LayoutParams parms = ((Activity) cx).getWindow().getAttributes();
                parms.alpha =1.0f;
                ((Activity) cx).getWindow().setAttributes(parms);

                if (mListener != null) {
                    mListener.dissmiss();
                }
            }
        });

        return mPopupWindow;
    }

    public interface OnDissmissListener{

        void dissmiss();

    }

    private OnDissmissListener mListener;

    public void setOnDissmissListener(OnDissmissListener listener) {
        mListener = listener;
    }


}

