package com.xhly.manageapp.ui

import BaseActivity
import android.app.csdk.CSDKManager
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED
import android.content.pm.PackageManager.DONT_KILL_APP
import android.os.Environment
import android.os.Environment.DIRECTORY_DOWNLOADS
import android.provider.Settings
import com.xhly.corelib.base.viewmodel.CommonBaseViewModel
import com.xhly.corelib.eventbus.UIEvent
import com.xhly.corelib.utils.LogShow
import com.xhly.manageapp.ManageApplication
import com.xhly.manageapp.databinding.ActivityManageBinding
import com.xhly.manageapp.utils.ZJAPPUtils
import java.io.File


//安装白名单为添加机制，不在白名单时需要去移除
class ManageActivity : BaseActivity<CommonBaseViewModel, ActivityManageBinding>() {
    private lateinit var csdkManager: CSDKManager
    private var type = 1
    private var type1 = 0
    private var type2 = 0
    private var type3 = 0
    private var type4 = 0
    private var type5 = 0
    private var type6 = 0
    private var appList =
        arrayListOf("com.android.calendar", "com.android.soundrecorder", "com.android.deskclock")

    override fun getBinding() = ActivityManageBinding.inflate(layoutInflater)

    override fun initData() {
        csdkManager = CSDKManager(this)
        //同意运行时权限
        csdkManager.setRuntimePermissions(true)
        //未知来源权限
      /*  csdkManager.enableAllUnkownsourcesV3(true)*/
    }


    override fun initView() {
        mBinding.adbopenBtn.setOnClickListener {
            Toast("设置" + csdkManager.enableUsbDebugging(true))
        }
        mBinding.adbcloseBtn.setOnClickListener {
            Toast("设置" + csdkManager.enableUsbDebugging(false))
        }
        mBinding.cameraBtn.setOnClickListener {
            if (type == 0) {
                type = 1
            } else {
                type = 0
            }
            Toast("设置" + csdkManager.disableCamera((type == 0)))
        }
        mBinding.bluBtn.setOnClickListener {
            type1 = if (type1 == 0) {
                1
            } else {
                0
            }
            Toast("设置" + csdkManager.enableBluetoothV3(type1 == 0))
            startActivity(Intent(Settings.ACTION_BLUETOOTH_SETTINGS))
        }
        mBinding.netBtn.setOnClickListener {
            type5 = if (type5 == 0) {
                1
            } else {
                0
            }
            csdkManager.enableDataV3(type5 == 0)
            startActivity(Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS))
        }

        mBinding.locationBtn.setOnClickListener {
            type3 = if (type3 == 0) {
                1
            } else {
                0
            }
            Toast("设置" + csdkManager.enableLocationV3(type3 == 0))
            startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
        }
        mBinding.wifiBtn.setOnClickListener {
            type2 = if (type2 == 0) {
                1
            } else {
                0
            }
            Toast("设置" + csdkManager.enableWifiV3(type2 == 0))
            startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
        }

        mBinding.wifiHighBtn.setOnClickListener {
            type6 = getType(type6)
            //csdkManager.disallowWifiV3(false)
            csdkManager.disallowWifiAdvanceSettingsV3(type6==0)
            startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
        }

        mBinding.wifiHighBtn2.setOnClickListener {
            type6 = getType(type6)
            //csdkManager.disallowWifiV3(false)
            csdkManager.disallowWifiAdvanceSettingsV3(type6==0)
             startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
        }

