package com.example.hmg_qline.hmg_qline

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.io.File

class MainActivity : FlutterActivity() {
    private val CHANNEL = "com.example.hmg_qline/foreground"

    companion object {
        private const val TAG = "MainActivity"
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            Log.d(TAG, "MethodChannel call received: ${call.method}")

            when (call.method) {
                "reopenApp" -> {
                    Log.d(TAG, "reopenApp called, bringing app to foreground")
                    moveTaskToBack(false)
                    result.success("App brought to foreground")
                }

                "restartApp" -> {
                    Log.d(TAG, "Restarting application")
                    restartApplication()
                    result.success("App restart initiated")
                }

                "restartDevice" -> {
                    Log.d(TAG, "Attempting device restart")
                    restartDevice(result)
                }

                "runShellScript" -> {
                    Log.d(TAG, "Executing shell restart command")
                    executeShellRestart(result)
                }

                "clearAudioCache" -> {
                    Log.d(TAG, "Clearing audio cache")
                    clearAudioResources()
                    result.success("Audio cache cleared")
                }

                "clearAllResources" -> {
                    Log.d(TAG, "Clearing all native resources")
                    clearAllNativeResources()
                    result.success("All resources cleared")
                }

                // === NEW: Alarm Scheduling Methods for Android 14+ compatibility ===

                "scheduleRestartAlarm" -> {
                    val hour = call.argument<Int>("hour") ?: 0
                    val minute = call.argument<Int>("minute") ?: 15
                    Log.d(TAG, "Scheduling restart alarm for $hour:$minute")
                    scheduleRestartAlarm(hour, minute)
                    result.success("Restart alarm scheduled for $hour:$minute")
                }

                "cancelRestartAlarm" -> {
                    Log.d(TAG, "Cancelling restart alarm")
                    AlarmScheduler.cancelRestartAlarm(this)
                    result.success("Restart alarm cancelled")
                }

                "canScheduleExactAlarms" -> {
                    val canSchedule = AlarmScheduler.canScheduleExactAlarms(this)
                    Log.d(TAG, "Can schedule exact alarms: $canSchedule")
                    result.success(canSchedule)
                }

                "requestExactAlarmPermission" -> {
                    Log.d(TAG, "Requesting exact alarm permission")
                    AlarmScheduler.requestExactAlarmPermission(this)
                    result.success("Permission request initiated")
                }

                else -> {
                    Log.w(TAG, "Method not implemented: ${call.method}")
                    result.notImplemented()
                }
            }
        }
    }

    /**
     * Schedule daily restart alarm at specified time.
     * Compatible with Android 14+ and older versions.
     */
    private fun scheduleRestartAlarm(hour: Int, minute: Int) {
        try {
            AlarmScheduler.scheduleRestartAlarm(this, hour, minute)
            Log.d(TAG, "Restart alarm scheduled successfully for $hour:$minute")
        } catch (e: Exception) {
            Log.e(TAG, "Error scheduling restart alarm: ${e.message}")
        }
    }

    private fun restartApplication() {
        try {
            Log.d(TAG, "Initiating app restart")

            // Get the launch intent
            val intent = packageManager.getLaunchIntentForPackage(packageName)

            if (intent != null) {
                // Configure intent for clean restart
                intent.apply {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
                    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                    putExtra("restarted", true)
                }

                // Use AlarmManager for reliable restart (works better on Android 14+)
                val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
                val pendingIntent = PendingIntent.getActivity(
                    this,
                    0,
                    intent,
                    PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
                )

                // Schedule restart in 500ms
                alarmManager.set(
                    AlarmManager.RTC,
                    System.currentTimeMillis() + 500,
                    pendingIntent
                )

                Log.d(TAG, "Restart scheduled via AlarmManager")

                // Now safely exit the app
                Handler(Looper.getMainLooper()).postDelayed({
                    finishAffinity()
                    android.os.Process.killProcess(android.os.Process.myPid())
                }, 100)

            } else {
                Log.e(TAG, "Could not create restart intent")
            }

        } catch (e: Exception) {
            Log.e(TAG, "Error during restart: ${e.message}")
            // Fallback: try simple restart
            fallbackRestart()
        }
    }

    /**
     * Fallback restart method if AlarmManager approach fails
     */
    private fun fallbackRestart() {
        try {
            Log.d(TAG, "Attempting fallback restart")
            val intent = packageManager.getLaunchIntentForPackage(packageName)?.apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
                putExtra("restarted", true)
            }

            if (intent != null) {
                startActivity(intent)
                finishAffinity()
                Runtime.getRuntime().exit(0)
            }
        } catch (e: Exception) {
            Log.e(TAG, "Fallback restart also failed: ${e.message}")
        }
    }

    private fun restartDevice(result: MethodChannel.Result) {
        try {
            Log.d("MainActivity", "Attempting device restart (requires root)")

            // Try different reboot commands
            val rebootCommands = arrayOf(
                arrayOf("su", "-c", "reboot"),
                arrayOf("su", "-c", "reboot now"),
                arrayOf("reboot")
            )

            var success = false
            for (command in rebootCommands) {
                try {
                    val process = Runtime.getRuntime().exec(command)
                    val exitCode = process.waitFor()

                    if (exitCode == 0) {
                        success = true
                        Log.d("MainActivity", "Device restart command executed successfully")
                        result.success("Device restart initiated")
                        break
                    }
                } catch (e: Exception) {
                    Log.w("MainActivity", "Reboot command failed: ${command.joinToString(" ")}")
                }
            }

            if (!success) {
                Log.e("MainActivity", "All reboot commands failed - device may not have root access")
                result.error("RESTART_ERROR", "Device restart failed - root access required", null)
            }

        } catch (e: Exception) {
            Log.e("MainActivity", "Exception during device restart: ${e.message}")
            result.error("RESTART_ERROR", "Device restart failed: ${e.message}", null)
        }
    }

    private fun executeShellRestart(result: MethodChannel.Result) {
        try {
            Log.d("MainActivity", "Executing shell restart script")

            // Copy script from assets to internal storage
            val scriptFile = copyAssetToFile("scripts/restart_app.sh")

            if (scriptFile != null && scriptFile.exists()) {
                // Make script executable
                Runtime.getRuntime().exec("chmod 755 ${scriptFile.absolutePath}")

                // Execute the script
                val process = Runtime.getRuntime().exec("su -c ${scriptFile.absolutePath}")
                val exitCode = process.waitFor()

                if (exitCode == 0) {
                    result.success("Shell restart executed successfully")
                } else {
                    result.error("SHELL_ERROR", "Script execution failed with code: $exitCode", null)
                }
            } else {
                result.error("SCRIPT_ERROR", "Could not copy script from assets", null)
            }

        } catch (e: Exception) {
            Log.e("MainActivity", "Shell restart error: ${e.message}")
            result.error("SHELL_EXCEPTION", e.message, null)
        }
    }

    private fun copyAssetToFile(assetPath: String): File? {
        return try {
            val inputStream = assets.open(assetPath)
            val file = File(filesDir, "restart_app.sh")

            file.outputStream().use { output ->
                inputStream.copyTo(output)
            }

            file
        } catch (e: Exception) {
            Log.e("MainActivity", "Error copying asset: ${e.message}")
            null
        }
    }


    private fun clearAudioResources() {
        try {
            Log.d("MainActivity", "Clearing audio resources")

            // Force garbage collection
            System.gc()

            // Clear audio-related system caches
            val commands = arrayOf(
                "am force-stop com.google.android.tts",
                "am force-stop com.android.providers.media"
            )

            for (command in commands) {
                try {
                    Runtime.getRuntime().exec(command)
                } catch (e: Exception) {
                    Log.w("MainActivity", "Failed to clear audio service: $command")
                }
            }

        } catch (e: Exception) {
            Log.e("MainActivity", "Error clearing audio resources: ${e.message}")
        }
    }

    private fun clearAllNativeResources() {
        try {
            Log.d("MainActivity", "Clearing all native resources")

            // Clear audio resources
            clearAudioResources()

            // Clear image caches
            try {
                Runtime.getRuntime().exec("am broadcast -a android.intent.action.MEDIA_MOUNTED")
            } catch (e: Exception) {
                Log.w("MainActivity", "Failed to clear media cache")
            }

            // Force garbage collection
            System.gc()
            System.runFinalization()

            Log.d("MainActivity", "Native resources cleared")

        } catch (e: Exception) {
            Log.e("MainActivity", "Error clearing native resources: ${e.message}")
        }
    }

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

        // Log if app was restarted
        if (intent.getBooleanExtra("restarted", false)) {
            Log.d(TAG, "App restarted successfully")
        }

        // Log if launched from boot
        if (intent.getBooleanExtra("launched_from_boot", false)) {
            Log.d(TAG, "App launched from boot")
            // Give system time to settle after boot
            Thread.sleep(2000)
        }

        // Schedule daily restart alarm at 00:15 (12:15 AM)
        // This ensures the alarm is always set when app starts
        initializeRestartAlarm()
    }

    /**
     * Initialize the daily restart alarm.
     * Called on app start to ensure alarm is always scheduled.
     */
    private fun initializeRestartAlarm() {
        try {
            Log.d(TAG, "Initializing daily restart alarm")
            AlarmScheduler.scheduleRestartAlarm(this, 0, 15) // 00:15 (12:15 AM)
            Log.d(TAG, "Daily restart alarm initialized for 00:15")
        } catch (e: Exception) {
            Log.e(TAG, "Error initializing restart alarm: ${e.message}")
        }
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "Activity resumed")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "Activity paused - cleaning up resources")

        // Light cleanup when app goes to background
        System.gc()
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "Activity destroyed")

        // Final cleanup
        clearAllNativeResources()
    }
}


