package com.cheng.blzb.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.example.base.extensions.toast
import com.example.base.utils.AppUtils
import com.example.base.utils.Utils

object NaviUtils {

    fun goAMapNavi(context: Context, address: String, lng: String, lat: String) {
        if (isAMapInstall()) {
            val uri = "amapuri://route/plan/?did=&dlat=${lat}&dlon=${lng}&dname=${address}&dev=0&t=1"
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
            context.startActivity(intent)
        } else {
            toast("请安装高德地图后再试")
        }
    }

    fun goTxMapNavi(context: Context, address: String, lng: String, lat: String) {
        if (isTxMapInstall()) {
            val uri = "qqmap://map/routeplan?type=bus&to=${address}&tocoord=${lat},${lng}&referer=${AppUtils.getAppName()}"
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
            context.startActivity(intent)
        } else {
            toast("请安装腾讯地图后再试")
        }
    }

    fun goBaiduMapNavi(context: Context, address: String, lng: String, lat: String) {
        if (isBaiduMapInstall()) {
            val uri = "baidumap://map/direction?destination=name:${address}|latlng:${lat},${lng}&coord_type=gcj02&mode=transit&sy=0&src=${context.packageName}"
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
            context.startActivity(intent)
        } else {
            toast("请安装百度地图后再试")
        }
    }

    private fun isAMapInstall(): Boolean {
        return try {
            Utils.getApp().packageManager.getPackageInfo("com.autonavi.minimap", 0)
            true
        } catch (e: Exception) {
            false
        }
    }

    private fun isBaiduMapInstall(): Boolean {
        return try {
            Utils.getApp().packageManager.getPackageInfo("com.baidu.BaiduMap", 0)
            true
        } catch (e: Exception) {
            false
        }
    }

    private fun isTxMapInstall(): Boolean {
        return try {
            Utils.getApp().packageManager.getPackageInfo("com.tencent.map", 0)
            true
        } catch (e: Exception) {
            false
        }
    }
}