package vietnamworks.com.vnwcore;

import android.content.Context;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import R.helper.Callback;
import R.helper.CallbackResult;
import R.helper.CallbackSuccess;
import R.helper.CodecX;
import R.helper.Common;
import R.helper.LocalStorage;

/**
 * Created by duynk on 1/20/16.
 */
public class Auth {
    public enum ELoginError {
        EMPTY_EMAIL (10000),
        INVALID_EMAIL (10001),
        EMPTY_PASSWORD (10002),
        WRONG_CREDENTIAL (10003)


        ;
        private final int code;

        private ELoginError(int code) {
            this.code = code;
        }

        public boolean is(int code) {
            return this.code == code;
        }
    }

    private static ArrayList<String> recentEmails = new ArrayList<>();
    public static void login(Context ctx, final String email, final String password, final Callback callback) {
        if (email == null || email.isEmpty()) {
            callback.onCompleted(ctx, new CallbackResult(new CallbackResult.CallbackError(ELoginError.EMPTY_EMAIL.code, "")));
        } else if (!Common.isValidEmail(email)) {
            callback.onCompleted(ctx, new CallbackResult(new CallbackResult.CallbackError(ELoginError.INVALID_EMAIL.code, "")));
        } else if (password == null || password.isEmpty()) {
            callback.onCompleted(ctx, new CallbackResult(new CallbackResult.CallbackError(ELoginError.EMPTY_PASSWORD.code, "")));
        } else {
            VNWAPI.login(ctx, email, password, new Callback() {
                @Override
                public void onCompleted(Context context, CallbackResult result) {
                    if (result.hasError()) {
                        callback.onCompleted(context, new CallbackResult(result.getError()));
                    } else {
                        //save current account;
                        try {
                            JSONObject data = (JSONObject) result.getData();
                            if (data.getJSONObject("meta").getString("message").equalsIgnoreCase("OK")) {
                                vietnamworks.com.vnwcore.entities.Auth auth = new vietnamworks.com.vnwcore.entities.Auth();
                                auth.importFromJson(data.getJSONObject("data"));

                                LocalStorage.set("vnw_auth_current_account", data);

                                String strRecentEmail = LocalStorage.getString("vnw_auth_recent_emails", "");
                                String []emails = strRecentEmail.split(";");
                                HashMap<String, String> emailHash = new HashMap<String, String>();
                                for (String e: emails) {
                                    emailHash.put(e, "");
                                }
                                StringBuilder sb = new StringBuilder();
                                recentEmails = new ArrayList<String>();
                                String delim = "";
                                for (String k: emailHash.keySet()) {
                                    sb.append(delim);
                                    sb.append(k);
                                    recentEmails.add(k);
                                    delim = ";";
                                }
                                LocalStorage.set("vnw_auth_recent_emails", sb.toString());

                                String epassword = CodecX.encode(password);
                                LocalStorage.set("vnw_auth_current_credential", email + ";" + epassword);
                                callback.onCompleted(context, new CallbackSuccess());
                            } else {
                                if (data.getJSONObject("meta").getString("code").equalsIgnoreCase("200")) {
                                    callback.onCompleted(context, new CallbackResult(new CallbackResult.CallbackError(ELoginError.WRONG_CREDENTIAL.code, "")));
                                } else {
                                    callback.onCompleted(context, new CallbackResult(new CallbackResult.CallbackError(-1, "")));
                                }
                            }
                        } catch (Exception E) {
                            callback.onCompleted(context, new CallbackResult(new CallbackResult.CallbackError(-1, E.getMessage())));
                        }
                    }
                    }
            });
        }
    }

    public static void autoLogin(Context ctx, final Callback callback) {
        String credential = LocalStorage.getString("vnw_auth_current_credential", null);
        if (credential == null) {
            callback.onCompleted(ctx, new CallbackResult(new CallbackResult.CallbackError(-1, "")));
        } else {
            int split_index = credential.indexOf(";");
            String email = credential.substring(0, split_index);
            String epassword = credential.substring(split_index + 1, credential.length());
            String password = CodecX.decode(epassword);
            if (password == null || password.isEmpty() || email.isEmpty()) {
                callback.onCompleted(ctx, new CallbackResult(new CallbackResult.CallbackError(-2, "")));
            } else {
                login(ctx, email, password, callback);
            }
        }
    }

    public static void logout(Callback callback) {

    }
}

@@ -2532,897 +2532,157 @@
     
-String %5B%5Demails = strRecentEmail.split(%22;%22);%0A                                HashMap%3CString, String%3E emailHash = new HashMap%3CString, String%3E();%0A                                for (String e: emails) %7B%0A                                    emailHash.put(e, %22%22);%0A                                %7D%0A                                StringBuilder sb = new StringBuilder();%0A                                recentEmails = new ArrayList%3CString%3E();%0A                                String delim = %22%22;%0A                                for (String k: emailHash.keySet()) %7B%0A                                    sb.append(delim);%0A                                    sb.append(k);%0A                                    recentEmails.add(k);%0A                                    delim = %22;%22;%0A                                %7D%0A                                LocalStorage.set(%22vnw_auth_r
+strRecentEmail = strRecentEmail + %22;%22 + email;%0A                                recentEmails.clear();%0A                                getR
 ecent
-_e
+E
 mails
-%22, sb.toString()
+(
 );%0A%0A
@@ -4672,37 +4672,1014 @@
  logout(
-Callback callback) %7B%0A
+) %7B%0A        LocalStorage.remove(%22vnw_auth_current_account%22);%0A        LocalStorage.remove(%22vnw_auth_current_credential%22);%0A    %7D%0A%0A    public static ArrayList%3CString%3E getRecentEmails() %7B%0A        if (recentEmails == null %7C%7C recentEmails.isEmpty()) %7B%0A            String strRecentEmail = LocalStorage.getString(%22vnw_auth_recent_emails%22, %22%22);%0A            String %5B%5Demails = strRecentEmail.split(%22;%22);%0A            HashMap%3CString, String%3E emailHash = new HashMap%3CString, String%3E();%0A            for (String e: emails) %7B%0A                emailHash.put(e, %22%22);%0A            %7D%0A            StringBuilder sb = new StringBuilder();%0A            recentEmails = new ArrayList%3CString%3E();%0A            String delim = %22%22;%0A            for (String k: emailHash.keySet()) %7B%0A                sb.append(delim);%0A                sb.append(k);%0A                recentEmails.add(k);%0A                delim = %22;%22;%0A            %7D%0A            LocalStorage.set(%22vnw_auth_recent_emails%22, sb.toString());%0A        %7D%0A        return recentEmails;
 %0A    %7D%0A%7D

load recent emails