package com.secspace.lib.common.function

import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import com.secspace.lib.common.env.Packages
import com.secspace.lib.common.env.SEnvironment
import com.secspace.lib.common.function.SwitchInMdm.init
import com.secspace.lib.common.utils.*
import com.secspace.log.Log
import com.secspace.mdmengine.api.manager.MdmFactoryManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File

object WorkManager{
    private val TAG = "SecSpaceWorkManager"
    private lateinit var mContext: Context
    private val mHandler: Handler = Handler(Looper.getMainLooper(), Handler.Callback { true })
    private var mEnterTime: Long = 0
    private var isFrstCheckDeviceOwner = true
    var isChecksCompleted = false

    fun enter(launcher: Activity?) {
        mContext = launcher!!.applicationContext
        val isMdmInitialized = Prefs.isMdmInitializedWhenEnter(mContext)
        if (isChecksCompleted && isMdmInitialized) {
            Log.i(TAG, "isMdmInitialized: $isMdmInitialized, isChecksCompleted:$isChecksCompleted")
            return
        }
        mEnterTime = SystemClock.uptimeMillis()
        //检查中间件是否是最新的
        if (!DpmHelper.getInstance(mContext).isMiddlewareLatest) {
            DialogManager.getInstance().showInstallMiddleWare(launcher)
            return
        }

        DpmHelper.getInstance(mContext).initMiddleWare()

        //检查中间件是否激活
        if (!DpmHelper.getInstance(mContext).isMiddlewareDPMActived) {
            MdmFactoryManager.getInstance().deviceSettingsManager.setSilentActiveAdmin(DpmHelper.getInstance(mContext).middleWareAdmin)
            Log.i(TAG, "silent active middleware")
        }
        //安装耗时，检查是否都是最新的
        if (!DpmHelper.getInstance(mContext).isMiddlewareLatest) {
            Log.i(TAG, "will install delay")
            mHandler.postDelayed( { enter(launcher) }, 2000)
            return
        }
        GlobalScope.launch(Dispatchers.IO) {
            init(launcher)
            checkLocalUpdateFile() //检查本地更新文件
        }
        isChecksCompleted = true
        Prefs.setMdmInitializedWhenEnter(mContext, true)
    }

    fun exit(context: Context?){
        GlobalScope.launch(Dispatchers.IO) {
            context?.let { SwitchOutMdm.exit(it) }
        }
    }

    private fun hasPermissions(permissions: Array<String>, packageName: String): Boolean {
        if (Build.VERSION.SDK_INT < 23) {
            return true
        } else {
            val length = permissions.size
            for (i in 0 until length) {
                val permission = permissions[i]
                if (mContext.packageManager.checkPermission(permission, packageName) != PackageManager.PERMISSION_GRANTED) {
                    return false
                }
            }
            return true
        }
    }

    /**
     * 检查本地是否有更新文件，有且版本号高则更新
     */
    private fun checkLocalUpdateFile() {
        Log.d(TAG, "check local update file")
        val dir = File(mContext.getExternalFilesDir(""), "Download")
        if (!dir.exists()) {
            Log.i(TAG, "Download dir is not exist.")
            return
        }
        val files = dir.listFiles()
        if (files == null || files.size == 0) {
            Log.i(TAG, "No file")
            return
        }
        var updateFile: File? = null
        val currentVCode = PkgManager.getInstance(mContext).getVersionCodeFromPackage(mContext.packageName)
        var newVCode = -1
        for (file: File in files) {
            val fileCode = PkgManager.getInstance(mContext).getStorageApkVersionCode(file.absolutePath)
            if (newVCode < fileCode) {
                newVCode = fileCode
                updateFile = file
            }
        }
        if (updateFile == null) {
            Log.i(TAG, "No update file")
            return
        }
        Log.i(TAG, String.format("new version(%d),old version(%d)", newVCode, currentVCode))
        if (newVCode <= currentVCode) {
            Log.i(TAG, "No Update.")
            return
        }
        Log.i(TAG, "Will install update File: " + updateFile.absolutePath)
        ToastUtil.showShort(mContext, "更新中...")
        install(updateFile.absolutePath)
    }

    private fun pathFromAssets(name: String): String {
        return mContext.externalCacheDir.absolutePath + File.separator + name
    }

    private fun install(path: String) {
        PkgManager.getInstance(mContext).installByInstaller(path)
    }

    /**
     * 安装中间件
     */
    fun installMiddleWare(context: Context) {
        val path = pathFromAssets(SEnvironment.getMiddleWareNameForAssets())
        FileUtil.copyFileFromAssets(context, SEnvironment.getMiddleWareNameForAssets(), path)
        install(path)
    }

    /**
     * 安装增强件
     */
    fun installMdmEnhance(silent: Boolean) {
        val path = pathFromAssets(SEnvironment.getMdmEnhanceNameForAssets())
        FileUtil.copyFileFromAssets(mContext, (SEnvironment.getOuterDirForAssets()
                + File.separator + SEnvironment.getMdmEnhanceNameForAssets()), path)
        install(path)
    }
}