package com.xhly.manageapp.utils

import com.xhly.corelib.Const
import com.xhly.corelib.utils.hourToCurrentDateLong
import com.xhly.manageapp.ManageApplication
import com.xhly.manageapp.bean.strategy.StrategyBean
import java.util.Calendar

object StrategyUtils {
    public fun setStrategy(strategyBean: StrategyBean) {
        //是否启用白名单
        if (strategyBean.haswifi == 1) {
            ManageApplication.enableWifiWhiteList(true)
            strategyBean.wifis?.let {
                val list = arrayListOf<String>()
                it.forEach { s ->
                    if (!s.isNullOrBlank()) {
                        list.add(s)
                    }
                }
                ManageApplication.addWifiWhiteList(list)
            }
        } else {
            ManageApplication.enableWifiWhiteList(false)
        }
        //是否开启应用策略
        if (strategyBean.hasapp == 1) {
            //开启
            strategyBean.apps?.let { appArray ->
                val list = arrayListOf<String>()
                appArray.forEach {
                    list.add(it.appPackage)
                }
                ManageApplication.addInstallPackageWhiteList(list)
            }
        } else {
            //不开启应用策略
            val installPackageWhiteList = ManageApplication.getInstallPackageWhiteList()
            ManageApplication.removeInstallPackageWhiteList(installPackageWhiteList)
        }

        //是否开启时间策略，这个策略在生命周期中调用getAppTimeStrategy(strategyBean)


        //是否设备策略
        if (strategyBean.hasdevice == 1) {
            //开启
            deviceStrategy(strategyBean)
        } else {
            //不设置则默认全部打开
            deviceStrategy(strategyBean)
        }

        //违规处理,直接在事件触发时判断，不再这里处理
        if (strategyBean.hasviolate == 1) {

        }
    }

    private fun deviceStrategy(strategyBean: StrategyBean) {
        val padDevices = strategyBean.padDevices
        padDevices?.forEach {
            when (it.pdtype) {
                Const.DEVICETYPE1 -> {
                    ManageApplication.disableCamera(it.enabled == 1)
                }

                Const.DEVICETYPE2 -> {
                    ManageApplication.enableBluetooth(it.enabled == 1)
                }

                Const.DEVICETYPE3 -> {
                    ManageApplication.disallowUsbDebugging(it.enabled == 1)
                }

                Const.DEVICETYPE4 -> {
                    ManageApplication.enableWifi(it.enabled == 1)
                }

                Const.DEVICETYPE5 -> {

                }

                Const.DEVICETYPE6 -> {
                    ManageApplication.enableData(it.enabled == 1)
                }

                Const.DEVICETYPE7 -> {
                    ManageApplication.enableLocation(it.enabled == 1)
                }

                Const.DEVICETYPE8 -> {
                    ManageApplication.disallowWifiAdvanceSettings(it.enabled != 1)
                }

                Const.DEVICETYPE9 -> {
                    ManageApplication.enableMassStorage(it.enabled == 1)
                }

                Const.DEVICETYPE10 -> {
                    ManageApplication.setCustomOTG(it.enabled == 1)
                }
            }
        }
    }

    /**
     * 时间策略处理，返回为真则锁定
     */
    public fun getAppTimeStrategy(strategyBean: StrategyBean): Boolean {
        //是否开启时间策略，返回为真则锁定
        var lockFlag = false
        val dayList = arrayListOf(2, 3, 4, 5, 6, 7, 1)
        val currentTimeMillis = System.currentTimeMillis()
        if (strategyBean.hastime == 1) {
            val calendar = Calendar.getInstance()
            //返回值 1 周日 2周一 3周二 4周三 5周四 6周五 7周六
            val dayWeek = calendar.get(Calendar.DAY_OF_WEEK)
            val padTimes = strategyBean.padTimes
            if (padTimes == null || padTimes.size == 0) {
                //开启策略，但不返回时间则表示锁定
                return true
            }
            padTimes?.forEach { appTimeManageBean ->
                val day = dayList[appTimeManageBean.week - 1]
                if (day == dayWeek) {
                    val starttime = appTimeManageBean.starttime
                    val stoptime = appTimeManageBean.stoptime
                    starttime?.let {
                        val start = starttime.hourToCurrentDateLong()
                        stoptime?.let {
                            val end = stoptime.hourToCurrentDateLong()
                            //只有有一个时段符合即为可用
                            if (currentTimeMillis > start && currentTimeMillis < end) {
                                //在使用范围不锁定
                                return false
                            } else {
                                lockFlag = true
                            }
                        }
                    }
                }else{
                    lockFlag = true
                }
            }
            return lockFlag
        }
        return false
    }

    public fun getAppTimeStrategy() {

    }
}