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 DiagnosticsFragment : Fragment() {

    private lateinit var output: TextView

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

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

        view.findViewById<Button>(R.id.btnSignal).setOnClickListener {
            execCommand("dumpsys telephony.registry | grep -i 'mSignalStrength' | head -5")
        }

        view.findViewById<Button>(R.id.btnCellInfo).setOnClickListener {
            execCommand("dumpsys telephony.registry | grep -i 'mCellIdentity' | head -10")
        }

        view.findViewById<Button>(R.id.btnServiceState).setOnClickListener {
            execCommand("dumpsys telephony.registry | grep -i 'mServiceState' | head -5")
        }

        view.findViewById<Button>(R.id.btnVoLTE).setOnClickListener {
            execCommand("dumpsys telephony.registry | grep -iE 'mImsRegistered|ImsConfig|VoLTE|VoNR' | head -10")
        }

        view.findViewById<Button>(R.id.btnConnectivity).setOnClickListener {
            execCommand("dumpsys connectivity | grep -E 'NetworkAgentInfo|LinkProperties|NetworkCapabilities' | head -15")
        }

        view.findViewById<Button>(R.id.btnValidation).setOnClickListener {
            execCommand("dumpsys connectivity | grep -i 'VALIDATED' | head -5")
        }

        view.findViewById<Button>(R.id.btnDnsCheck).setOnClickListener {
            execCommand("ping -c 4 8.8.8.8")
        }

        view.findViewById<Button>(R.id.btnRadioLog).setOnClickListener {
            execCommand("logcat -b radio -d -v time | grep -E 'RIL|DctController|Telephony|IMS' | tail -20")
        }

        view.findViewById<Button>(R.id.btnQueryApn).setOnClickListener {
            execCommand("content query --uri content://telephony/carriers/preferapn")
        }

        view.findViewById<Button>(R.id.btnListApn).setOnClickListener {
            execCommand("content query --uri content://telephony/carriers --projection name:apn:type:mcc:mnc 2>/dev/null | head -20")
        }

        view.findViewById<Button>(R.id.btnRadioInfo).setOnClickListener {
            execCommand("am start -n com.android.settings/.RadioInfo")
            appendOutput("→ Opening Radio Info Menu...")
        }

        view.findViewById<Button>(R.id.btnCqaTest).setOnClickListener {
            execCommand("am start -n com.motorola.cqatest/.CQATestMain")
            appendOutput("→ Opening Motorola CQA Test Menu...")
        }
    }

    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())
            } else {
                appendOutput("(no output)")
            }
        }.start()
    }

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