package core.util.net

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.NetworkInfo
import java.lang.Exception

/**
 * Function :
 * Date:  2020-10-15 10:02
 * Author: ReiChin_
 * Version:
 */
class NetWatchdog(context: Context) {

    private val mAppContext = context.applicationContext

    private var isReconnect = false

    // 广播过滤器，监听网络变化
    private val mNetIntentFilter = IntentFilter()

    init {
        mNetIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
    }

    private val mReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            // 获取手机的连接服务管理器，这里是连接管理器类
            val cm = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

            val wifiNetworkInfo: NetworkInfo? = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            val mobileNetworkInfo: NetworkInfo? = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            val activeNetworkInfo: NetworkInfo? = cm.getActiveNetworkInfo()

            val wifiState = wifiNetworkInfo?.state ?: NetworkInfo.State.UNKNOWN
            val mobileState = mobileNetworkInfo?.state ?: NetworkInfo.State.UNKNOWN

            if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting) {
                mNetConnectedListener?.run {
                    isReconnect = false
                    onReNetConnected(isReconnect)
                }
            } else if (activeNetworkInfo == null){
                mNetConnectedListener?.run {
                    isReconnect = true
                    onNetUnConnected()
                }
            }

            if (NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED == mobileState) {
                mNetChangeListener?.onWifiTo4G()
            } else if (NetworkInfo.State.CONNECTED == wifiState && NetworkInfo.State.CONNECTED != mobileState) {
                mNetChangeListener?.on4GToWifi()
            } else if (NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED != mobileState) {
                mNetChangeListener?.onNetDisconnected()
            }
        }
    }

    private var mNetChangeListener: NetChangeListener? = null

    fun setNetChangeListener(listener: NetChangeListener) {
        mNetChangeListener = listener
    }

    private var mNetConnectedListener: NetConnectedListener? = null

    fun setNetConnectedListener(listener: NetConnectedListener) {
        mNetConnectedListener = listener
    }

    /**
     * 开始监听
     */
    fun startWatch() {
        try {
            mAppContext.registerReceiver(mReceiver, mNetIntentFilter)
        } catch (e: Exception) {
        }
    }

    /**
     * 结束监听
     */
    fun stopWatch() {
        try {
            mAppContext.unregisterReceiver(mReceiver)
        } catch (e: Exception) {
        }
    }

    companion object {
        /**
         * 静态方法获取是否有网络连接
         *
         * @param context 上下文
         * @return 是否连接
         */
        fun hasNet(context: Context): Boolean {
            // 获取手机的连接服务管理器，这里是连接管理器类
            val cm = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

            val wifiNetworkInfo: NetworkInfo? = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            val mobileNetworkInfo: NetworkInfo? = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            val activeNetworkInfo: NetworkInfo? = cm.activeNetworkInfo

            val wifiState = wifiNetworkInfo?.state ?: NetworkInfo.State.UNKNOWN
            val mobileState = mobileNetworkInfo?.state ?: NetworkInfo.State.UNKNOWN

            if (NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED != mobileState) {
                return false
            }
            if (activeNetworkInfo == null || !activeNetworkInfo.isConnectedOrConnecting) {
                return false
            }

            return true
        }

        /**
         * 静态判断是不是4G网络
         *
         * @param context 上下文
         * @return 是否是4G
         */
        fun is4GConnected(context: Context): Boolean {
            // 获取手机的连接服务管理器，这里是连接管理器类
            val cm = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

            val mobileNetworkInfo: NetworkInfo ? = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)

            val mobileState = mobileNetworkInfo?.state ?: NetworkInfo.State.UNKNOWN

            return NetworkInfo.State.CONNECTED == mobileState
        }
    }


}