/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.react.modules.i18nmanager;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.text.TextUtilsCompat;
import android.support.v4.view.ViewCompat;

import java.util.Locale;

public class I18nUtil {
  private static I18nUtil sharedI18nUtilInstance = null;

  private static final String MY_PREFS_NAME =
    "com.facebook.react.modules.i18nmanager.I18nUtil";
  private static final String KEY_FOR_PREFS_ALLOWRTL =
    "RCTI18nUtil_allowRTL";
  private static final String KEY_FOR_PREFS_FORCERTL =
    "RCTI18nUtil_forceRTL";

  private I18nUtil() {
     // Exists only to defeat instantiation.
  }

  public static I18nUtil getInstance() {
    if(sharedI18nUtilInstance == null) {
      sharedI18nUtilInstance = new I18nUtil();
    }
    return sharedI18nUtilInstance;
  }

  /**
   * Check if the device is currently running on an RTL locale.
   * This only happens when the app:
   * - is forcing RTL layout, regardless of the active language (for development purpose)
   * - allows RTL layout when using RTL locale
   */
  public boolean isRTL(Context context) {
    if (isRTLForced(context)) {
      return true;
    }
    return isRTLAllowed(context) &&
      isDevicePreferredLanguageRTL();
  }

  /**
   * Should be used very early during app start up
   * Before the bridge is initialized
   */
  private boolean isRTLAllowed(Context context) {
    SharedPreferences prefs =
      context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
    return prefs.getBoolean(KEY_FOR_PREFS_ALLOWRTL, false);
  }

  public void allowRTL(Context context, boolean allowRTL) {
    SharedPreferences.Editor editor =
      context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
    editor.putBoolean(KEY_FOR_PREFS_ALLOWRTL, allowRTL);
    editor.apply();
  }

  /**
   * Could be used to test RTL layout with English
   * Used for development and testing purpose
   */
  private boolean isRTLForced(Context context) {
    SharedPreferences prefs =
      context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
    return prefs.getBoolean(KEY_FOR_PREFS_FORCERTL, false);
  }

  public void forceRTL(Context context, boolean allowRTL) {
    SharedPreferences.Editor editor =
      context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
    editor.putBoolean(KEY_FOR_PREFS_FORCERTL, allowRTL);
    editor.apply();
  }

  // Check if the current device language is RTL
  private boolean isDevicePreferredLanguageRTL() {
    final int directionality =
      TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault());
    return directionality == ViewCompat.LAYOUT_DIRECTION_RTL;
   }
}

@@ -705,18 +705,22 @@
  String 
-MY
+SHARED
 _PREFS_N
@@ -1782,372 +1782,152 @@
     
-SharedPreferences prefs =%0A      context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);%0A    return prefs.getBoolean(KEY_FOR_PREFS_ALLOWRTL, false);%0A  %7D%0A%0A  public void allowRTL(Context context, boolean allowRTL) %7B%0A    SharedPreferences.Editor editor =%0A      context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();%0A    editor.putBoolean(
+return isPrefSet(context, KEY_FOR_PREFS_ALLOWRTL, false);%0A  %7D%0A%0A  public void allowRTL(Context context, boolean allowRTL) %7B%0A    setPref(context, 
 KEY_
@@ -1957,36 +1957,16 @@
 owRTL);%0A
-    editor.apply();%0A
   %7D%0A%0A  /
@@ -2128,135 +2128,34 @@
     
-SharedPreferences prefs =%0A      context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);%0A    return prefs.getBoolean(
+return isPrefSet(context, 
 KEY_
@@ -2227,37 +2227,37 @@
 ontext, boolean 
-allow
+force
 RTL) %7B%0A    Share
@@ -2255,144 +2255,25 @@
     
-SharedPreferences.Editor editor =%0A      context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();%0A    editor.putBoolean(
+setPref(context, 
 KEY_
@@ -2296,39 +2296,19 @@
 TL, 
-allow
+force
 RTL);%0A
-    editor.apply();%0A
   %7D%0A
@@ -2578,11 +2578,503 @@
 TL;%0A   %7D
+%0A%0A  private boolean isPrefSet(Context context, String key, boolean defaultValue) %7B%0A    SharedPreferences prefs =%0A      context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);%0A    return prefs.getBoolean(key, defaultValue);%0A  %7D%0A%0A  private void setPref(Context context, String key, boolean value) %7B%0A    SharedPreferences.Editor editor =%0A      context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE).edit();%0A    editor.putBoolean(key, value);%0A    editor.apply();%0A  %7D
 %0A%7D%0A

Add `isPrefSet` and `setPref` to make I18nUtil cleaner