        mBinding.usbBtn.setOnClickListener {
            type4 = if (type4 == 0) {
                1
            } else {
                0
            }
            var s = if (type4 == 0) {
                "禁用"
            } else {
                "启用"
            }
            Toast("$s,设置" + csdkManager.requestKeyControl_V3(26, type4 == 0))
        }
        var type7 = 0
        mBinding.sdBtn.setOnClickListener {
            type7 = if (type7 == 0) {
                1
            } else {
                0
            }
            Toast("设置" + csdkManager.enableMassStorage(type7 == 0))
        }
        var type8 = 0
        mBinding.otgBtn.setOnClickListener {
            type8 = if (type8 == 0) {
                1
            } else {
                0
            }
            csdkManager.isCustomOtg=(type8 == 0)
            Toast("设置")
        }
        var type9 = 0
        mBinding.banBtn.setOnClickListener {
            type9 = getType(type9)
            /*appList.forEach {
                csdkManager.setPackageEnabled(it, type9 == 0)
            }*/
            csdkManager.setPackageEnabled("com.coolapk.market", type9 == 0)
        }
        mBinding.uninstallBtn.setOnClickListener {
            //csdkManager.removeInstallPackageWhiteList(arrayListOf("com.baidu.searchbox"))
            csdkManager.uninstallPackage("com.coolapk.market", true)
        }
        mBinding.installBtn.setOnClickListener {
            var file = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)
            var name = file.toString()
            var sb = StringBuffer()
            csdkManager.installPackageWhiteList?.forEach {
                sb.append("$it,")
            }
            Toast("路径$name")
            LogShow("白名单" + sb.toString())
            /*   csdkManager.addInstallPackageWhiteList(
                   arrayListOf(
                       "com.xhly.manageapp", "com.coolapk.market"
                   )
               )*/

            /* csdkManager.disableInstallation(false)*/
          /*  ManageApplication.addInstallPackageWhiteList(arrayListOf( "com.coolapk.market"))*/
            ManageApplication.removeInstallPackageWhiteList(arrayListOf( "com.coolapk.market"))
            csdkManager.installPackage(File(name,"test.apk").toString())
            //ZJAPPUtils.installApk(this, File(name, "test.apk"))
        }
        var type10 = 0
        mBinding.banmodleBtn.setOnClickListener {
            var componentName = ComponentName(
                "com.coolapk.market",
                "com.coolapk.market.view.appmanager.AppManagerActivity"
            )
            type10 = getType(type10)
            var state = if (type10 == 0) {
                Toast("启用功能")
                COMPONENT_ENABLED_STATE_ENABLED
            } else {
                Toast("关闭功能")
                COMPONENT_ENABLED_STATE_DISABLED
            }
            csdkManager.setComponentEnabled(
                componentName,
                state,
                DONT_KILL_APP
            )
        }
        var type11 = 0
        mBinding.getsnBtn.setOnClickListener {
            var array =
                arrayListOf("设备Mac地址", "设备SN号", "获取设备型号信息", "获取设备IMEI信息")
            array.forEach {
                var indexOf = array.indexOf(it)
                var deviceInfo = csdkManager.getDeviceInfo(indexOf + 1)
                LogShow("$it==$deviceInfo")
            }
        }
        var num = 0
        mBinding.resetBtn.setOnClickListener {
            num++
            Toast("还有${5 - num}次，恢复出厂")
            if (num == 5) {
                var launchFactoryReset = csdkManager.launchFactoryReset()
                Toast("恢复出厂成功=" + launchFactoryReset)
            }
        }
        mBinding.launcherBtn.setOnClickListener {
            //Toast("设置成功"+csdkManager.setLauncher("com.xhly.manageapp"))
            csdkManager.setCustomLauncher(
                "com.xhly.manageapp",
                "com.xhly.manageapp.ui.ManageActivity"
            )
        }
        var type12 = 0
        mBinding.airbtnBtn.setOnClickListener {
            type12 = getType(type12)
            Toast("禁止操作" + csdkManager.disallowAirplaneModeV3(type12==0 ))
        }
        var type13 = 0
        mBinding.statusBtn.setOnClickListener {
            type13 = getType(type13)
            Toast("禁止操作" + csdkManager.hideStatusBar(type13 == 0))
        }
        var type14 = 0
        mBinding.statuspanelBtn.setOnClickListener {
            type14 = getType(type14)
            Toast("禁止操作" + csdkManager.disableStatusBarPanel(type14 == 0))
        }
        var type15 = 0
        mBinding.hidekeyBtn.setOnClickListener {
            type15 = getType(type15)
            csdkManager.hideBackSoftKey(type15 == 0)
            csdkManager.hideMenuSoftKey(type15 == 0)
            csdkManager.hideHomeSoftKey(type15 == 0)
        }
    }

    override fun finish() {
        super.finish()
        UIEvent("update").post()
    }

    private fun getType(num: Int): Int {
        var type = num
        type = if (num == 0) {
            Toast("关闭功能false")
            1
        } else {
            Toast("启用功能true")
            0
        }
        return type
    }
}