package com.abbidot.tracker.deprecated.ui.fragment.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.constant.KeyNames
import com.abbidot.tracker.util.Util
import com.baidu.mapapi.search.route.BikingRouteResult
import com.baidu.mapapi.search.route.DrivingRouteResult
import com.baidu.mapapi.search.route.IndoorRouteResult
import com.baidu.mapapi.search.route.IntegralRouteResult
import com.baidu.mapapi.search.route.MassTransitRouteResult
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener
import com.baidu.mapapi.search.route.PlanNode
import com.baidu.mapapi.search.route.RoutePlanSearch
import com.baidu.mapapi.search.route.TransitRouteResult
import com.baidu.mapapi.search.route.WalkingRouteLine
import com.baidu.mapapi.search.route.WalkingRoutePlanOption
import com.baidu.mapapi.search.route.WalkingRouteResult
import com.google.android.gms.maps.model.LatLng
import com.mapbox.api.directions.v5.DirectionsCriteria
import com.mapbox.api.directions.v5.MapboxDirections
import com.mapbox.api.directions.v5.models.DirectionsResponse
import com.mapbox.api.directions.v5.models.DirectionsRoute
import com.mapbox.api.directions.v5.models.RouteOptions
import com.mapbox.geojson.Point
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


/**
 *Created  by .yzq on 2022/4/13/013.
 * @link
 * @description:获取路线规划
 */
class RoutePlanningViewModel : ViewModel() {

    //mapbox步行路线规划
    val mRouteMapBoxLiveData = MutableLiveData<DirectionsRoute?>()

    //百度地图步行路线规划
    private var mRoutePlanSearch: RoutePlanSearch? = null
    val mRouteBaiduMapLiveData = MutableLiveData<WalkingRouteLine?>()


    /**
     * 获取mapbox路线规划信息
     */
    fun getMapBoxRoutePlan(activity: BaseActivity<*>, startLng: LatLng, endLng: LatLng) {
        activity.showLoading(true)
        val coordinates = mutableListOf<Point>()
        coordinates.add(Point.fromLngLat(startLng.longitude, startLng.latitude))
        coordinates.add(Point.fromLngLat(endLng.longitude, endLng.latitude))

        val routeOptions = RouteOptions.builder().profile(DirectionsCriteria.PROFILE_WALKING)
            .coordinatesList(coordinates).build()
        val mapboxAccessToken =
            Util.getMetadata(activity.applicationContext, KeyNames.MAPBOX_ACCESS_TOKEN)
        val client =
            MapboxDirections.builder().accessToken(mapboxAccessToken).routeOptions(routeOptions)
                .build()

        client.enqueueCall(object : Callback<DirectionsResponse?> {
            override fun onResponse(
                call: Call<DirectionsResponse?>, response: Response<DirectionsResponse?>
            ) {
                activity.showLoading(false)
                LogUtil.d("Response code: " + response.code())

                val result = response.body()
                if (result == null) {
                    LogUtil.e(
                        "RoutePlanningViewModel",
                        "No routes found, make sure you set the right user and access token."
                    )
                    mRouteMapBoxLiveData.value = null
                    return
                } else if (result.routes().size < 1) {
                    LogUtil.e("RoutePlanningViewModel", "No routes found")
                    mRouteMapBoxLiveData.value = null
                    return
                }
                val route = result.routes()[0]
                LogUtil.e(route.toString())
                mRouteMapBoxLiveData.value = route
                client.cancelCall()
            }

            override fun onFailure(call: Call<DirectionsResponse?>, t: Throwable) {
                activity.showLoading(false)
                LogUtil.e("RoutePlanningViewModel", "Error: " + t.message)
                mRouteMapBoxLiveData.value = null
                client.cancelCall()
            }
        })
    }

    /**
     * 百度地图步行路线规划,经纬度不用再转化
     */
    fun getBaiduMapRoutePlan(
        activity: BaseActivity<*>,
        startLng: com.baidu.mapapi.model.LatLng,
        endLng: com.baidu.mapapi.model.LatLng
    ) {
        activity.showLoading(true)
        if (null == mRoutePlanSearch) {
            mRoutePlanSearch = RoutePlanSearch.newInstance()
            mRoutePlanSearch!!.setOnGetRoutePlanResultListener(object :
                OnGetRoutePlanResultListener {
                override fun onGetWalkingRouteResult(walkingRouteResult: WalkingRouteResult?) {
                    activity.showLoading(false)
                    if (null != walkingRouteResult) {
                        try {
                            //routeLines=null的情況
                            walkingRouteResult.routeLines.size
                        } catch (e: Exception) {
                            walkingRouteResult.routeLines = mutableListOf<WalkingRouteLine>()
                        }
                        if (walkingRouteResult.routeLines.size > 0) {
                            //获取路径规划数据,(以返回的第一条数据为例)
                            val result = walkingRouteResult.routeLines[0]
                            mRouteBaiduMapLiveData.value = result
                        } else {
                            mRouteBaiduMapLiveData.value = null
                        }
                    } else {
                        mRouteBaiduMapLiveData.value = null
                    }
                }

                override fun onGetTransitRouteResult(p0: TransitRouteResult?) {
                }

                override fun onGetMassTransitRouteResult(p0: MassTransitRouteResult?) {
                }

                override fun onGetDrivingRouteResult(p0: DrivingRouteResult?) {
                }

                override fun onGetIndoorRouteResult(p0: IndoorRouteResult?) {
                }

                override fun onGetBikingRouteResult(p0: BikingRouteResult?) {
                }

                override fun onGetIntegralRouteResult(p0: IntegralRouteResult?) {

                }
            })
        }

        val stNode = PlanNode.withLocation(startLng)
        val enNode = PlanNode.withLocation(endLng)
        mRoutePlanSearch?.walkingSearch(WalkingRoutePlanOption().from(stNode).to(enNode))
    }

    override fun onCleared() {
        super.onCleared()
        mRoutePlanSearch?.destroy()
    }
}