package com.hikoncont.activity

import android.app.Activity
import android.content.Intent
import android.media.projection.MediaProjectionManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity

/**
 * 简单的权限申请Activity
 * 专门用于测试MIUI设备的MediaProjection权限对话框显示问题
 * 这个Activity尽可能简单，避免任何可能干扰权限对话框显示的因素
 */
class SimplePermissionActivity : AppCompatActivity() {
    
    companion object {
        private const val TAG = "SimplePermissionActivity"
        private const val REQUEST_MEDIA_PROJECTION = 1001
    }
    
    private var mediaProjectionManager: MediaProjectionManager? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        Log.i(TAG, "🧪 SimplePermissionActivity创建")
        Log.i(TAG, "📱 设备信息: ${android.os.Build.MANUFACTURER} ${android.os.Build.MODEL}")
        
        // 不设置任何布局，保持最简单的状态
        
        // 立即申请权限
        requestMediaProjectionPermission()
    }
    
    private fun requestMediaProjectionPermission() {
        try {
            Log.i(TAG, "🧪 开始最简单的MediaProjection权限申请")
            
            mediaProjectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
            
            val intent = mediaProjectionManager?.createScreenCaptureIntent()
            if (intent == null) {
                Log.e(TAG, "❌ 创建权限Intent失败")
                finishWithResult(false)
                return
            }
            
            Log.i(TAG, "🧪 权限Intent创建成功")
            Log.i(TAG, "  - Component: ${intent.component}")
            Log.i(TAG, "  - Action: ${intent.action}")
            
            // 最简单的启动方式
            startActivityForResult(intent, REQUEST_MEDIA_PROJECTION)
            
            Log.i(TAG, "🧪 权限对话框启动完成")
            
        } catch (e: Exception) {
            Log.e(TAG, "❌ 权限申请失败", e)
            finishWithResult(false)
        }
    }
    
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        
        Log.i(TAG, "🧪 收到权限结果")
        Log.i(TAG, "  - requestCode: $requestCode")
        Log.i(TAG, "  - resultCode: $resultCode")
        Log.i(TAG, "  - data: $data")
        
        when (requestCode) {
            REQUEST_MEDIA_PROJECTION -> {
                val success = resultCode == Activity.RESULT_OK && data != null
                Log.i(TAG, "🧪 权限申请结果: ${if (success) "成功" else "失败"}")
                
                if (success) {
                    // 将权限数据发送回MainActivity
                    val resultIntent = Intent().apply {
                        putExtra("resultCode", resultCode)
                        putExtra("resultData", data)
                    }
                    setResult(Activity.RESULT_OK, resultIntent)
                } else {
                    setResult(Activity.RESULT_CANCELED)
                }
                
                finish()
            }
        }
    }
    
    private fun finishWithResult(success: Boolean) {
        setResult(if (success) Activity.RESULT_OK else Activity.RESULT_CANCELED)
        finish()
    }
    
    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "🧪 SimplePermissionActivity销毁")
    }
} 