package com.yidian.shenghuoquan.newscontent.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;

public class HeightProvider extends PopupWindow implements OnGlobalLayoutListener {
    private final Activity mActivity;
    private final View rootView;
    private HeightListener listener;
    private static int heightMax; // 记录popup内容区的最大高度
    private static int keyboardHeight; // 记录keyboard高

    public HeightProvider(Activity activity) {
        super(activity);
        this.mActivity = activity;

        // 基础配置
        rootView = new View(activity);
        setContentView(rootView);

        // 监听全局Layout变化
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        setBackgroundDrawable(new ColorDrawable(0));

        // 设置宽度为0，高度为全屏
        setWidth(0);
        setHeight(LayoutParams.MATCH_PARENT);

        // 设置键盘弹出方式
        setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
    }

    public HeightProvider init() {
        if (!isShowing()) {
            final View view = mActivity.getWindow().getDecorView();
            // 延迟加载popupwindow，如果不加延迟就会报错
            view.post(() -> showAtLocation(view, Gravity.NO_GRAVITY, 0, 0));
        }
        return this;
    }

    public HeightProvider setHeightListener(HeightListener listener) {
        this.listener = listener;
        return this;
    }

    @Override
    public void onGlobalLayout() {
        Rect rect = new Rect();
        rootView.getWindowVisibleDisplayFrame(rect);
        if (rect.bottom > heightMax) {
            heightMax = rect.bottom;
        }

        // 两者的差值就是键盘的高度
        keyboardHeight = heightMax - rect.bottom;
        if (listener != null) {
            listener.onHeightChanged(heightMax, keyboardHeight);
        }
    }

    public interface HeightListener {
        void onHeightChanged(int heightMax, int height);
    }

    public static boolean isKeyboardShowing() {
        return keyboardHeight > heightMax / 3;
    }

    public static int getHeightMax() {
        return heightMax;
    }
}
