package com.dphms.netguard

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class MainActivity : AppCompatActivity() {

    private lateinit var viewPager: ViewPager2
    private lateinit var tabLayout: TabLayout
    private lateinit var shizukuStatus: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        shizukuStatus = findViewById(R.id.shizukuStatus)
        viewPager = findViewById(R.id.viewPager)
        tabLayout = findViewById(R.id.tabLayout)

        viewPager.adapter = TabsPagerAdapter(this)
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            tab.text = when (position) {
                0 -> "NETWORK"
                1 -> "DIAGNOSE"
                2 -> "SCANNER"
                3 -> "TOOLS"
                else -> ""
            }
        }.attach()

        checkShizukuStatus()

        val perms = arrayOf(
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.POST_NOTIFICATIONS
        )
        val needed = perms.filter { checkSelfPermission(it) != PackageManager.PERMISSION_GRANTED }
        if (needed.isNotEmpty()) {
            requestPermissions(needed.toTypedArray(), 100)
        }
    }

    private fun checkShizukuStatus() {
        if (!ShizukuManager.isShizukuRunning()) {
            shizukuStatus.text = "● Shizuku: OFFLINE"
            shizukuStatus.setTextColor(getColor(R.color.accent_red))
            return
        }

        if (!ShizukuManager.hasPermission()) {
            shizukuStatus.text = "● Shizuku: NEED PERMISSION"
            shizukuStatus.setTextColor(getColor(R.color.accent_amber))
            ShizukuManager.requestPermission()
            return
        }

        ShizukuManager.bindService(this) { service: INetGuardService? ->
            if (service != null) {
                shizukuStatus.text = "● Shizuku: READY"
                shizukuStatus.setTextColor(getColor(R.color.accent_green))
            } else {
                shizukuStatus.text = "● Shizuku: BIND FAILED"
                shizukuStatus.setTextColor(getColor(R.color.accent_red))
            }
        }
    }

    override fun onResume() {
        super.onResume()
        checkShizukuStatus()
    }

    override fun onDestroy() {
        super.onDestroy()
        ShizukuManager.unbindService(this)
    }

    private class TabsPagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
        override fun getItemCount() = 4

        override fun createFragment(position: Int): Fragment = when (position) {
            0 -> NetworkFragment()
            1 -> DiagnosticsFragment()
            2 -> ScannerFragment()
            3 -> ToolsFragment()
            else -> NetworkFragment()
        }
    }
}
