package com.gametime.watch

import android.content.Intent
import android.os.Bundle
import androidx.wear.ambient.AmbientModeSupport
import com.gametime.watch.bridge.WatchBridgePlugin
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine

class MainActivity :
    FlutterFragmentActivity(),
    AmbientModeSupport.AmbientCallbackProvider {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        AmbientModeSupport.attach(this)
        WatchBridgePlugin.attachActivity(this)
        WatchBridgePlugin.handleActivityReentry(this, intent)
    }

    override fun onResume() {
        super.onResume()
        WatchBridgePlugin.handleActivityReentry(this, intent)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        WatchBridgePlugin.handleActivityReentry(this, intent)
    }

    override fun onDestroy() {
        WatchBridgePlugin.detachActivity(this)
        super.onDestroy()
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        WatchBridgePlugin.register(flutterEngine, applicationContext)
    }

    override fun getAmbientCallback(): AmbientModeSupport.AmbientCallback {
        return object : AmbientModeSupport.AmbientCallback() {}
    }

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