import 'dart:io';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher_string.dart';
import '../../tool/YSNetWork.dart';
import '../../tool/YSTools.dart';

class YSChooseMapView extends StatelessWidget {
  final String latitude;
  final String longitude;
  const YSChooseMapView({Key? key, required this.latitude, required this.longitude}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: hsp(220),
      width: MediaQuery.of(context).size.width,
      decoration: const BoxDecoration(
        color: Color(0xFFF1F2F3),
        borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10))
      ),
      child: ListView.separated(
          itemBuilder: (context,index){
            return GestureDetector(
              onTap: (){
                if(index==0){
                  XMapNavigatorUtil.gotoAMap(latitude: latitude,longitude: longitude,toInstallCallBack: (){
                    ysFlutterToast('高德地图未安装');
                    // launchUrlString('https://wap.amap.com/?type=pcm&from=banner&src=pc');
                  });
                }else if(index==1){
                  XMapNavigatorUtil.gotoTencentMap(latitude: latitude,longitude: longitude,toInstallCallBack: (){
                    ysFlutterToast( '腾讯地图未安装');
                    // launchUrlString('https://map.qq.com/mobile/');
                  });
                }else if(index==2){
                  XMapNavigatorUtil.gotoBaiduMap(latitude: latitude,longitude: longitude,toInstallCallBack: (){
                    ysFlutterToast('百度地图未安装');
                    // launchUrlString('https://map.baidu.com/mapclient-pages/download/?from=pcmap&source=index');
                  });
                }else{
                  Navigator.pop(context);
                }
              },
              behavior: HitTestBehavior.opaque,
              child: Container(
                height: hsp(49),
                color: Colors.white,
                alignment: Alignment.center,
                child: Text(index==0?'高德地图':index==1?'腾讯地图':index==2?'百度地图':'取消'),
              ),
            );
          },
          separatorBuilder: (context,index){
            return Container(height: index==2?hsp(21):hsp(1),);
          },
          itemCount: 4
      ),
    );
  }
}

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

  /// 腾讯地图导航
  static Future<bool> gotoTencentMap(
      {longitude, latitude, required VoidCallback toInstallCallBack}) async {
    var url = 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';

    return gotoMap(
        url: url,
        toInstallCallBack: () {
          toInstallCallBack();
        });
  }

  /// 百度地图导航
  static Future<bool> gotoBaiduMap(
      {longitude, latitude, required VoidCallback toInstallCallBack}) async {
    var url = 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=gcj02&mode=driving';
    return gotoMap(
        url: url,
        toInstallCallBack: () {
          toInstallCallBack();
        });
  }

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

    if (!canLaunchUrl) {
      toInstallCallBack();

      return false;
    }
    try{
      await launchUrlString(url);
      return true;
    }catch(e){
      toInstallCallBack();
      return false;
    }

  }

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

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

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

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

  /// 判断地图是否有安装
  static Future<bool> isMapInstall(String url) {
    if(url.contains('amap')){
      return isAmapMapInstall();
    }else if(url.contains('qqmap')){
      return isBaiduMapInstall();
    }else if(url.contains('baidumap')){
      return isBaiduMapInstall();
    }
    return canLaunchUrlString(url);
  }
}