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

import android.content.Context
import android.location.Location
import androidx.fragment.app.viewModels
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.vm.GeoCoderViewModel
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker

/**
 *Created  by .yzq on 2024/6/4/004.
 * @link
 * @description:
 */
class WiFiZone2GoogleMapFragment : BaseGoogleMapFragment() {

    private val mGeoCoderViewModel: GeoCoderViewModel by viewModels()

    //wifi zone当前位置信息  //保存地理信息,方便传参给其他页面
    var mDecWifiAddressData: HistoryDataBean? = null

    companion object {
        @JvmStatic
        fun newInstance(context: Context) = WiFiZone2GoogleMapFragment().apply {
            mContext = context
        }
    }

    override fun onMapLoadOk(googleMap: GoogleMap) {

        mDecWifiAddressData = HistoryDataBean()

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

                }

                override fun onMarkerDragEnd(marker: Marker) {
                    marker.position.let {
                        geoCoderLatLon(it.latitude, it.longitude)
                    }
                }

                override fun onMarkerDragStart(marker: Marker) {
                }
            })

            setOnMapLongClickListener { latLng ->
                mMarker?.position = latLng
                geoCoderLatLon(latLng.latitude, latLng.longitude)
            }

            //点击地图任意地方都不消失自定义头部地点布局
            setOnMapClickListener {
                mMarker?.showInfoWindow()
            }
        }
        getLastLocation()
    }

    override fun liveDataObserve() {
        //反地理编码成功返回
        mGeoCoderViewModel.mLatLonAddressLiveData.observe(viewLifecycleOwner) {
            mMarker?.apply {
                mDecWifiAddressData?.let { history ->
                    //更新位置时间
                    //                    history.dayTime = timeString
                    //详细位置
                    history.address = it
                    //传递位置信息给InfoWindowAdapter窗口
                    showMarkerInfoWindow(history)
                }
            }
        }
    }

    private fun geoCoderLatLon(latitude: Double, longitude: Double) {
        mDecWifiAddressData?.apply {
            this.latitude = latitude
            this.longitude = longitude
        }
        mGeoCoderViewModel.getLatLonAddress(latitude, longitude, false)
    }

    override fun locationSuccess(location: Location?) {
        location?.apply {
            val latLng = LatLng(latitude, longitude)
            moveCameraLocation(latLng, true)
            if (null == mMarker) {
                mMarker = addImageMarker(
                    latLng, R.drawable.icon_location_svg, anchorCenter = false, isDraggable = true
                )
            } else mMarker?.position = latLng
            geoCoderLatLon(latitude, longitude)
        }
    }
}