package com.ejada.hmg

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.Nullable
import androidx.annotation.RequiresApi
import com.ejada.hmg.penguin.PenguinInPlatformBridge
import com.ejada.hmg.watch.huawei.HuaweiWatch
import com.ejada.hmg.watch.huawei.samsung_watch.SamsungWatch
import com.huawei.hms.hihealth.result.HealthKitAuthResult
import com.huawei.hms.support.api.entity.auth.Scope
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant


class MainActivity: FlutterFragmentActivity() {

    private var huaweiWatch : HuaweiWatch? = null
    @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)

        PenguinInPlatformBridge(flutterEngine, this).create()
        SamsungWatch(flutterEngine, this)
        huaweiWatch = HuaweiWatch(flutterEngine, this)
    }

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

//    override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//
//        // Process only the response result of the authorization process.
//        if (requestCode == 1002) {
//            // Obtain the authorization response result from the intent.
//            val result: HealthKitAuthResult? = huaweiWatch?.mSettingController?.parseHealthKitAuthResultFromIntent(data)
//            if (result == null) {
//                Log.w(huaweiWatch?.TAG, "authorization fail")
//                return
//            }
//
//            if (result.isSuccess) {
//                Log.i(huaweiWatch?.TAG, "authorization success")
//                 if (result.getAuthAccount() != null && result.authAccount.authorizedScopes != null) {
//                    val authorizedScopes: MutableSet<Scope?> = result.authAccount.authorizedScopes
//                     if(authorizedScopes.isNotEmpty()) {
//                         huaweiWatch?.getHealthAppAuthorization()
//                     }
//                }
//            } else {
//                Log.w("MainActivty", "authorization fail, errorCode:" + result.getErrorCode())
//            }
//        }
//    }
}
