package com.xyxsbj.mylibrary.tool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import java.lang.reflect.Method;

/**
 * 创建人 mQ
 * 创建时间 2019/5/20 0020 10:35
 * 文件名称 ActivityTool
 * 说明: activity工具类
 **/
public class ActivityTool {

    public static void startActivity(Context context, Class<?> cls) {
        startActivity(context, cls, null);
    }

    public static void startActivity(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null)
            intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void startActivityForResult(Activity activity, Class<?> cls, int requestCode) {
        startActivityForResult(activity, cls, null, requestCode);
    }

    public static void startActivityForResult(Activity activity, Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(activity, cls);
        if (bundle != null)
            intent.putExtras(bundle);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 添加Fragment
     */
    public static void addFragment(FragmentManager manager, int layout, Fragment fragment) {
        manager.beginTransaction()
                .add(layout, fragment)
                .commit();
    }

    /**
     * 隐藏底部虚拟按键 并设置为亮色主题(状态栏字体深色)
     */
    public static void hideNavigationAndLight(Context context, Window window) {
        if (Build.VERSION.SDK_INT < 19 || !checkDeviceHasNavigationBar(context)) {
            //一定要判断是否存在按键，否则在没有按键的手机调用会影响别的功能。如之前没有考虑到，导致图传全屏变成小屏显示。
        } else {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide Navigation
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;

            // 设置属性
            window.getDecorView().setSystemUiVisibility(flag);
        }
    }

    /**
     * 隐藏底部虚拟按键 并设置为暗色主题(状态栏字体白色)
     */
    public static void hideNavigationAndDark(Context context, Window window) {
        if (Build.VERSION.SDK_INT < 19 || !checkDeviceHasNavigationBar(context)) {
            //一定要判断是否存在按键，否则在没有按键的手机调用会影响别的功能。如之前没有考虑到，导致图传全屏变成小屏显示。
        } else {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide Navigation
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

            // 设置属性
            window.getDecorView().setSystemUiVisibility(flag);
        }
    }


    /**
     * 隐藏顶部状态栏
     */
    public static void hideStatusBar(Context context, Window window) {
        if (Build.VERSION.SDK_INT < 19 || !checkDeviceHasNavigationBar(context)) {
            //一定要判断是否存在按键，否则在没有按键的手机调用会影响别的功能。如之前没有考虑到，导致图传全屏变成小屏显示。
        } else {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            // 设置属性
            window.getDecorView().setSystemUiVisibility(flag);
        }
    }

    /**
     * 隐藏底部虚拟按键和顶部状态栏
     */
    public static void hideNavigationAndStatusBar(Context context, Window window) {
        if (Build.VERSION.SDK_INT < 19 || !checkDeviceHasNavigationBar(context)) {
            //一定要判断是否存在按键，否则在没有按键的手机调用会影响别的功能。如之前没有考虑到，导致图传全屏变成小屏显示。
        } else {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide Navigation
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            // 设置属性
            window.getDecorView().setSystemUiVisibility(flag);
        }
    }

    /**
     * 判断是否存在虚拟按键
     */
    private static boolean checkDeviceHasNavigationBar(Context context) {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class<?> systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {

        }
        return hasNavigationBar;
    }
}
