package com.dphms.netguard

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment

class ToolsFragment : Fragment() {

    private lateinit var output: TextView

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.tab_tools, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        output = view.findViewById(R.id.tvToolsOutput)

        view.findViewById<Button>(R.id.btnDataOn).setOnClickListener {
            execCommand("svc data enable && settings put global mobile_data 1")
            appendOutput("→ Mobile Data ENABLED")
        }

        view.findViewById<Button>(R.id.btnDataOff).setOnClickListener {
            execCommand("svc data disable && settings put global mobile_data 0")
            appendOutput("→ Mobile Data DISABLED")
        }

        view.findViewById<Button>(R.id.btnAirplaneOn).setOnClickListener {
            execCommand("cmd connectivity airplane-mode enable")
            appendOutput("→ Airplane Mode ON")
        }

        view.findViewById<Button>(R.id.btnAirplaneOff).setOnClickListener {
            execCommand("cmd connectivity airplane-mode disable")
            appendOutput("→ Airplane Mode OFF")
        }

        view.findViewById<Button>(R.id.btnSoftReset).setOnClickListener {
            Thread {
                appendOutput("→ Starting soft reset sequence...")
                execCommand("svc data disable")
                Thread.sleep(500)
                execCommand("cmd connectivity airplane-mode enable")
                Thread.sleep(3000)
                execCommand("am force-stop com.android.phone")
                Thread.sleep(1000)
                execCommand("cmd connectivity airplane-mode disable")
                Thread.sleep(3000)
                execCommand("svc data enable")
                appendOutput("→ Soft reset complete! Data should be working.")
            }.start()
        }

        view.findViewById<Button>(R.id.btnMotoProps).setOnClickListener {
            execCommand("getprop | grep -E 'vendor.radio|gsm|ril|ro.vendor.carrier|persist.vendor.radio'")
        }

        view.findViewById<Button>(R.id.btnCarrier).setOnClickListener {
            execCommand("getprop ro.vendor.carrier && getprop gsm.operator.alpha && getprop gsm.sim.operator.alpha")
        }
    }

    private fun execCommand(cmd: String) {
        Thread {
            val service = ShizukuManager.getService()
            if (service == null) {
                appendOutput("ERROR: Shizuku not ready.")
                return@Thread
            }
            val result = service.executeCommand(cmd)
            if (result.isNotBlank()) {
                appendOutput(result.trim())
            }
        }.start()
    }

    private fun appendOutput(text: String) {
        activity?.runOnUiThread {
            output.text = output.text.toString() + "\n\n" + text
        }
    }
}
