package com.convenient.android.common.utils.net

import android.annotation.SuppressLint
import android.content.Context
import android.net.wifi.WifiManager

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/8/1
 * description:
 */

object WifiApUtil {

    const val WIFI_AP_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED"

    /**
     * 获取WIFI热点的状态
     */
    fun getWifiApState(context: Context): Int {
        return try {
            @SuppressLint("WifiManagerLeak") val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
            val method = wifiManager.javaClass.getMethod("getWifiApState")
            method.invoke(wifiManager) as Int
        } catch (e: Exception) {
            HOTSPOT.WIFI_AP_STATE_FAILED.value
        }
    }

    /**
     * 判断Wifi热点是否可用
     */
    fun isApEnabled(context: Context): Boolean {
        return getWifiApState(context) == HOTSPOT.WIFI_AP_STATE_ENABLED.value
    }

}
enum class HOTSPOT(var value: Int) {
    // 正在关闭
    WIFI_AP_STATE_DISABLING(10),  // 已关闭
    WIFI_AP_STATE_DISABLED(11),  // 正在开启
    WIFI_AP_STATE_ENABLING(12),  // 已开启
    WIFI_AP_STATE_ENABLED(13),  //错误状态
    WIFI_AP_STATE_FAILED(14);

    companion object {
        fun getType(value: Int): HOTSPOT? {
            for (hotspot in values()) {
                if (hotspot.value == value) {
                    return hotspot
                }
            }
            return null
        }
    }
}