package com.grkj.iscs.extentions

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import com.grkj.iscs.util.NetManager
import java.util.Locale

/**
 * 网络管理器
 */
fun Context.netManager() = NetManager.getInstance(applicationContext)

fun Context.addNetObserver(observer: Observer<Boolean>) {
    netManager().liveData.observeForever(observer)
}

fun Context.removeNetObserver(observer: Observer<Boolean>) {
    netManager().liveData.removeObserver(observer)
}

fun Context.serialNo() : String {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
        && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
        == PackageManager.PERMISSION_GRANTED) {
        return Build.getSerial().toUpperCase(Locale.ROOT)
    }
    return Build.SERIAL.toUpperCase(Locale.ROOT)
}

/**
 * 检查权限
 */
fun Context.checkPermissions(permissions: Array<String>): Boolean {
    for (permission in permissions) {
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
            return false
        }
    }
    return true
}