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

import android.content.Context
import androidx.fragment.app.viewModels
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.bean.MapDeviceBean
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.vm.GeoCoderViewModel
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.model.LatLng


/**
 *Created  by .yzq on 2022/6/24/024.
 * @link
 * @description:
 */
class HomeMapBaiduMapFragment : BaseBaiduMapFragment() {

    private val mGeoCoderViewModel: GeoCoderViewModel by viewModels()

    //宠物当前位置信息
    var mDecPetAddressData: HistoryDataBean? = null
    private var mMapDeviceBean: MapDeviceBean? = null
    private lateinit var mMapLoadOk :() -> Unit

    //启动动画移动地图摄像机
    private var isMoveCamera = true

    //已经转换好的经纬度坐标
    private val mLatLngList = mutableListOf<LatLng>()

    /**
     *是否显示信息窗（弹窗覆盖物InfoWindow）
     */
    private var isShowInfoWindow = false

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

    fun setMapDeviceBean(mapDeviceBean: MapDeviceBean?) {
        mMapDeviceBean = mapDeviceBean
    }

    override fun liveDataObserve() {
        //反地理编码成功返回
        mGeoCoderViewModel.mLatLonAddressLiveData.observe(viewLifecycleOwner) {
            //保存地理信息,方便传参给导航页面
            if (null == mDecPetAddressData) mDecPetAddressData = HistoryDataBean()

            var timeString = ""
            mMapDeviceBean?.apply {
//                timeString = String.format(
//                    getString(R.string.map_current_update_time),
//                    DateUtils.getRelativeTimeSpanString(updateTime * 1000)
//                )
                timeString = Utils.formatTime(updateTime * 1000, Utils.DATE_FORMAT_PATTERN_EN6)
            }

            mDecPetAddressData?.apply {
                //更新位置时间
                dayTime = timeString
                //详细位置
                address = it
            }
            showMarkerInfoWindow(mDecPetAddressData!!)
        }
    }

    override fun onMapLoadOk(baiduMap: BaiduMap) {
        //开启地图的定位图层
//        baiduMap.isMyLocationEnabled = true
        startLocation(60, false)
//        val locationConfiguration =
//            MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, null)
//        //自定义精度圈边框颜色
//        baiduMap.setMyLocationConfiguration(locationConfiguration)

//        initFenceList()

//        mBaiduMap?.setOnMarkerClickListener {
//            if (!isShowInfoWindow) {
//                startReverseGeocode()
//            } else {
//                baiduMap.hideInfoWindow()
//            }
//            isShowInfoWindow = !isShowInfoWindow
//            false
//        }

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


    /**
     * 画线
     */
    fun addLine(lat: Double, lon: Double) {
        val convertLonLat = getBaiduMapLatLng(lat, lon)
        mLatLngList.add(convertLonLat)
        addPolyline(mBaiduMap!!, mLatLngList, startCapImageRes = R.drawable.map_live_start)
        refreshPetCurrentLocation(
            convertLonLat,
            needMoveCamera = isMoveCamera,
        )
        isMoveCamera = false
    }

    /**
     * 计算用户和宠物的距离
     */
    fun getUserAndPetDistance(): Double {
        var distance = 0.0
        mUserLatLng?.apply {
            mPetLatLng?.let {
                distance =
                    Util.measureLatLonDistance(latitude, longitude, it.latitude, it.longitude)
            }
        }
        return distance
    }

    /**
     * 刚开始的反编译地理位置
     */
    private fun startReverseGeocode() {
        mMapDeviceBean?.apply {
            mGeoCoderViewModel.getLatLonAddress(latitude, longitude, false)
        }
    }


    override fun onRefreshPetCurrentLocationOk(baiduMap: BaiduMap) {
//        if (isShowInfoWindow) {
//            isShowInfoWindow = false
//            baiduMap.hideInfoWindow()
//        }
        startReverseGeocode()
    }
}