package com.isidroid.ui.maps

//  val context = LocalContext.current
//    val lifecycleOwner = LocalLifecycleOwner.current
//
//    val stateFlow = remember { MutableStateFlow(LatLng(lat, lng)) }
//    var googleMapView by remember { mutableStateOf<GoogleMap?>(null) }
//    var clusterManager by remember { mutableStateOf<ClusterManager<MyClusterItem>?>(null) }
//
//
//    LaunchedEffect(stateFlow) {
//        stateFlow
//            .flowWithLifecycle(lifecycleOwner.lifecycle)
//            .distinctUntilChanged()
//            .debounce(1000L)
//            .collect { newLatLng ->
//                val radius = findMapRadius(googleMapView)
//                onCameraMove(newLatLng.latitude, newLatLng.longitude, radius)
//            }
//    }
//
//    MapCreatorComponent(
//        modifier = modifier,
//        stateFlow = stateFlow,
//        onMapViewReady = { map, manager ->
//            googleMapView = map
//            clusterManager = manager
//
//            Timber.i("sdfsdfsdf MapCreatorComponent.onMapViewReady")
//
////            map.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(lat, lng), 16f))
//        }
//    )
//
//    HandleCallbacks(mapMarkersStateFlow, clusterManager)
//
//    LaunchedEffect(stateFlow) {
//        stateFlow
//            .flowWithLifecycle(lifecycleOwner.lifecycle)
//            .distinctUntilChanged()
//            .debounce(1000L)
//            .collect { newLatLng ->
//                val radius = findMapRadius(googleMapView)
//                onCameraMove(newLatLng.latitude, newLatLng.longitude, radius)
//            }
//    }
//}
//
//@Composable
//private fun HandleCallbacks(mapMarkersStateFlow: StateFlow<List<MapMarker>>, clusterManager: ClusterManager<MyClusterItem>?) {
////    val coroutineScope = rememberCoroutineScope()
//    val mapMarkers = mapMarkersStateFlow.collectAsState()
////
////    Timber.i("sdfsdfsdf mapMarkers=${mapMarkers.value.size}, clusterManager=$clusterManager")
//}
//
//@Composable
//private fun MapCreatorComponent(
//    modifier: Modifier,
//    stateFlow: MutableStateFlow<LatLng>,
//    onMapViewReady: (GoogleMap, ClusterManager<MyClusterItem>) -> Unit
//) {
//    val mapView = rememberMapViewWithLifecycle()
//    val context = LocalContext.current
//    val coroutineScope = rememberCoroutineScope()
//    var isCreated by remember { mutableStateOf(false) }
//
//    Timber.i("sdfsdfsdf MapCreatorComponent create isCreated=$isCreated")
//
//    if(isCreated) return
//
//    AndroidView(
//        factory = { mapView },
//        modifier = modifier
//    ) {
//        mapView.getMapAsync { googleMap ->
//            Timber.i("sdfsdfsdf MapCreatorComponent.onMapViewReady")
//            isCreated = true
//
//
//            val clusterManager = ClusterManager<MyClusterItem>(context, googleMap)
//            onMapViewReady(googleMap, clusterManager)
//
//            googleMap.setOnCameraIdleListener(clusterManager)
//            googleMap.setOnMarkerClickListener(clusterManager)
//
//            clusterManager.setOnClusterClickListener { clusterItem ->
//                true
//            }
//
//            clusterManager.setOnClusterItemClickListener { clusterItem -> true }
//            googleMap.setOnCameraMoveListener {
//                val centerLocation = googleMap.cameraPosition.target
//                coroutineScope.launch { stateFlow.emit(centerLocation) }
//            }
//        }
//    }
//}
//
//
////private fun addClusteredMarkers(clusterManager: ClusterManager<MyClusterItem>) {
////    val items = listOf(
////        MyClusterItem(LatLng(40.7579247, -73.9881229), "Title 1", "Snippet 1"),
////        MyClusterItem(LatLng(40.7579347, -73.9881129), "Title 2", "Snippet 2"),
////        MyClusterItem(LatLng(40.7579347, -73.9851129), "Title 3", "Snippet 2")
////
////    )
////
////    clusterManager.clearItems()
////    clusterManager.addItems(items)
////    clusterManager.cluster()
////}