package chat.rocket.android.fragment.add_server;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.rest.DefaultServerPolicyApi;
import chat.rocket.android.api.rest.ServerPolicyApi;
import chat.rocket.android.helper.OkHttpHelper;
import chat.rocket.android.helper.ServerPolicyApiValidationHelper;
import chat.rocket.android.helper.ServerPolicyHelper;
import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.android.shared.BasePresenter;

public class InputHostnamePresenter extends BasePresenter<InputHostnameContract.View>
    implements InputHostnameContract.Presenter {

  private final RocketChatCache rocketChatCache;
  private final ConnectivityManagerApi connectivityManager;

  public InputHostnamePresenter(RocketChatCache rocketChatCache,
                                ConnectivityManagerApi connectivityManager) {
    this.rocketChatCache = rocketChatCache;
    this.connectivityManager = connectivityManager;
  }

  @Override
  public void connectTo(final String hostname) {
    view.showLoader();

    connectToEnforced(ServerPolicyHelper.enforceHostname(hostname));
  }

  public void connectToEnforced(final String hostname) {
    final ServerPolicyApi serverPolicyApi =
        new DefaultServerPolicyApi(OkHttpHelper.getClientForUploadFile(), hostname);

    final ServerPolicyApiValidationHelper validationHelper =
        new ServerPolicyApiValidationHelper(serverPolicyApi);

    clearSubscriptions();

    final Disposable subscription = ServerPolicyHelper.isApiVersionValid(validationHelper)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .doOnTerminate(() -> view.hideLoader())
        .subscribe(
            serverValidation -> {
              if (serverValidation.isValid()) {
                onServerValid(hostname, serverValidation.usesSecureConnection());
              } else {
                view.showInvalidServerError();
              }
            },
            throwable -> view.showConnectionError());

    addSubscription(subscription);
  }

  private void onServerValid(final String hostname, boolean usesSecureConnection) {
    rocketChatCache.setSelectedServerHostname(hostname);

    connectivityManager.addOrUpdateServer(hostname, hostname, !usesSecureConnection);
    connectivityManager.keepAliveServer();

    view.showHome();
  }
}

@@ -43,16 +43,61 @@
 erver;%0A%0A
+import chat.rocket.android.BackgroundLooper;%0A
 import i
@@ -193,51 +193,8 @@
 ble;
-%0Aimport io.reactivex.schedulers.Schedulers;
 %0A%0Aim
@@ -711,20 +711,16 @@
 ct.View%3E
-%0A   
  impleme
@@ -757,17 +757,16 @@
 enter %7B%0A
-%0A
   privat
@@ -935,40 +935,8 @@
 che,
-%0A                               
  Con
@@ -1159,25 +1159,24 @@
 owLoader();%0A
-%0A
     connectT
@@ -1337,24 +1337,16 @@
 icyApi =
-%0A       
  new Def
@@ -1411,25 +1411,24 @@
  hostname);%0A
-%0A
     final Se
@@ -1475,24 +1475,16 @@
 Helper =
-%0A       
  new Ser
@@ -1670,16 +1670,23 @@
 cribeOn(
+Android
 Schedule
@@ -1692,11 +1692,35 @@
 ers.
-io(
+from(BackgroundLooper.get()
 ))%0A 
@@ -2156,17 +2156,16 @@
 ror());%0A
-%0A
     addS
@@ -2225,22 +2225,16 @@
 erValid(
-final 
 String h
@@ -2328,24 +2328,67 @@
 hostname);%0A%0A
+    hostname = hostname.replace(%22/%22, %22.%22);%0A
     connecti
@@ -2531,9 +2531,8 @@
 );%0A  %7D%0A%7D
-%0A

Update InputHostnamePresenter.java