package com.audio.record.screen.test.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.media.projection.MediaProjectionManager
import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import com.audio.record.screen.test.base.BaseActivity
import com.audio.record.screen.test.databinding.ActivityPlayBinding
import com.audio.record.screen.test.service.FloatingWindowBridge
import com.audio.record.screen.test.tool.Common


/**
 * Service悬浮窗中请求权限的透明Activity
 */
class ScreenPermissionActivity : AppCompatActivity() {

    private lateinit var screenCaptureLauncher: ActivityResultLauncher<Intent>

    //录音权限
    private lateinit var micLauncher: ActivityResultLauncher<String>
    companion object{

        const val key_with_audio="key_with_audio"
    }
    private var withAudio = false

    private lateinit var mediaProjectionManager:MediaProjectionManager
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
//            Common.hideSystemBars(this)
//            Common.setStatusBarTextColor(this, false)
            setFullScreenTransparent()
        }
    }
    private fun setFullScreenTransparent() {
        WindowCompat.setDecorFitsSystemWindows(window, false)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.insetsController?.let {
                it.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
                it.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            }
        } else {
            @Suppress("DEPRECATION")
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                        View.SYSTEM_UI_FLAG_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        }

        window.statusBarColor = Color.TRANSPARENT
        window.navigationBarColor = Color.TRANSPARENT
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        Common.setStatusBarTextColor(this, true)
          mediaProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
        withAudio = intent.getBooleanExtra(key_with_audio, false)
        screenCaptureLauncher = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) { result ->
            Common.showLog("--------------screenCaptureLauncher")
            if (result.resultCode == RESULT_OK && result.data != null) {
                val data: Intent? = result.data
                FloatingWindowBridge.updateMediaProjection(result.resultCode, data!!)
            }
            finish()
        }

        micLauncher = registerForActivityResult(
            ActivityResultContracts.RequestPermission()
        ) { isGranted ->
            if (isGranted) {
                Common.showLog("mic 权限授予")
                requestScreenPermission()
            } else {
                Common.showLog("mic 权限拒绝")
            }
        }
        if(withAudio){
            micLauncher.launch(android.Manifest.permission.RECORD_AUDIO)
        }else{
            requestScreenPermission()
        }
    }


    private fun requestScreenPermission() {
        Common.showLog("--------------requestScreenPermission")
        val intent = mediaProjectionManager.createScreenCaptureIntent()
        screenCaptureLauncher.launch(intent)
    }



}