package com.lila.wakeup

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

/**
 * 状态查看 Activity（开发期辅助 + 首次启动权限申请入口）。
 *
 * 职责：
 *  1. 首次启动时弹麦克风权限申请
 *  2. 启动 [WakeupForegroundService]
 *  3. 显示当前状态（开发期可选）
 *
 * 量产时（方案 B）改为 launcher 隐藏（删 LAUNCHER intent-filter），
 * 用户不用进 APP 也能开机自启。
 */
class MainActivity : AppCompatActivity() {

    companion object {
        private const val TAG = "KwsService.Main"
        private const val PERMISSION_REQUEST_CODE = 1001
    }

    private lateinit var statusText: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 简化：用一个 TextView 直接显示状态，不用 layout xml
        statusText = TextView(this).apply {
            text = "Lila 语音唤醒服务\n\n初始化中..."
            textSize = 18f
            setPadding(60, 60, 60, 60)
        }
        setContentView(statusText)

        checkAndRequestPermissions()
    }

    private fun checkAndRequestPermissions() {
        val needRequestList = mutableListOf<String>()

        // 麦克风权限（核心）
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED
        ) {
            needRequestList.add(Manifest.permission.RECORD_AUDIO)
        }

        // 通知权限（Android 13+）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
                != PackageManager.PERMISSION_GRANTED
            ) {
                needRequestList.add(Manifest.permission.POST_NOTIFICATIONS)
            }
        }

        if (needRequestList.isNotEmpty()) {
            ActivityCompat.requestPermissions(
                this,
                needRequestList.toTypedArray(),
                PERMISSION_REQUEST_CODE
            )
        } else {
            startWakeupService()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode != PERMISSION_REQUEST_CODE) return

        val recordIdx = permissions.indexOf(Manifest.permission.RECORD_AUDIO)
        if (recordIdx >= 0 && grantResults[recordIdx] == PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "RECORD_AUDIO granted")
            startWakeupService()
        } else {
            statusText.text = "Lila 语音唤醒服务\n\n❌ 麦克风权限被拒绝，无法启动唤醒"
            Log.e(TAG, "RECORD_AUDIO denied, service NOT started")
        }
    }

    private fun startWakeupService() {
        val intent = Intent(this, WakeupForegroundService::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent)
        } else {
            startService(intent)
        }
        statusText.text = """
            Lila 语音唤醒服务

            ✅ 服务已启动
            ✅ 监听唤醒词中

            可关闭本界面，服务会在后台持续运行。
            通知栏可看到运行状态。

            日志：adb logcat -s KwsService
        """.trimIndent()
        Log.i(TAG, "WakeupForegroundService start requested")
    }
}
