package app.xixi.nomo

import android.app.ComponentCaller
import android.content.Intent
import android.graphics.Color
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import android.util.Log
import androidx.core.view.WindowCompat

class MainActivity: FlutterActivity() {

    private lateinit var coreApiImpl: CoreApiImpl
    companion object {
        private const val TAG = "MainActivity"
    }
    
    override fun provideFlutterEngine(context: android.content.Context): FlutterEngine? {
        // 使用预初始化的Flutter引擎
        val engine = App.getFlutterEngine()
        if (engine != null) {
            // 确保引擎状态正确
            engine.lifecycleChannel.appIsResumed()
            Log.d(TAG, "使用预初始化的Flutter引擎")
        } else {
            Log.w(TAG, "预初始化的Flutter引擎未找到，将创建新引擎")
        }
        return engine
    }
    
    override fun onCreate(savedInstanceState: android.os.Bundle?) {
        super.onCreate(savedInstanceState)
        VLog.init(this, "client")
        // 1. Edge-to-Edge 布局（内容延伸到状态栏和导航栏）
        WindowCompat.setDecorFitsSystemWindows(window, false)

        // 2. 针对Android 14+的系统导航栏透明处理
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            // Android 14+ 使用新的API
            window.isNavigationBarContrastEnforced = false
            window.navigationBarColor = Color.TRANSPARENT
            window.statusBarColor = Color.TRANSPARENT
        } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
            // Android 11+ 使用WindowInsetsController
            window.insetsController?.let { controller ->
                controller.systemBarsBehavior = 
                    android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                controller.setSystemBarsAppearance(
                    android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
                    android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
                )
            }
            window.navigationBarColor = Color.TRANSPARENT
            window.statusBarColor = Color.TRANSPARENT
        } else {
            // Android 10及以下使用传统方法
            window.decorView.systemUiVisibility = (
                android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            )
            window.navigationBarColor = Color.TRANSPARENT
            window.statusBarColor = Color.TRANSPARENT
        }

        Log.d(TAG, "MainActivity onCreate - 进程ID: ${android.os.Process.myPid()}")
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        val messenger = flutterEngine.dartExecutor.binaryMessenger
        
        // 创建 CoreApiImpl 实例
        coreApiImpl = CoreApiImpl(this)

        // 初始化 XrayApi
        coreApiImpl.initXrayApi();
        
        // 设置 XRayApi 状态监听
        coreApiImpl.setVpnServiceEventListener()
        
        // 设置 CoreApi
        CoreApi.setUp(messenger, coreApiImpl)
        
        // 注册事件流处理器
        coreApiImpl.registerEventStreamHandler(messenger)
        
        Log.d(TAG, "CoreApi 和事件流处理器已配置完成")
    }

//    override fun onResume() {
//        super.onResume()
//        coreApiImpl.xrayInit()
//    }
//
//    override fun onPause() {
//        super.onPause()
//        coreApiImpl.xrayUnInit()
//    }

    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, data)
        coreApiImpl.onActivityResult(requestCode, resultCode, data)
    }
}