import 'dart:io';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

/*
 * 地图App的跳转，根据launchUrl的方式跳转到对应App导航页面
 */
class MapNavigatorUtil {

  /// 苹果地图
  static Future<bool> gotoAppleMapByKeywords({keywords,longitude, latitude ,required VoidCallback toInstallCallBack})  {
    var url = 'http://maps.apple.com/?&daddr=${Uri.encodeComponent(keywords)}';

   return gotoMap(url: url, toInstallCallBack: () {
      if (null != toInstallCallBack) {
        toInstallCallBack();
      }
    });

    // bool canLaunchUrl = await canLaunch(url);
    // if (!canLaunchUrl) {
    //   gotoAppStore();
    //   return false;
    // }
    // await launch(url);
    //
    // return canLaunchUrl;
  }

  /// 苹果地图
  // static Future<bool> gotoAppStore()  {
  //   var url = 'itms-apps://itunes.apple.com/cn/app/id';
  //   bool canLaunchUrl = await canLaunch(url);
  //   if (!canLaunchUrl) {
  //     Fluttertoast.showToast(msg: '打开失败~', gravity: ToastGravity.CENTER);
  //     return false;
  //   }
  //   await launch(url);
  // }

  /// 苹果地图
  static Future<bool> gotoAppleMap({longitude, latitude ,required VoidCallback toInstallCallBack})  {
    var url = 'http://maps.apple.com/?&daddr=$latitude,$longitude';

    return gotoMap(url: url, toInstallCallBack: () {
      if (null != toInstallCallBack) {
        toInstallCallBack();
      }
    });

    // bool canLaunchUrl = await canLaunch(url);
    // if (!canLaunchUrl) {
    //   Fluttertoast.showToast(msg: '打开失败~', gravity: ToastGravity.CENTER);
    //   return false;
    // }
    // await launch(url);
  }

  /// 高德地图导航
  static Future<bool> gotoAMap(
      {longitude, latitude, address, required VoidCallback toInstallCallBack}) {
    var url =
        // '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';
        '${Platform.isAndroid ? 'android' : 'ios'}amap://viewMap?sourceApplication=amap&poiname=${address}&lat=$latitude&lon=$longitude&dev=0';
        // '${Platform.isAndroid ? 'android' : 'ios'}amap://viewReGeo?sourceApplication=softname&lat=$latitude&lon=$longitude&dev=0';

    return gotoMap(
        url: url,
        toInstallCallBack: () {
          if (null != toInstallCallBack) {
            toInstallCallBack();
          }
        });
  }

  /// 腾讯地图导航
  static Future<bool> gotoTencentMap(
      {longitude, latitude,address, required VoidCallback toInstallCallBack}) async {
    var url =
    //OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
        // 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
        // 'qqmap://map/marker?marker=coord:$latitude,$longitude;title:marker;addr:汉口火车站&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77';
        'qqmap://map/marker?marker=coord:$latitude,$longitude;title:${address};addr:${address}&referer=BVWBZ-DBB3J-NZGFF-XUKHP-DTDQK-2BFLN';
        // 'qqmap://map/geocoder?coord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';

    return gotoMap(
        url: url,
        toInstallCallBack: () {
          if (null != toInstallCallBack) {
            toInstallCallBack();
          }
        });
  }

  /// 百度地图导航
  static Future<bool> gotoBaiduMap(
      {longitude, latitude,address, required VoidCallback toInstallCallBack}) async {
    var url =
        // 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=gcj02&mode=driving';
        // 'baidumap://map/geocoder?location=$latitude,$longitude&coord_type=gcj02&src=andr.baidu.openAPIdemo';
        // 'baidumap://map/geocoder?&src=andr.baidu.openAPIdemo&address=湖北省武汉市汉口站';
        'baidumap://map/marker?location=$latitude,$longitude&title=${address}&coord_type=gcj02&src=${Platform.isAndroid ? 'andr' : 'ios'}.baidu.openAPIdemo';
        // 'baidumap://map/marker?location=$latitude,$longitude&coord_type=gcj02&src=${Platform.isAndroid ? 'andr' : 'ios'}.baidu.openAPIdemo';
    return gotoMap(
        url: url,
        toInstallCallBack: () {
          if (null != toInstallCallBack) {
            toInstallCallBack();
          }
        });
  }

  /// 跳转到第三方地图
  /// [url]跳转地址
  /// [toInstallCallBack]地图未安装回调
  static Future<bool> gotoMap(
      {required String url, required VoidCallback toInstallCallBack}) async {
    bool canLaunchUrl = await isMapInstall(url);

    if (!canLaunchUrl) {
      if (null != toInstallCallBack) {
        toInstallCallBack();
      }

      return false;
    }

    await launch(url);

    return true;
  }

  static void toInstallMap(String url) {
    launch(url);
  }

  static Future<bool> isBaiduMapInstall() {
    return canLaunch('baidumap://map/direction');
  }

  static Future<bool> isTencentMapInstall() {
    return canLaunch('qqmap://map/routeplan');
  }

  static Future<bool> isAmapMapInstall() {
    return canLaunch('${Platform.isAndroid ? 'android' : 'ios'}amap://navi');
  }

  static Future<bool> isIOSAmapMapInstall() async {

    // var url = 'http://maps.apple.com/?&daddr=$latitude,$longitude';
    var url = 'http://maps.apple.com/?&daddr=30.617915,114.255173';
    bool canLaunchUrl = await canLaunch(url);

    return canLaunchUrl;
  }

  /// 判断地图是否有安装
  static Future<bool> isMapInstall(String url) {
    return canLaunch(url);
  }
}
