package com.huetoyou.chatexchange.auth;

import android.util.Log;

import com.huetoyou.chatexchange.net.RequestFactory;

import static jodd.jerry.Jerry.jerry;

/**
 * Perform Stack Exchange login via email and password
 */
class StackExchangeAuth {

    private static final String TAG = "StackExchangeAuth";

    /**
     * Callbacks for activity during the login process
     */
    interface Listener {

        /**
         * Authentication succeeded
         * @param authToken token containing cookies
         */
        void authSucceeded(String authToken);

        /**
         * Indicate progress of authentication
         * @param progress value between 0 and 100 inclusive
         */
        void authProgress(int progress);

        /**
         * Authentication failed
         * @param message descriptive error
         */
        void authFailed(String message);
    }

    private RequestFactory mRequestFactory = new RequestFactory();

    private String mEmail;
    private String mPassword;
    private Listener mListener;

    private String mLoginUrl;
    private String mNetworkFkey;

    /**
     * Implement the onFailed() method since it is always handled the same way
     */
    private abstract class RequestListener implements RequestFactory.Listener {
        @Override
        public void onFailed(String message) {
            Log.e(TAG, message);
            mListener.authFailed(message);
        }
    }

    private void confirmOpenId() {
        Log.d(TAG, "confirming OpenID...");
        //...
    }

    private void fetchChatFkey() {
        Log.d(TAG, "fetching chat fkey...");
        //...
    }

    private void fetchAuthUrl() {
        Log.d(TAG, "fetching auth URL...");
        //...
    }

    /**
     * Retrieve the network fkey from the login form
     */
    private void fetchNetworkFkey() {
        Log.d(TAG, "fetching network fkey...");
        mRequestFactory.get(mLoginUrl, new RequestListener() {
            @Override
            public void onSucceeded(String data) {
                mListener.authProgress(40);
                mNetworkFkey = jerry(data).$("#fkey").attr("value");
                if (mNetworkFkey == null) {
                    mListener.authFailed("unable to find network fkey");
                } else {
                    fetchAuthUrl();
                }
            }
        });
    }

    /**
     * Retrieve the URL of the page that contains the login form
     */
    private void fetchLoginUrl() {
        Log.d(TAG, "fetching login URL...");
        mRequestFactory.get("https://stackexchange.com/users/signin", new RequestListener() {
            @Override
            public void onSucceeded(String data) {
                mListener.authProgress(20);
                mLoginUrl = data;
                fetchNetworkFkey();
            }
        });
    }

    /**
     * Create a new Stack Exchange authenticator with the provided credentials
     * @param email account email obtained from the user
     * @param password account password obtained from the user
     * @param listener callback listener
     */
    StackExchangeAuth(String email, String password, Listener listener) {
        mEmail = email;
        mPassword = password;
        mListener = listener;

        // Start the authentication process
        fetchLoginUrl();
    }
}

@@ -114,16 +114,65 @@
 ctory;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 import s
@@ -201,16 +201,16 @@
 .jerry;%0A
-
 %0A/**%0A * 
@@ -1152,16 +1152,45 @@
 orkFkey;
+%0A    private String mAuthUrl;
 %0A%0A    /*
@@ -1709,32 +1709,96 @@
    //...%0A    %7D%0A%0A
+    /**%0A     * Submit the credentials the user supplied%0A     */%0A
     private void
@@ -1859,37 +1859,916 @@
 L...%22);%0A        
-//...
+Map%3CString, String%3E form = new HashMap%3C%3E();%0A        form.put(%22email%22, mEmail);%0A        form.put(%22password%22, mPassword);%0A        form.put(%22affId%22, %2211%22);%0A        form.put(%22fkey%22, mNetworkFkey);%0A        mRequestFactory.post(%0A                %22https://openid.stackexchange.com/affiliate/form/login/submit%22,%0A                form,%0A                new RequestListener() %7B%0A                    @Override%0A                    public void onSucceeded(String data) %7B%0A                        mListener.authProgress(60);%0A                        mAuthUrl = jerry(data).$(%22noscript a%22).attr(%22href%22);%0A                        if (mAuthUrl == null %7C%7C mAuthUrl.isEmpty()) %7B%0A                            mListener.authFailed(%22unable to read auth URL%22);%0A                        %7D else %7B%0A                            fetchChatFkey();%0A                        %7D%0A                    %7D%0A                %7D%0A        );
 %0A    %7D%0A%0A    /**%0A
@@ -3203,16 +3203,42 @@
  == null
+ %7C%7C mNetworkFkey.isEmpty()
 ) %7B%0A    
@@ -3285,19 +3285,19 @@
 able to 
-fin
+rea
 d networ

Implement more of the auth flow.