package com.cooleshow.base.utils;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.cooleshow.base.R;
import com.cooleshow.base.widgets.CommonPopupWindow;


/**
 * PopupWindow工具类
 */
public class PopupUtil {

    public interface ShowListener {
        void onShow(View view, PopupWindow popupWindow);
    }

    /*居中显示PopupWindow*/
    public static void showInCenter(Activity activity, int resourcesId, ShowListener showListener) {
        View popupView = LayoutInflater.from(Utils.getApp()).inflate(resourcesId, null);
        PopupWindow popupWindow = createPopupWindow(popupView, activity, true);
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        showListener.onShow(popupView, popupWindow);
    }

    /*居中显示PopupWindow带动画*/
    public static void showInCenter(Activity activity, int resourcesId, int animationStyle, ShowListener showListener) {
        View popupView = LayoutInflater.from(Utils.getApp()).inflate(resourcesId, null);
        PopupWindow popupWindow = createPopupWindow(popupView, activity, animationStyle, true);
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        showListener.onShow(popupView, popupWindow);
    }


    /*底部显示PopupWindow*/
    public static void showInBottom(Activity activity, int resourcesId, ShowListener showListener) {
        View popupView = LayoutInflater.from(Utils.getApp()).inflate(resourcesId, null);
        PopupWindow popupWindow = createPopupWindow(popupView, activity, true);
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        showListener.onShow(popupView, popupWindow);
    }

    /*顶部显示PopupWindow*/
    public static void showInTop(Activity activity, int resourcesId, ShowListener showListener) {
        View popupView = LayoutInflater.from(Utils.getApp()).inflate(resourcesId, null);
        PopupWindow popupWindow = createPopupWindow(popupView, activity, true);
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.TOP, 0, 0);
        showListener.onShow(popupView, popupWindow);
    }

    /*下方显示PopupWindow*/
    public static void showInDrop(Context activity, int resourcesId, View anchor, ShowListener showListener) {
        View popupView = LayoutInflater.from(activity).inflate(resourcesId, null);
        PopupWindow popupWindow = createNoBackPopupWindow(popupView, activity, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.showAsDropDown(anchor);
        showListener.onShow(popupView, popupWindow);
    } /*下方显示PopupWindow*/


    /*下方显示PopupWindow,有偏移值*/
    public static void showInDrop(Context activity, int resourcesId, View anchor, int x, int y, ShowListener showListener) {
        View popupView = LayoutInflater.from(activity).inflate(resourcesId, null);
        PopupWindow popupWindow = createNoBackPopupWindow(popupView, activity, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT, false);
        popupWindow.showAsDropDown(anchor, x, y);
        showListener.onShow(popupView, popupWindow);
    }

    /*下方显示PopupWindow,包裹内容有偏移值*/
    public static PopupWindow showInDropWrapNObg(Context activity, int resourcesId, View anchor, ShowListener showListener) {
        View popupView = LayoutInflater.from(activity).inflate(resourcesId, null);
        PopupWindow popupWindow = createNoBackPopupWindow(popupView, activity, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.showAsDropDown(anchor);
        showListener.onShow(popupView, popupWindow);
        return popupWindow;
    }
    /*下方显示PopupWindow,包裹内容有偏移值*/
    public static void showInDropWrapNObg(Activity activity, int resourcesId, View anchor, int x, int y, ShowListener showListener) {
        View popupView = LayoutInflater.from(activity).inflate(resourcesId, null);
        PopupWindow popupWindow = createNoBackPopupWindow(popupView, activity, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //        popupWindow.showAsDropDown(anchor, x, y);
        int[] location = new int[2];
        anchor.getLocationOnScreen(location);
        popupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, location[0] - anchor.getWidth() * 2 + x, location[1] + anchor.getHeight() - y);
        showListener.onShow(popupView, popupWindow);
    }

    /*下方显示PopupWindow,包裹内容有偏移值*/

    public static void showInDropWrap(Context activity, int resourcesId, View anchor, ShowListener showListener) {
        View popupView = LayoutInflater.from(activity).inflate(resourcesId, null);
        PopupWindow popupWindow = createPopupWindow(popupView, activity, true);
        popupWindow.showAsDropDown(anchor);
        showListener.onShow(popupView, popupWindow);
    }

    /*创建PopupWindow*/
    public static PopupWindow createPopupWindow(View popupView, Context activity, int animationStyle, boolean touchable) {
        CommonPopupWindow popupWindow = new CommonPopupWindow.Builder(activity)
                //设置PopupWindow布局
                .setView(popupView)
                //设置宽高
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)
                //设置动画
                .setOutsideTouchable(touchable)
                .setAnimationStyle(animationStyle)
                //设置背景颜色，取值范围0.0f-1.0f 值越小越暗 1.0f为透明
                .setBackGroundLevel(0.5f)
                //设置PopupWindow里的子View及点击事件
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {

                    }
                })
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();
        return popupWindow;

    } /*创建PopupWindow*/

    public static PopupWindow createPopupWindow(View popupView, Context activity, boolean touchable) {
        CommonPopupWindow popupWindow = new CommonPopupWindow.Builder(activity)
                //设置PopupWindow布局
                .setView(popupView)
                .setOutsideTouchable(touchable)
                //设置宽高
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)
                //设置动画
                //设置背景颜色，取值范围0.0f-1.0f 值越小越暗 1.0f为透明
                .setBackGroundLevel(0.5f)
                //设置PopupWindow里的子View及点击事件
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {

                    }
                })
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();
        return popupWindow;

    }

    public static PopupWindow createNoBackPopupWindow(View popupView, Context activity, int width, int height, boolean touchable) {
        CommonPopupWindow popupWindow = new CommonPopupWindow.Builder(activity)
                //设置PopupWindow布局
                .setView(popupView)
                .setOutsideTouchable(touchable)
                .setAnimationStyle(R.style.TopAnimation)
                //设置宽高
                .setWidthAndHeight(width, height)
                //设置动画
                //设置PopupWindow里的子View及点击事件
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {

                    }
                })
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(touchable)
                //开始构建
                .create();
        return popupWindow;

    }
}
