import 'package:url_launcher/url_launcher.dart' as _url_launcher;
export 'package:guru_utils/uri/uri_utils.dart';

/// Created by Haoyi on 4/30/21

enum UriType { assets, file, http, unknown }

class UriUtils {
  static bool isAssetsPath(String path) {
    return path[0] == 'a' &&
        path[1] == 's' &&
        path[2] == 's' &&
        path[3] == 'e' &&
        path[4] == 't' &&
        path[5] == 's';
  }

  static bool isNetworkPath(String path) {
    return path[0] == 'h' && path[1] == 't' && path[2] == 't' && path[3] == 'p';
  }

  static bool isFilePath(String path) {
    return path[0] == '/';
  }

  static UriType parseUriType(String path) {
    switch (path[0]) {
      case 'p':
        if (path[1] == 'a' &&
            path[2] == 'c' &&
            path[3] == 'k' &&
            path[4] == 'a' &&
            path[5] == 'g' &&
            path[6] == 'e') {
          return UriType.assets;
        }
        break;
      case 'a':
        if (path[1] == 's' &&
            path[2] == 's' &&
            path[3] == 'e' &&
            path[4] == 't' &&
            path[5] == 's') {
          return UriType.assets;
        }
        break;
      case 'h':
        if (path[1] == 't' && path[2] == 't' && path[3] == 'p') {
          return UriType.http;
        }
        break;
      case '/':
        return UriType.file;
    }
    return UriType.unknown;
  }

  static Future<bool> canLaunch(Uri uri) async {
    return _url_launcher.canLaunchUrl(uri);
  }

  static launchURL(Uri uri) async {
    await _url_launcher.launchUrl(uri);
  }
}
