package com.abbidot.tracker.ui.fragment.map.googlemap

import android.content.Context
import androidx.fragment.app.viewModels
import com.abbidot.tracker.bean.WiFiZoneBean
import com.abbidot.tracker.vm.GeoCoderViewModel
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.Marker

/**
 *Created  by .yzq on 2024/6/4/004.
 * @link
 * @description:
 */
class EditWiFiZoneGoogleMapFragment(

) : BaseGoogleMapFragment() {

    private val mGeoCoderViewModel: GeoCoderViewModel by viewModels()

    private lateinit var mMapLoadOk: () -> Unit
    private lateinit var mWiFiZoneBean: WiFiZoneBean

    companion object {
        @JvmStatic
        fun newInstance(context: Context, wiFiZoneBean: WiFiZoneBean, mapLoadOk: () -> Unit) =
            EditWiFiZoneGoogleMapFragment().apply {
                mContext = context
                mMapLoadOk = mapLoadOk
                mWiFiZoneBean = wiFiZoneBean
            }
    }

    override fun onMapLoadOk(googleMap: GoogleMap) {

        googleMap.setOnMarkerDragListener(object : GoogleMap.OnMarkerDragListener {
            override fun onMarkerDrag(marker: Marker) {

            }

            override fun onMarkerDragEnd(marker: Marker) {
                marker.position.let {
                    mGeoCoderViewModel.getLatLonAddress(it.latitude, it.longitude, false)
                    mWiFiZoneBean.latitude = it.latitude
                    mWiFiZoneBean.longitude = it.longitude
                }
            }

            override fun onMarkerDragStart(marker: Marker) {

            }
        })

        getLastLocation()

        //变量是否初始化
        if (::mMapLoadOk.isInitialized) mMapLoadOk()
    }

    override fun liveDataObserve() {
        mGeoCoderViewModel.mLatLonAddressLiveData.observe(viewLifecycleOwner) {
            mWiFiZoneBean.address = it
        }
    }

}