package com.ejada.hmg
import android.app.PendingIntent
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import android.view.WindowManager
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi
import com.cloud.hmg_patient_app.PenguinInPlatformBridge
import com.cloud.hmg_patient_app.whatsapp.AppSignatureRetriever
import com.ejada.hmg.utils.*
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
import com.cloud.hmg_patient_app.whatsapp.WhatsApp
import com.cloud.hmg_patient_app.whatsapp.WhatsAppOtpPlatformBridge


class MainActivity: FlutterFragmentActivity() {
    @RequiresApi(Build.VERSION_CODES.O)
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        // Create Flutter Platform Bridge
        this.window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON)

        PlatformBridge(flutterEngine, this).create()
        OpenTokPlatformBridge(flutterEngine, this).create()
        PenguinInPlatformBridge(flutterEngine, this).create()
        WhatsAppOtpPlatformBridge(flutterEngine, this).invoke()
        AppSignatureRetriever().logSignatures(this)
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {x
//            val mChannel = NotificationChannel("video_call_noti", "video call", NotificationManager.IMPORTANCE_HIGH)
//            val soundUri = Uri.parse("android.resource://" + getApplicationContext()
//                    .getPackageName() + "/" + R.raw.alert)
//            System.out.println("soundUri");
//            System.out.println("soundUri: $soundUri");
//            System.out.println("soundUri : ${soundUri.path}");
//            val att = AudioAttributes.Builder()
//                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
//                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
//                    .build();
//            mChannel.setSound(soundUri , att)
//            mChannel.description = "Video Call Notifications"
//            val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
//            notificationManager.createNotificationChannel(mChannel)
//        }

//        val time = timeToMillis("04:00:00", "HH:mm:ss")

    }
    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()}")

    }

    override fun onResume() {
        super.onResume()
    }
}