package in.testpress.testpress.util;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.net.Uri;
import android.support.annotation.StringRes;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import in.testpress.testpress.R;
import in.testpress.testpress.TestpressServiceProvider;
import in.testpress.testpress.authenticator.LogoutService;
import in.testpress.testpress.core.TestpressService;
import in.testpress.testpress.models.InstituteSettings;
import in.testpress.testpress.ui.RssFeedDetailActivity;

public class UIUtils {

    /**
     * Helps determine if the app is running in a Tablet context.
     *
     * @param context
     * @return
     */
    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    public static void hideSoftKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }

    public static void setGone(View[] views) {
        for (View view : views) {
            view.setVisibility(View.GONE);
        }
    }

    public static void setVisible(View[] views) {
        for (View view : views) {
            view.setVisibility(View.VISIBLE);
        }
    }

    public static AlertDialog.Builder getAlertDialog(Context context, @StringRes int title,
                                                     @StringRes int messageRes) {

        return new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyle)
                .setTitle(title)
                .setMessage(messageRes)
                .setNegativeButton(R.string.ok, null);
    }

    public static void openInBrowser(Context context, String url) {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.setToolbarColor(ContextCompat.getColor(context, R.color.primary));
        CustomTabsIntent customTabsIntent = builder.build();
        try {
            customTabsIntent.launchUrl(context, Uri.parse(url));
        } catch (ActivityNotFoundException e) {
            boolean wrongUrl = !url.startsWith("http://") && !url.startsWith("https://");
            int message = wrongUrl ? R.string.wrong_url : R.string.browser_not_available;
            UIUtils.getAlertDialog(context, R.string.not_supported, message).show();
        }
    }

    public static String getMenuItemName(int titleResId, InstituteSettings instituteSettings) {
        switch (titleResId) {
            case R.string.dashboard:
                return instituteSettings.getDashboardLabel();
            case R.string.testpress_leaderboard:
                return instituteSettings.getLeaderboardLabel();
            case R.string.bookmarks:
                return instituteSettings.getBookmarksLabel();
            case R.string.documents:
                return instituteSettings.getDocumentsLabel();
            case R.string.store:
                return instituteSettings.getStoreLabel();
            case R.string.posts:
                return instituteSettings.getPostsLabel();
            case R.string.learn:
                return instituteSettings.getLearnLabel();
            case R.string.label_username:
                return instituteSettings.getLoginLabel();
            case R.string.label_password:
                return instituteSettings.getLoginPasswordLabel();
            default:
                return "";
        }
    }
}

@@ -828,16 +828,60 @@
 ctivity;
+%0Aimport in.testpress.testpress.util.Strings;
 %0A%0Apublic
@@ -3121,32 +3121,49 @@
          return 
+Strings.toString(
 instituteSetting
@@ -3175,32 +3175,33 @@
 DashboardLabel()
+)
 ;%0A            ca
@@ -3250,32 +3250,49 @@
          return 
+Strings.toString(
 instituteSetting
@@ -3306,32 +3306,33 @@
 aderboardLabel()
+)
 ;%0A            ca
@@ -3369,32 +3369,49 @@
          return 
+Strings.toString(
 instituteSetting
@@ -3423,32 +3423,33 @@
 BookmarksLabel()
+)
 ;%0A            ca
@@ -3486,32 +3486,49 @@
          return 
+Strings.toString(
 instituteSetting
@@ -3540,32 +3540,33 @@
 DocumentsLabel()
+)
 ;%0A            ca
@@ -3599,32 +3599,49 @@
          return 
+Strings.toString(
 instituteSetting
@@ -3653,24 +3653,25 @@
 StoreLabel()
+)
 ;%0A          
@@ -3708,32 +3708,49 @@
          return 
+Strings.toString(
 instituteSetting
@@ -3762,24 +3762,25 @@
 PostsLabel()
+)
 ;%0A          
@@ -3817,32 +3817,49 @@
          return 
+Strings.toString(
 instituteSetting
@@ -3867,32 +3867,33 @@
 .getLearnLabel()
+)
 ;%0A            ca
@@ -3935,32 +3935,49 @@
          return 
+Strings.toString(
 instituteSetting
@@ -3989,24 +3989,25 @@
 LoginLabel()
+)
 ;%0A          
@@ -4061,16 +4061,33 @@
  return 
+Strings.toString(
 institut
@@ -4119,16 +4119,17 @@
 dLabel()
+)
 ;%0A      

Handle null string for getMenuItemName in Utils