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

import android.content.Context
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.deprecated.ui.activity.map.HomeMapNavigationActivity
import com.abbidot.tracker.deprecated.ui.fragment.vm.RoutePlanningViewModel
import com.abbidot.tracker.ui.fragment.map.baidumap.BaseBaiduMapFragment
import com.baidu.location.BDLocation
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.OverlayOptions
import com.baidu.mapapi.map.PolylineOptions
import com.baidu.mapapi.model.LatLng
import com.qmuiteam.qmui.util.QMUIDisplayHelper


/**
 *Created  by .yzq on 2022/6/27/027.
 * @link
 * @description:
 */
class NavigationBaiduMapFragment: BaseBaiduMapFragment() {

    private val mRoutePlanningViewModel: RoutePlanningViewModel by viewModels()

    private lateinit var mMapNavigationActivity: HomeMapNavigationActivity

    /**
     * 宠物经纬度 已经转换好的
     */
//    private lateinit var mPetLatLng: LatLng

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

    override fun initData() {
        mMapNavigationActivity = mActivity as HomeMapNavigationActivity
        super.initData()
    }

    override fun liveDataObserve() {
        mRoutePlanningViewModel.mRouteBaiduMapLiveData.observe(viewLifecycleOwner) {
            it?.apply {
                val distanceStr = Utils.formatDecimal(it.distance.toDouble(), 2)
                val minStr = Utils.formatDecimal((it.duration / 60).toDouble(), 2)
                mMapNavigationActivity.setDistanceData(distanceStr, minStr)
            }
        }
    }

    override fun onMapLoadOk(baiduMap: BaiduMap) {
//        initFenceList()

        //开启地图的定位图层
        baiduMap.isMyLocationEnabled = true

        mMapNavigationActivity.apply {
            //宠物经纬度
            val convertLonLat = getBaiduMapLatLng(mMapDeviceBean!!.latitude, mMapDeviceBean!!.longitude,true)

            mPetLatLng = convertLonLat

            //画围栏
            mFencesMapViewModel.setFencesData(
                mContext, mMapDeviceBean!!.fences, this@NavigationBaiduMapFragment
            )

            refreshPetCurrentLocation(
                mMapDeviceBean!!.latitude,
                mMapDeviceBean!!.longitude,
                isAnimMoveCamera = false,
                headBgResId = R.drawable.pic_map_location
            )

            startLocation()
        }
    }

    override fun locationSuccess(location: BDLocation?) {
        val locationLatLng = LatLng(location!!.latitude, location.longitude)
        val points = mutableListOf<LatLng>()
        points.add(locationLatLng)
        points.add(mPetLatLng!!)
        addNavigationLine(points)
    }

    /**
     * 画导航直线
     */
    private fun addNavigationLine(points: MutableList<LatLng>) {
        if (points.size < 1) return
        mRoutePlanningViewModel.getBaiduMapRoutePlan(mMapNavigationActivity, points[0], points[1])

        //设置折线的属性
        val mOverlayOptions: OverlayOptions = PolylineOptions().width(QMUIDisplayHelper.dpToPx(2))
            .color(ContextCompat.getColor(mContext!!, R.color.navigation_line_color))
            .dottedLine(true).points(points)
        mBaiduMap!!.addOverlay(mOverlayOptions)
    }

    override fun onDestroy() {
        mBaiduMap?.isMyLocationEnabled = false
        super.onDestroy()
    }


}