package chat.rocket.android.helper;

import android.support.annotation.NonNull;

import org.json.JSONObject;

import rx.Observable;

public class ServerPolicyHelper {

  private static final String DEFAULT_HOST = ".rocket.chat";
  private static final String VERSION_PROPERTY = "version";

  public static String enforceHostname(String hostname) {
    if (hostname == null) {
      return "demo.rocket.chat";
    }

    return removeTrailingSlash(removeProtocol(enforceDefaultHost(hostname)));
  }

  public static Observable<ServerValidation> isApiVersionValid(
      @NonNull ServerPolicyApiValidationHelper serverPolicyApiValidationHelper) {
    return serverPolicyApiValidationHelper.getApiVersion()
        .map(serverInfo ->
            new ServerValidation(isValid(serverInfo.getApiInfo()),
                serverInfo.usesSecureConnection()));
  }

  @NonNull
  private static String enforceDefaultHost(String hostname) {
    if (hostname.indexOf('.') == -1) {
      hostname = hostname + DEFAULT_HOST;
    }
    return hostname;
  }

  @NonNull
  private static String removeProtocol(String hostname) {
    // yep. cheap.
    return hostname.replace("http://", "").replace("https://", "");
  }

  private static String removeTrailingSlash(String hostname) {
    if (hostname.charAt(hostname.length() - 1) != '/') {
      // no need for a regex - just return it
      return hostname;
    }

    return hostname.replaceAll("/+$", "");
  }

  private static boolean isValid(JSONObject jsonObject) {
    if (jsonObject == null) {
      return false;
    }

    try {
      return jsonObject.has(VERSION_PROPERTY)
          && isVersionValid(jsonObject.getString(VERSION_PROPERTY));
    } catch (Exception e) {
      return false;
    }
  }

  private static boolean isVersionValid(String version) {
    if (version == null || version.length() == 0) {
      return false;
    }

    String[] versionParts = version.split("\\.");
    return versionParts.length >= 3 && Integer.parseInt(versionParts[1]) >= 49;
  }

  public static class ServerInfoResponse {
    private final boolean secureConnection;
    private final JSONObject apiInfo;

    public ServerInfoResponse(boolean secureConnection, JSONObject apiInfo) {
      this.secureConnection = secureConnection;
      this.apiInfo = apiInfo;
    }

    public boolean usesSecureConnection() {
      return secureConnection;
    }

    public JSONObject getApiInfo() {
      return apiInfo;
    }
  }

  public static class ServerValidation {
    private final boolean valid;
    private final boolean secureConnection;

    public ServerValidation(boolean valid, boolean secureConnection) {
      this.valid = valid;
      this.secureConnection = secureConnection;
    }

    public boolean isValid() {
      return valid;
    }

    public boolean usesSecureConnection() {
      return secureConnection;
    }
  }
}

@@ -420,16 +420,49 @@
  return 
+removeExtraInvalidChars(%0A        
 removeTr
@@ -518,16 +518,17 @@
 tname)))
+)
 ;%0A  %7D%0A%0A 
@@ -1466,32 +1466,160 @@
 /+$%22, %22%22);%0A  %7D%0A%0A
+  private static String removeExtraInvalidChars(String hostname) %7B%0A    return hostname.replaceAll(%22%5B%5E%5C%5Cw%7C%5C%5C.%7C%5C%5C-%7C/%5D%22, %22%22);%0A  %7D%0A%0A
   private static

Remove more invalid chars