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

/**
 *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 {
//                        val newLatLng = toGpsLatLon(LatLng(it.latitude, it.longitude))
//                        geoCoderLatLon(newLatLng.latitude, newLatLng.longitude)
//                    }
//                }
//
//                override fun onMarkerDragStart(marker: Marker) {
//                }
//            })
//
//            setOnMapLongClickListener { latLng ->
//                mMarker?.position = latLng
//                val newLatLng = toGpsLatLon(latLng)
//                geoCoderLatLon(newLatLng.latitude, newLatLng.longitude)
//            }

            setOnCameraIdleListener {
                getCameraCenterLatLng(this, true)
            }
            setOnCameraMoveListener {
                getCameraCenterLatLng(this)
            }

            //点击地图任意地方都不消失自定义头部地点布局
            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 getCameraCenterLatLng(googleMap: GoogleMap, isNeedGeoCoder: Boolean = false) {
        googleMap.cameraPosition.let {
            mMarker?.position = it.target
            if (isNeedGeoCoder) {
                val newLatLng = toGpsLatLon(it.target)
                geoCoderLatLon(newLatLng.latitude, newLatLng.longitude)
            }
        }
    }

    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)
            if (null == mMarker) {
                mMarker = addImageMarker(
                    latLng, R.drawable.icon_location_svg, anchorCenter = false
                )
            } else mMarker?.position = latLng
            geoCoderLatLon(latitude, longitude)
        }
    }
}