package org.lagonette.app.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;

public final class IntentUtil {

    private IntentUtil() {
    }

    //TODO Improve with label and do not start directly the directions
    public static boolean startDirection(@NonNull Context context, double latitude, double longitude) {
        Intent intent = new Intent(
                android.content.Intent.ACTION_VIEW,
                Uri.parse("google.navigation:q=" + latitude + "," + longitude)
        );
        PackageManager packageManager = context.getPackageManager();
        if (intent.resolveActivity(packageManager) != null) {
            context.startActivity(intent);
            return true;
        } else {
            return false;
        }
    }

    public static boolean makeCall(@NonNull Context context, @NonNull String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)); //TODO Improve || crash with genymotion
        PackageManager packageManager = context.getPackageManager();
        if (intent.resolveActivity(packageManager) != null) {
            context.startActivity(intent);
            return true;
        } else {
            return false;
        }
    }

    public static boolean goToWebsite(@NonNull Context context, @NonNull String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://" + url)); //TODO Improve
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
            return true;
        } else {
            return false;
        }
    }

    public static boolean writeEmail(@NonNull Context context, @NonNull String email) {
        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
        intent.putExtra(Intent.EXTRA_SUBJECT, "");
        intent.putExtra(Intent.EXTRA_TEXT, "");
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
            return true;
        } else {
            return false;
        }
    }
}

@@ -232,16 +232,236 @@
 Util %7B%0A%0A
+    public static final String SCHEME_TEL = %22tel%22;%0A%0A    public static final String SCHEME_MAILTO = %22mailto%22;%0A%0A    public static final String SCHEME_HTTP = %22http%22;%0A%0A    public static final String SCHEME_HTTPS = %22https%22;%0A%0A
     priv
@@ -475,24 +475,24 @@
 entUtil() %7B%0A
-
     %7D%0A%0A    /
@@ -1253,77 +1253,50 @@
 Uri.
-parse(%22tel:%22 + phoneNumber)); //TODO Improve %7C%7C crash with genymotion
+fromParts(SCHEME_TEL, phoneNumber, null));
 %0A   
@@ -1731,25 +1731,24 @@
 rse(
-%22http://%22 + 
+formatUrl(
 url))
+)
 ; //
@@ -2116,30 +2116,43 @@
 Uri.
-parse(%22mailto:%22 + emai
+fromParts(SCHEME_MAILTO, email, nul
 l));
@@ -2429,32 +2429,32 @@
   return false;%0A
-
         %7D%0A    %7D%0A
@@ -2452,11 +2452,244 @@
  %7D%0A    %7D
+%0A%0A    private static String formatUrl(@NonNull String url) %7B%0A        if (!url.startsWith(SCHEME_HTTP + %22://%22) && !url.startsWith(SCHEME_HTTP + %22://%22)) %7B%0A            url = SCHEME_HTTP + %22://%22 + url;%0A        %7D%0A        return url;%0A    %7D
 %0A%7D%0A

Improve intent util