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

import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import com.abbidot.baselibrary.util.AppUtils
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.googlemap.BaseGoogleMapFragment
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.*


/**
 *Created  by .yzq on 2022/1/13/013.
 * @link
 * @description:导航谷歌地图
 */
class NavigationGoogleMapFragment: BaseGoogleMapFragment() {

    private val mRoutePlanningViewModel: RoutePlanningViewModel by viewModels()

    private var mMyLocation: Location? = null

//    /**
//     * 宠物经纬度
//     */
//    private var mPetLatLng: LatLng? = null

    private lateinit var mMapNavigationActivity: HomeMapNavigationActivity

    /**
     * 导航画的线
     */
    private var mNavigationLine: Polyline? = null

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

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

    override fun liveDataObserve() {
        mRoutePlanningViewModel.mRouteMapBoxLiveData.observe(viewLifecycleOwner) {
            val distanceStr: String
            val minStr: String
            //如果返回为空，就大概算出
            if (null == it) {
                //计算2点的距离
                val distance =
                    com.abbidot.tracker.util.Util.measureLatLonDistance(mMyLocation!!.latitude,
                            mMyLocation!!.longitude, mPetLatLng!!.latitude, mPetLatLng!!.longitude)
                //计算步行需要多久 假设步行速度为1.3m/s 只计算大概数据
                val time = distance / 1.3

                distanceStr = Utils.formatDecimal(distance, 2)
                minStr = Utils.formatDecimal(time / 60, 2)
            } else {
                distanceStr = Utils.formatDecimal(it.distance(), 2)
                minStr = Utils.formatDecimal(it.duration() / 60, 2)
            }
            mMapNavigationActivity.setDistanceData(distanceStr, minStr)
        }
    }

    /**
     * 移动中心点
     */
    private fun moveLocation(location: Location) {
        moveCameraLocation(LatLng(location.latitude, location.longitude))
    }


    /**
     * 画导航直线
     */
    private fun addNavigationLine(startLng: LatLng, endLng: LatLng) {

        mRoutePlanningViewModel.getMapBoxRoutePlan(mMapNavigationActivity, startLng, endLng)

        mNavigationLine?.remove()

        mNavigationLine = mGoogleMap!!.addPolyline(
                PolylineOptions().clickable(true).add(startLng, endLng).width(AppUtils.dpToPx(2f))
                        .color(ContextCompat.getColor(mContext!!, R.color.navigation_line_color))
                        .geodesic(true))
        mNavigationLine!!.startCap = RoundCap()
        //连接类型,可以指定棱台或圆角
        mNavigationLine!!.jointType = JointType.ROUND
//        mNavigationLine!!.endCap =
//            CustomCap(BitmapDescriptorFactory.fromResource(R.drawable.pic_map_location),
//                    QMUIDisplayHelper.dpToPx(3).toFloat())

        //设置用户定位位置图标
        mGoogleMap!!.addMarker(MarkerOptions().position(startLng))

        //自定义描边图案
        val pattern = listOf(Dash(30F), Gap(20F))
        mNavigationLine!!.pattern = pattern
    }

    @SuppressLint("MissingPermission")
    override fun onMapLoadOk(googleMap: GoogleMap) {
//        initFenceList()
        //启用“我的位置”图层
        googleMap.apply {
            //显示“我的位置”按钮
            isMyLocationEnabled = true
            setOnMyLocationButtonClickListener {
                mMyLocation?.let {
                    moveLocation(it)
                }
                false
            }
        }

        setGoogleMapZoom(12.5f)

        mMapNavigationActivity.apply {
            mPetLatLng = LatLng(mMapDeviceBean!!.latitude, mMapDeviceBean!!.longitude)

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

            refreshPetCurrentLocation(mPetLatLng!!, isAnimMoveCamera = false,
                    headBgResId = R.drawable.pic_map_location)

            getLastLocation()
        }
    }

    override fun locationSuccess(location: Location?) {
        mMyLocation = location
        addNavigationLine(LatLng(location!!.latitude, location.longitude), mPetLatLng!!)
    }
}