//package com.example.hmg_qline.hmg_qline
//
//import android.content.Intent
//import android.os.Bundle
//import android.util.Log
//import io.flutter.embedding.android.FlutterActivity
//import io.flutter.embedding.engine.FlutterEngine
//import io.flutter.plugin.common.MethodChannel
//
//class MainActivity : FlutterActivity() {
//    private val CHANNEL = "com.example.hmg_qline/foreground"
//
//    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
//        super.configureFlutterEngine(flutterEngine)
//
//        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
//            Log.d("MainActivity", "MethodChannel call received: ${call.method}")
//            when (call.method) {
//                "reopenApp" -> {
//                    Log.d("MainActivity", "reopenApp called, bringing app to foreground")
//                    // Simply bring current activity to foreground
//                    moveTaskToBack(false)
//                    // Or trigger Flutter navigation if needed
//                    result.success("App brought to foreground")
//                }
//
//                else -> result.notImplemented()
//            }
//
//
//            when (call.method) {
//                "runShellScript" -> {
//                    Log.d("MainActivity", "reopenApp called, bringing app to foreground")
//                    // Simply bring current activity to foreground
//                    moveTaskToBack(false)
//                    // Or trigger Flutter navigation if needed
//                    result.success("App brought to foreground")
//                }
//
//                else -> result.notImplemented()
//            }
//
//
//        }
//    }
//
//
//}
//
//
////}
////
////    private fun hasShellPermissions(): Boolean {
////        return try {
////            val process = Runtime.getRuntime().exec("id")
////            process.waitFor()
////            val exitCode = process.exitValue()
////            Log.d("MainActivity", "Shell permission check exit code: $exitCode")
////            exitCode == 0
////        } catch (e: Exception) {
////            Log.e("MainActivity", "Error checking shell permissions: ${e.message}")
////            false
////        }
////    }
////
////    private fun executeShellScript(result: MethodChannel.Result) {
////        try {
////            // Copy script from assets to internal storage
////            val assetManager = assets
////            val inputStream = assetManager.open("restart_app.sh")
////            val scriptFile = java.io.File(filesDir, "restart_app.sh")
////
////            scriptFile.outputStream().use { output ->
////                inputStream.copyTo(output)
////            }
////
////            // Make script executable
////            scriptFile.setExecutable(true)
////
////            // Execute the script
////            val process = Runtime.getRuntime().exec("sh ${scriptFile.absolutePath}")
////            val exitCode = process.waitFor()
////
////            if (exitCode == 0) {
////                Log.d("MainActivity", "Shell script executed successfully")
////                result.success("Script executed successfully")
////            } else {
////                val errorStream = process.errorStream.bufferedReader().readText()
////                Log.e("MainActivity", "Script execution failed with exit code: $exitCode, error: $errorStream")
////                result.error("SCRIPT_ERROR", "Script execution failed", errorStream)
////            }
////
////        } catch (e: Exception) {
////            Log.e("MainActivity", "Error executing shell script: ${e.message}")
////            result.error("EXECUTION_ERROR", "Failed to execute script: ${e.message}", null)
////        }
////    }
