// Path: Android/app/src/main/java/app/intra/sys/firebase/LogWrapper.java
// public class LogWrapper {
//   public static void logException(Throwable t) {
//     Log.e("LogWrapper", "Error", t);
//     if (BuildConfig.DEBUG) {
//       return;
//     }
//     try {
//       FirebaseCrashlytics.getInstance().recordException(t);
//     } catch (IllegalStateException e) {
//       // This only occurs during unit tests.
//     }
//   }
// 
//   public static void log(int severity, String tag, String message) {
//     Log.println(severity, tag, message);
//     if (BuildConfig.DEBUG) {
//       return;
//     }
//     try {
//       FirebaseCrashlytics.getInstance().log(tag + ": " + message);
//     } catch (IllegalStateException e) {
//       // This only occurs during unit tests.
//     }
//   }
// }
// 
// Path: Android/app/src/main/java/app/intra/ui/settings/Untemplate.java
// public class Untemplate {
//   /**
//    * Performs variable expansion on a URI Template (RFC 6570) in the special case where all
//    * variables are undefined.  This is the only case of URI templates that is needed for DOH in POST
//    * mode.
//    * @param template A URI template (or just a URI)
//    * @return A URI produced by this template when all variables are undefined
//    */
//   public static String strip(String template) {
//     return template.replaceAll("\\{[^}]*\\}", "");
//   }
// }


import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import app.intra.R;
import app.intra.sys.firebase.LogWrapper;
import app.intra.ui.settings.Untemplate;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
      // In practice, we expect that domain will always be cloudflare-dns.com at this point, because
      // that was the only other option in the relevant legacy versions of Intra.
      // Look for the corresponding URL among the builtin servers.
      for (String u : urls) {
        Uri parsed = Uri.parse(u);
        if (domain.equals(parsed.getHost())) {
          url = u;
          break;
        }
      }
    }

    if (url == null) {
      LogWrapper.log(Log.WARN, LOG_TAG, "Legacy domain is unrecognized");
      return;
    }
    setServerUrl(context, url);
  }

  public static void setServerUrl(Context context, String url) {
    SharedPreferences.Editor editor = getUserPreferences(context).edit();
    editor.putString(URL_KEY, url);
    editor.apply();
  }

  public static String getServerUrl(Context context) {
    String urlTemplate = getUserPreferences(context).getString(URL_KEY, null);
    if (urlTemplate == null) {
      return null;
    }
// Path: Android/app/src/main/java/app/intra/sys/firebase/LogWrapper.java
// public class LogWrapper {
//   public static void logException(Throwable t) {
//     Log.e("LogWrapper", "Error", t);
//     if (BuildConfig.DEBUG) {
//       return;
//     }
//     try {
//       FirebaseCrashlytics.getInstance().recordException(t);
//     } catch (IllegalStateException e) {
//       // This only occurs during unit tests.
//     }
//   }
// 
//   public static void log(int severity, String tag, String message) {
//     Log.println(severity, tag, message);
//     if (BuildConfig.DEBUG) {
//       return;
//     }
//     try {
//       FirebaseCrashlytics.getInstance().log(tag + ": " + message);
//     } catch (IllegalStateException e) {
//       // This only occurs during unit tests.
//     }
//   }
// }
// 
// Path: Android/app/src/main/java/app/intra/ui/settings/Untemplate.java
// public class Untemplate {
//   /**
//    * Performs variable expansion on a URI Template (RFC 6570) in the special case where all
//    * variables are undefined.  This is the only case of URI templates that is needed for DOH in POST
//    * mode.
//    * @param template A URI template (or just a URI)
//    * @return A URI produced by this template when all variables are undefined
//    */
//   public static String strip(String template) {
//     return template.replaceAll("\\{[^}]*\\}", "");
//   }
// }


// Path: Android/app/src/main/java/app/intra/sys/PersistentState.java
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import app.intra.R;
import app.intra.sys.firebase.LogWrapper;
import app.intra.ui.settings.Untemplate;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

      // In practice, we expect that domain will always be cloudflare-dns.com at this point, because
      // that was the only other option in the relevant legacy versions of Intra.
      // Look for the corresponding URL among the builtin servers.
      for (String u : urls) {
        Uri parsed = Uri.parse(u);
        if (domain.equals(parsed.getHost())) {
          url = u;
          break;
        }
      }
    }

    if (url == null) {
      LogWrapper.log(Log.WARN, LOG_TAG, "Legacy domain is unrecognized");
      return;
    }
    setServerUrl(context, url);
  }

  public static void setServerUrl(Context context, String url) {
    SharedPreferences.Editor editor = getUserPreferences(context).edit();
    editor.putString(URL_KEY, url);
    editor.apply();
  }

  public static String getServerUrl(Context context) {
    String urlTemplate = getUserPreferences(context).getString(URL_KEY, null);
    if (urlTemplate == null) {
      return null;
    }
    return Untemplate.strip(urlTemplate);