package com.eventtan.test.penguin_flutter_sample

import android.content.Intent
import android.content.pm.PackageManager
import android.util.Log
import com.eventtan.app.penguin_flutter_sample.penguin.PenguinViewFactory
import com.eventtan.test.penguin_flutter_sample.PermissionManager.PermissionHelper
import com.eventtan.test.penguin_flutter_sample.PermissionManager.PermissionManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine


/**
 * The main activity for the Flutter application.
 * This activity is responsible for configuring the Flutter engine and registering platform view factories.
 */
class MainActivity : FlutterActivity() {



    private lateinit var flutterEngine: FlutterEngine

    /**
     * Configures the Flutter engine by registering platform view factories.
     *
     * @param flutterEngine The [FlutterEngine] instance associated with this activity.
     */
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        flutterEngine.platformViewsController.registry.registerViewFactory(
            "penguin_lib", // Unique identifier for the view factory
            PenguinViewFactory(
                flutterEngine.dartExecutor.binaryMessenger, // Used for communication with Dart
                this // Reference to the current MainActivity
            )


        )
    }


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

        val granted = grantResults.all { it == PackageManager.PERMISSION_GRANTED }

        val intent = Intent("PERMISSION_RESULT_ACTION").apply {
            putExtra("PERMISSION_GRANTED", granted)
        }
        sendBroadcast(intent)

        // Log the request code and permission results
        Log.d("PermissionsResult", "Request Code: $requestCode")
        Log.d("PermissionsResult", "Permissions: ${permissions.joinToString()}")
        Log.d("PermissionsResult", "Grant Results: ${grantResults.joinToString()}")

    }


}

