package com.bonait.bnframework.common.helper;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowInsets;
import android.view.WindowManager;

import com.qmuiteam.qmui.util.QMUINotchHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

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

public class ActiveMax {
    /**
     * 最大化窗体
     * View.SYSTEM_UI_FLAG_LAYOUT_STABLE：全屏显示时保证尺寸不变。
     * View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN：Activity全屏显示，状态栏显示在Activity页面上面。
     * View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION：效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
     * View.SYSTEM_UI_FLAG_HIDE_NAVIGATION：隐藏导航栏
     * View.SYSTEM_UI_FLAG_FULLSCREEN：Activity全屏显示，且状态栏被隐藏覆盖掉。
     * View.SYSTEM_UI_FLAG_VISIBLE：Activity非全屏显示，显示状态栏和导航栏。
     * View.INVISIBLE：Activity伸展全屏显示，隐藏状态栏。
     * View.SYSTEM_UI_LAYOUT_FLAGS：效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
     * View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY：必须配合View.SYSTEM_UI_FLAG_FULLSCREEN和View.SYSTEM_UI_FLAG_HIDE_NAVIGATION组合使用，达到的效果是拉出状态栏和导航栏后显示一会儿消失。
     *
     * @param activity
     */
    public static List<Activity> activities=new ArrayList<>();
    public static void MaxFrom(Activity activity) {
        // 全屏展示
        if(!activities.contains(activity))
        {
            activities.add(activity);
        }

        //显示全屏
        ShowMaxFrom(activity);

        // 注册监听器
        activity.getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(keyboardVisibilityListener);
        // 禁用触摸事件触发导航栏显示
        // activity.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(setOnSystemUiVisibilityChangeListener);
    }

    public static void destroy(Activity activity){
        activities.remove(activity);
        activity.getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(keyboardVisibilityListener);
    }

    /**
     * 状态栏变化后事件
     */
    private static View.OnSystemUiVisibilityChangeListener setOnSystemUiVisibilityChangeListener=new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                //如果非全屏

            }
        }
    };
    /**
     *  键盘打开后改变状态栏显示方式->这里启动监听并且设置全屏
     */
    private static ViewTreeObserver.OnGlobalLayoutListener keyboardVisibilityListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // 在此处处理键盘关闭事件
            // 检查键盘的可见性和高度等信息来判断键盘是否关闭
            for (Activity activity:activities)
            {
                ShowMaxFrom(activity);
            }
        }
    };

    /**
     * 显示全屏
     * @param activity
     */
    public static void ShowMaxFrom(Activity activity)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // 全屏显示，隐藏状态栏和导航栏，拉出状态栏和导航栏显示一会儿后消失。
                // 启动游戏模式，设置状态栏和导航栏中的图标变小，变模糊或者弱化其效果
                activity.getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LOW_PROFILE
                                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
                //activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_FULLSCREEN);
                //activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
            } else {
                // 全屏显示，隐藏状态栏
                activity.getWindow().getDecorView().setSystemUiVisibility(View.INVISIBLE);
            }
        }
    }

    /**
     * 关闭全屏显示
     * @param activity
     */
    public static void CloseMaxFrom(Activity activity) {
        // 非全屏显示，显示状态栏和导航栏
        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
    }
}
