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.graphics.Color
import android.os.Environment
import android.os.Environment.DIRECTORY_DOWNLOADS
import android.os.storage.StorageManager
import android.provider.Settings
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.kongzue.dialogx.dialogs.CustomDialog
import com.kongzue.dialogx.interfaces.OnBindView
import com.xhkjedu.manageapp.R
import com.xhkjedu.manageapp.databinding.ActivityManageBinding
import com.xhly.corelib.Const
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.bean.user.UserBean
import com.xhly.manageapp.utils.CurrentAppSharedPreferencesUtils
import com.xhly.manageapp.utils.StrategyUtils
import com.xhly.manageapp.viewmodel.BaseViewModel
import java.io.Console
import java.io.File


//安装白名单为添加机制，不在白名单时需要去移除
class ManageActivity : BaseActivity<BaseViewModel, ActivityManageBinding>() {
    //存储退出管控
    private val currentSpUtils by lazy { CurrentAppSharedPreferencesUtils.getInstance(this) }
    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() {
        //同意运行时权限
        ManageApplication.setRuntimePermissions(true)
        ManageApplication.enableAllUnkownsourcesV3(true)
    }


    override fun initView() {
        mBinding.adbopenBtn.setOnClickListener {
            Toast("设置" + ManageApplication.enableUsbDebugging(true))
        }
        mBinding.adbcloseBtn.setOnClickListener {
            Toast("设置" + ManageApplication.enableUsbDebugging(false))
        }
        mBinding.cameraBtn.setOnClickListener {
            if (type == 0) {
                type = 1
            } else {
                type = 0
            }
            val b = type == 0
            Toast("设置 $b")
            ManageApplication.disableCamera(b)
        }
        mBinding.bluBtn.setOnClickListener {
            type1 = if (type1 == 0) {
                1
            } else {
                0
            }
            var b = type1 == 0
            Toast("设置" + b)
            ManageApplication.enableBluetooth(b)
        }
        mBinding.netBtn.setOnClickListener {
            type5 = if (type5 == 0) {
                1
            } else {
                0
            }
            var b = type5 == 0
            Toast("设置" + b)
            ManageApplication.enableData(b)
            startActivity(Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS))
        }

        mBinding.locationBtn.setOnClickListener {
            type3 = if (type3 == 0) {
                1
            } else {
                0
            }
            Toast("设置" + (type3 == 0))
            ManageApplication.enableLocation((type3 == 0))
        }
        mBinding.wifiBtn.setOnClickListener {
            type2 = if (type2 == 0) {
                1
            } else {
                0
            }
            Toast("设置" +(type2 == 0))
            ManageApplication.enableWifi(type2==0)
        }

        mBinding.wifiHighBtn.setOnClickListener {
            type6 = getType(type6)
            //csdkManager.disallowWifi_v3(false)
            ManageApplication.disallowWifiAdvanceSettings(type6==0)
            Toast("设置" +(type6 == 0))
            startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
        }

        mBinding.wifiHighBtn2.setOnClickListener {
            type6 = getType(type6)
            //csdkManager.disallowWifi_v3(false)
            ManageApplication.disallowWifiAdvanceSettings(type6==0)
            Toast("设置" +(type6 == 0))
            startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
        }

        mBinding.usbBtn.setOnClickListener {
            type4 = getType(type4 )
            Toast("设置" + (type4 == 0))
            ManageApplication.requestKeyControl_V3(26,type4 == 0)
        }
        var type7 = 0
        mBinding.sdBtn.setOnClickListener {
            type7 = getType(type7 )
            Toast("设置" +(type7 == 0))
            ManageApplication.enableMassStorage(type7 == 0)
        }
        var type8 = 0
        mBinding.otgBtn.setOnClickListener {
            type8 = getType(type8)
            ManageApplication.setCustomOTG((type8 == 0))
            Toast("设置"+(type8 == 0))
        }
        var type9 = 0
        mBinding.banBtn.setOnClickListener {
            type9 = getType(type9)
            ManageApplication.setPackageEnabled("com.coolapk.market", type9 == 0)
        }
        mBinding.settimeBtn.setOnClickListener {
           // ManageApplication.setSysTime(0)
        }
        mBinding.uninstallBtn.setOnClickListener {
            //csdkManager.removeInstallPackageWhiteList(arrayListOf("com.baidu.searchbox"))
            ManageApplication.uninstallPackage("com.coolapk.market")
        }
        mBinding.installBtn.setOnClickListener {
            var file = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)
            var name =  getExternalFilesDir(null).toString()
            var sb = StringBuffer()
            ManageApplication.getInstallPackageWhiteList().forEach {
                sb.append("$it,")
            }
            Toast("路径$name")
            LogShow("白名单" + sb.toString())
            ManageApplication.addInstallPackageWhiteList(
                arrayListOf(
                    "com.xhly.manageapp", "com.coolapk.market"
                )
            )


            ManageApplication.disableInstallation(false)

            //ManageApplication.addInstallPackageWhiteList(arrayListOf("com.coolapk.market"))

           // ManageApplication.removeInstallPackageWhiteList(arrayListOf("com.coolapk.market"))
            ManageApplication.installPackage(File(name, "英语词典.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
            }
            ManageApplication.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 = ManageApplication.getDeviceInfo(indexOf + 1)
                LogShow("$it==$deviceInfo")
            }
        }
        var num = 0
        mBinding.resetBtn.setOnClickListener {
            num++
            Toast("还有${5 - num}次，恢复出厂")
            if (num == 5) {
                var launchFactoryReset = ManageApplication.launchFactoryReset()
                Toast("恢复出厂成功=" + launchFactoryReset)
            }
        }
        mBinding.launcherBtn.setOnClickListener {
            //Toast("设置成功"+csdkManager.setLauncher("com.xhly.manageapp"))
           ManageApplication.setCustomLauncher(
                "com.xhly.manageapp",
                "com.xhly.manageapp.ui.ManageActivity"
            )
        }
        var type12 = 0
        mBinding.airbtnBtn.setOnClickListener {
            type12 = getType(type12)
            Toast("禁止操作" +(type12 == 0))
            ManageApplication.disallowAirplaneMode_v3(type12 == 0)
        }
        var type13 = 0
        mBinding.statusBtn.setOnClickListener {
            type13 = getType(type13)
            Toast("禁止操作"+(type13 == 0))
            ManageApplication.hideStatusBar(type13 == 0)
        }
        var type14 = 0
        mBinding.statuspanelBtn.setOnClickListener {
            type14 = getType(type14)
            Toast("禁止操作" +(type14 == 0))
            ManageApplication.disableStatusBarPanel(type14 == 0)
        }
        var type15 = 0
        mBinding.hidekeyBtn.setOnClickListener {
            type15 = getType(type15)
            var b = (type15 == 0)
            ManageApplication.hideBackSoftKey(b)
            ManageApplication.hideMenuSoftKey(b)
            ManageApplication.hideHomeSoftKey(b)
        }
    }



    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
    }
}