package com.example.modifier.utils

import android.provider.Settings
import com.example.modifier.constants.CMD_HOME
import com.example.modifier.constants.CMD_MESSAGING_APP
import com.example.modifier.constants.CMD_START_PLAY_STORE
import com.example.modifier.constants.PACKAGE_GMS
import com.example.modifier.constants.PACKAGE_GSF
import com.example.modifier.constants.PACKAGE_MESSAGING
import com.example.modifier.constants.PACKAGE_PLAY
import com.example.modifier.extension.clear
import com.example.modifier.extension.disable
import com.example.modifier.extension.kill

suspend fun clear(gsf: Boolean, gms: Boolean, sms: Boolean) {
    try {
        val cmds: MutableList<String> = ArrayList()
        // suspend
        if (gsf) {
            cmds.add(PACKAGE_GSF.kill())
            cmds.add("echo 'gsf suspended'")
        }
        if (gms) {
            cmds.add(PACKAGE_GMS.kill())
            cmds.add("echo 'gms suspended'")
        }
        if (sms) {
            cmds.add(PACKAGE_MESSAGING.kill())
            cmds.add("echo 'sms suspended'")
        }
        cmds.add("sleep 1")
        // clear
        if (gsf) {
            cmds.add(PACKAGE_GSF.clear())
            cmds.add("echo 'cleared gsf'")
        }
        if (gms) {
            cmds.add(PACKAGE_GMS.clear())
            cmds.add("echo 'cleared gms'")
        }
        if (sms) {
            cmds.add(PACKAGE_MESSAGING.clear())
            cmds.add("echo 'cleared sms'")
        }
        cmds.add("sleep 1")
        cmds.add("sleep 1")
        shellRun(*cmds.toTypedArray<String>())
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

suspend fun resetAll() {
    try {
        clearConv()
        shellRun(
            "settings put secure location_mode 0",
            "settings put secure android_id ${genAndroidId()}",
            PACKAGE_PLAY.disable(),
            PACKAGE_GSF.clear(),
            PACKAGE_GMS.clear(),
            PACKAGE_PLAY.clear(),
            CMD_HOME,
            "sleep 10",
            PACKAGE_GMS.clear(),
            "sleep 2",
            "pm revoke $PACKAGE_GMS android.permission.NEARBY_WIFI_DEVICES",
            "pm revoke $PACKAGE_GMS android.permission.BLUETOOTH_CONNECT",
            "pm revoke $PACKAGE_GMS android.permission.BLUETOOTH_SCAN",
            "pm revoke $PACKAGE_GMS android.permission.BLUETOOTH_ADVERTISE",
            "pm revoke $PACKAGE_GMS android.permission.UWB_RANGING",
            "pm revoke $PACKAGE_GMS android.permission.GET_ACCOUNTS",
            "pm revoke $PACKAGE_GMS android.permission.SYSTEM_ALERT_WINDOW",
            "pm revoke $PACKAGE_GMS android.permission.POST_NOTIFICATIONS",
            "pm revoke $PACKAGE_GMS android.permission.CAMERA",
            "pm revoke $PACKAGE_GMS android.permission.RECEIVE_MMS",
            "pm revoke $PACKAGE_GMS android.permission.GET_APP_OPS_STATS",
            "pm revoke $PACKAGE_GMS android.permission.PROCESS_OUTGOING_CALLS",
            "pm revoke $PACKAGE_GMS android.permission.READ_CALL_LOG",
            "pm revoke $PACKAGE_GMS android.permission.WRITE_CONTACTS",
            "pm revoke $PACKAGE_GMS android.permission.CALL_PHONE",
            "pm revoke $PACKAGE_GMS android.permission.RECORD_AUDIO",
            "pm revoke $PACKAGE_GMS android.permission.READ_LOGS",
            "pm revoke $PACKAGE_GMS android.permission.READ_MEDIA_AUDIO",
            "pm revoke $PACKAGE_GMS android.permission.READ_MEDIA_IMAGES",
            "pm revoke $PACKAGE_GMS android.permission.READ_MEDIA_VIDEO",
            "pm revoke $PACKAGE_GMS android.permission.ACCESS_MEDIA_LOCATION",
            "pm revoke $PACKAGE_GMS android.permission.ACCESS_BROADCAST_RESPONSE_STATS",
            "pm revoke $PACKAGE_GMS android.permission.WRITE_CALL_LOG",
            "pm revoke $PACKAGE_GMS android.permission.BODY_SENSORS",
            "pm revoke $PACKAGE_GMS android.permission.DUMP",
            "appops set $PACKAGE_GMS WIFI_SCAN ignore",
            "appops set $PACKAGE_GMS BLUETOOTH_SCAN ignore",
            "appops set $PACKAGE_GMS FINE_LOCATION_SOURCE ignore",
            "appops set $PACKAGE_GMS ACCESS_RESTRICTED_SETTINGS ignore",
            "sleep 10",
            "cmd role add-role-holder android.app.role.SMS $PACKAGE_MESSAGING",
            PACKAGE_MESSAGING.clear(),
            CMD_MESSAGING_APP,
            "sleep 3",
            CMD_MESSAGING_APP.kill(),
            "sleep 3",
        )
    } catch (e: Exception) {
        e.printStackTrace()
    }
}