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

import android.content.Context
import com.abbidot.tracker.R
import com.abbidot.tracker.deprecated.ui.activity.map.MapLiveActivity
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.model.LatLng

/**
 *Created  by .yzq on 2022/6/28/028.
 * @link
 * @description:
 */
class MapLiveBaiduMapFragment : BaseBaiduMapFragment() {

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

    private lateinit var mMapLiveActivity: MapLiveActivity

    //已经转换好的经纬度坐标
    private lateinit var mLatLngList: MutableList<LatLng>

    //已经转换好的经纬度坐标
    private var mPetCurrentLatLng: LatLng? = null

    override fun initData() {
        mLatLngList = mutableListOf()
        mMapLiveActivity = mActivity as MapLiveActivity
        super.initData()
    }

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

    override fun onMapLoadOk(baiduMap: BaiduMap) {

//        initFenceList()

        //先开始获取围栏数据
        mMapLiveActivity.mFencesManageViewModel.getFenceList(
            mMapLiveActivity, mMapLiveActivity.mPet.deviceId
        )
    }

    /**
     * 刷新当前宠物位置
     */
    fun refreshPetCurrentLocation() {
        mPetCurrentLatLng?.let {
            refreshPetCurrentLocation(it.latitude, it.longitude, needConvertGCJ02 = false)
        }
    }

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