package com.isidroid.ui.maps.ext

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.LatLngBounds
import com.google.maps.android.SphericalUtil
import kotlin.math.roundToInt

internal fun findMapRadius(latLngBounds: LatLngBounds?): Int {
    latLngBounds ?: return 0

    val northEast = latLngBounds.northeast
    val southWest = latLngBounds.southwest

    return SphericalUtil.computeDistanceBetween(northEast, southWest).roundToInt()
}

@Composable
internal fun rememberMapViewWithLifecycle(): MapView {
    val context = LocalContext.current
    val mapView = remember { MapView(context) }

    DisposableEffect(Unit) {
        mapView.onCreate(null)
        mapView.onResume()
        onDispose { mapView.onDestroy() }
    }

    return mapView
}