package com.base.locationsharewhite.map

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.location.Criteria
import android.location.Location
import android.location.LocationManager
import android.view.View
import com.base.locationsharewhite.utils.BitmapUtils.getBitmapDescriptorFromLayoutResId
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.model.PolylineOptions
import kotlin.math.abs


object MapUtils {


    @SuppressLint("MissingPermission")
    fun Context.getLastKnowLatLng(): LatLng {
        val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager?
        if (locationManager != null) {
            val provider: String? = locationManager.getBestProvider(Criteria(), true)

            if (provider != null) {
                val lastKnownLocation: Location? = locationManager.getLastKnownLocation(provider)
                val latitude: Double = lastKnownLocation?.latitude ?: 0.0//维度
                val longitude: Double = lastKnownLocation?.longitude ?: 0.0//经度
                return LatLng(latitude, longitude)
            }
        }

        return LatLng(0.0, 0.0)
    }

    /**
     * 添加view为Marker
     */
    fun GoogleMap.addLocationMarker(
        marker: View,
        latLng: LatLng,
        tittle: String = "",
    ): Marker? {
        val bitmapDescriptor = getBitmapDescriptorFromLayoutResId(marker)
        return addMarker(
            MarkerOptions()
                .icon(bitmapDescriptor)
                .position(latLng)
                .title(tittle)
        )
    }

    fun GoogleMap.cameraMovePointsBounds(point1: LatLng, point2: LatLng, padding: Int) {
        // 创建包含两个点的边界
        val bounds = LatLngBounds.Builder().include(point1).include(point2).build()
        moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding))
    }


}