package com.za.common.util

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import com.amap.api.maps.model.LatLng
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.ln
import kotlin.math.sin
import kotlin.math.sqrt

object MapUtil {

    const val PN_GAODE_MAP: String = "com.autonavi.minimap" // 高德地图包名
    const val PN_BAIDU_MAP: String = "com.baidu.BaiduMap" // 百度地图包名
    const val PN_TENCENT_MAP: String = "com.tencent.map" // 百度地图包名

    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
     * 即谷歌、高德 转 百度
     *
     * @param latLng
     * @returns
     */
    fun gcj02ToBD09(latLng: LatLng): LatLng {
        val xPI = 3.141592653589793 * 3000.0 / 180.0
        val z = sqrt(latLng.longitude * latLng.longitude + latLng.latitude * latLng.latitude) + 0.00002 * sin(latLng.latitude * xPI)
        val theta = atan2(latLng.latitude, latLng.longitude) + 0.000003 * cos(latLng.longitude * xPI)
        val bdLat = z * sin(theta) + 0.006
        val bdLng = z * cos(theta) + 0.0065
        return LatLng(bdLat, bdLng)
    }

    /**
     * 检查应用是否安装
     *
     * @return
     */
    fun isGdMapInstalled(context: Context): Boolean {
        return isInstallPackage(context, PN_GAODE_MAP)
    }

    fun isBaiduMapInstalled(context: Context): Boolean {
        return isInstallPackage(context, PN_BAIDU_MAP)
    }

    fun isTencentInstalled(context: Context): Boolean {
        return isInstallPackage(context, PN_TENCENT_MAP)
    }

    private fun isInstallPackage(context: Context, packageName: String): Boolean {
        try {
            val info = context.packageManager
                .getApplicationInfo(packageName,
                    PackageManager.GET_UNINSTALLED_PACKAGES)
            return true
        } catch (e: PackageManager.NameNotFoundException) {
            return false
        }
    }


    //开启高德导航
    fun startNavigationGd(context: Context, lat: Double?, lng: Double?, address: String?) {
        val stringBuffer = "androidamap://route?sourceApplication=" + "amap" +
                "&dlat=" + lat +
                "&dlon=" + lng +
                "&dname=" + address +
                "&dev=" + 0 +
                "&t=" + 0
        val intent = Intent("android.intent.action.VIEW", Uri.parse(stringBuffer))
        intent.setPackage("com.autonavi.minimap")
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }

    //开启高德导航
    fun startNavigationBd(context: Context, lat: Double?, lng: Double?, address: String?) {
        val i1 = Intent("android.intent.action.VIEW")
        i1.setData(Uri.parse("baidumap://map/navi?query=${address}&mode=driving&location=${lat},${lng}&coord_type=gcj02&src=com.za.servicing"))
        context.startActivity(i1)
    }

    //开启高德导航
    fun startNavigationTencent(context: Context, lat: Double?, lng: Double?, address: String?) {
        val stringBuffer = "androidamap://route?sourceApplication=" + "amap" +
                "&dlat=" + lat +
                "&dlon=" + lng +
                "&dname=" + address +
                "&dev=" + 0 +
                "&t=" + 0
        val intent = Intent("android.intent.action.VIEW", Uri.parse(stringBuffer))
        intent.setPackage("com.autonavi.minimap")
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }


}