package vietnamworks.com.vnwcore;

import android.content.Context;
import android.support.annotation.NonNull;

import java.util.HashMap;

import R.helper.Callback;
import vietnamworks.com.volleyhelper.VolleyHelper;

/**
 * Created by duynk on 1/6/16.
 */
public class VNWAPI {
    private final static String productionServer = "https://api.vietnamworks.com";
    private final static String stagingServer = "https://api-staging.vietnamworks.com";

    private final static String API_JOB_SEARCH = "/jobs/search";

    private static String stagingKey;
    private static String productionKey;
    private static HashMap<String, String> header = new HashMap<>();
    private static Context context;
    private static Boolean isProduction;

    public static void init(Context ctx, String stagingKey, String productionKey, boolean isProduction) {
        VNWAPI.stagingKey = stagingKey;
        VNWAPI.productionKey = productionKey;
        VNWAPI.context = ctx;
        setProductionMode(isProduction);
    }

    public static void setProductionMode(boolean isProduction) {
        VNWAPI.isProduction = isProduction;
        if (isProduction) {
            header.put("CONTENT-MD5", VNWAPI.productionKey);
        } else {
            header.put("CONTENT-MD5", VNWAPI.stagingKey);
        }
    }

    public static void searchJob(int page_index, int page_size, @NonNull String job_title, String job_location, String job_category, Callback callback) {
        HashMap<String, String> input = new HashMap<>();
        input.put("job_title", job_title);
        if (job_location != null && !job_location.isEmpty()) {
            input.put("job_location", job_location);
        }
        if (job_category != null && !job_category.isEmpty()) {
            input.put("job_category", job_category);
        }
        VolleyHelper.post(context, isProduction?productionServer:stagingServer + API_JOB_SEARCH, header, input, callback);
    }

    public static void searchJob(int max_record, @NonNull String job_title, String job_location, String job_category, Callback callback) {
        searchJob(0, max_record, job_title, job_location, job_category, callback);
    }
}

@@ -104,16 +104,44 @@
 nNull;%0A%0A
+import org.json.JSONArray;%0A%0A
 import j
@@ -185,16 +185,81 @@
 llback;%0A
+import R.helper.CallbackResult;%0Aimport R.helper.CallbackSuccess;%0A
 import v
@@ -2250,11 +2250,1114 @@
 );%0A    %7D
+%0A%0A    public static void jobTitleSuggestion(@NonNull String jobTitle, final Callback callback) %7B%0A        HashMap%3CString, String%3E m = new HashMap%3C%3E();%0A        m.put(%22query%22, jobTitle);%0A%0A        VolleyHelper.stringRequest(context, %22http://www.vietnamworks.com/jobseekers/job_title_auto_completed_ajax.php%22, null, m, new Callback() %7B%0A            @Override%0A            public void onCompleted(Context context, CallbackResult result) %7B%0A                if (result.hasError()) %7B%0A                    callback.onCompleted(context, new CallbackResult(result.getError()));%0A                %7D else %7B%0A                    Object re = result.getData();%0A                    try %7B%0A                        String str = (String)re;%0A                        JSONArray jArray = new JSONArray(str);%0A                        callback.onCompleted(context, new CallbackSuccess(jArray));%0A                    %7D catch (Exception E) %7B%0A                        callback.onCompleted(context, new CallbackResult(new CallbackResult.CallbackError(-1, E.getMessage())));%0A                    %7D%0A                %7D%0A            %7D%0A        %7D);%0A    %7D
 %0A%7D%0A

add job title suggestion