package com.zulip.android;

import org.apache.http.HttpStatus;
import org.apache.http.client.HttpResponseException;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.TextView;

class AsyncLogin extends ZulipAsyncPushTask {
    public static final String UNREGISTERED = "unregistered";
    public static final String DISABLED = "disabled";

    LoginActivity context;
    boolean userDefinitelyInvalid = false;

    public AsyncLogin(LoginActivity loginActivity, String username,
            String password) {
        super(loginActivity.app);
        context = loginActivity;
        if (username.contains("@") == true) {
            // @-less usernames are used as indicating special cases, for
            // example in OAuth2 authentication
            this.app.setEmail(username);
        }
        this.setProperty("username", username);
        this.setProperty("password", password);
    }

    public final void execute() {
        execute("POST", "v1/fetch_api_key");
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (result != null) {
            try {
                JSONObject obj = new JSONObject(result);

                if (obj.getString("result").equals("success")) {
                    this.app.setLoggedInApiKey(obj.getString("api_key"));
                    this.context.openHome();
                    callback.onTaskComplete(result);

                    return;
                }
            } catch (JSONException e) {
                ZLog.logException(e);
            }
        }
        TextView errorText = (TextView) this.context
                .findViewById(R.id.error_text);
        errorText.setText("Unknown error");
        Log.wtf("login", "We shouldn't have gotten this far.");
    }

    @Override
    protected void handleHTTPError(final HttpResponseException e) {
        final TextView errorText = (TextView) this.context
                .findViewById(R.id.error_text);
        if (e.getStatusCode() == HttpStatus.SC_FORBIDDEN) {
            try {
                JSONObject obj = new JSONObject(e.getMessage());
                String reason = obj.getString("reason");
                userDefinitelyInvalid = reason.equals(AsyncLogin.DISABLED)
                        || reason.equals(AsyncLogin.UNREGISTERED);
            } catch (JSONException e1) {
                ZLog.logException(e1);
            }
            errorText.post(new Runnable() {
                @Override
                public void run() {
                    errorText.setText("Incorrect username or password");
                }
            });
            this.cancel(true);
        } else {
            errorText.post(new Runnable() {
                @Override
                public void run() {
                    errorText.setText("Network error");
                }
            });
            // supermethod invokes cancel for us
            super.handleHTTPError(e);
        }
    }
}

@@ -2090,24 +2090,86 @@
 ORBIDDEN) %7B%0A
+            String message = %22Unknown authentication error.%22;%0A
             
@@ -2166,32 +2166,32 @@
           try %7B%0A
-
                 
@@ -2292,24 +2292,72 @@
 (%22reason%22);%0A
+                message = obj.getString(%22msg%22);%0A
             
@@ -2572,32 +2572,81 @@
 ;%0A            %7D%0A
+            final String finalMessage = message;%0A
             erro
@@ -2777,40 +2777,20 @@
 ext(
-%22Incorrect username or password%22
+finalMessage
 );%0A 

Use error messages returned by the server, not client-specified ones.