package com.cmput301.t05.habilect;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;
import com.searchly.jestdroid.DroidClientConfig;
import com.searchly.jestdroid.JestClientFactory;
import com.searchly.jestdroid.JestDroidClient;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Delete;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;

/**
 * Created by Steven C on 11/10/2017.
 */
public class WebService {
    private static JestDroidClient client;

    public static void verifySettings() {
        if (client == null) {
            DroidClientConfig.Builder builder = new DroidClientConfig.Builder("http://cmput301.softwareprocess.es:8080/cmput301f17t05_habilect");
            DroidClientConfig config = builder.build();

            JestClientFactory factory = new JestClientFactory();
            factory.setDroidClientConfig(config);
            client = (JestDroidClient) factory.getObject();
        }
    }

    public static String getDocumentIdFromUserProfile(UserProfile userProfile) {
        String query = "{\n" + " \"query\": { \"term\": {\"identifier\":\"" + "123" + "\"} }\n" + "}";
//        String query = "{\n" + " \"query\": { \"term\": {\"identifier\":\"" + userProfile.getIdentifier() + "\"} }\n" + "}";

        Search search = new Search.Builder(query)
                .addIndex("user")
                .build();
        try {
            SearchResult result = client.execute(search);
            if (result.isSucceeded()) {
                SearchResult.Hit hit = result.getFirstHit(Map.class);
                Map source = (Map) hit.source;
                return (String) source.get(JestResult.ES_METADATA_ID);
            }
        } catch (Exception e) {
            Log.i("Error", "Error grabbing id");
        }
        return null;
    }

    //region UserProfile services
    public static class AddUserProfileTask extends AsyncTask<UserProfile, Void, Void> {

        @Override
        protected Void doInBackground(UserProfile... userProfile) {
            verifySettings();

            Index index = new Index.Builder(userProfile[0]).index("user").build();

            try {
                DocumentResult result = client.execute(index);
                if (result.isSucceeded()) {
                    Log.i("Success", "Success creating");
                } else {
                    Log.i("Error", "Error creating");
                }
            } catch (Exception e) {
                Log.i("Error", "");
            }
            return null;
        }
    }

    public static class UpdateUserProfileTask extends AsyncTask<UserProfile, Void, Void> {

        @Override
        protected Void doInBackground(UserProfile... userProfile) {
            verifySettings();

            try {
                String documentId = getDocumentIdFromUserProfile(userProfile[0]);
                if (documentId != null) {
                    Index index = new Index.Builder(userProfile[0]).index("user").type(documentId).build();
                    DocumentResult resultUpdate = client.execute(index);
                    if (resultUpdate.isSucceeded()) {
                        Log.i("Success", "Success updating");
                    } else {
                        Log.i("Error", "Error updating");
                    }
                } else {
                    Log.i("Error", "");
                }

            } catch (Exception e) {
                Log.i("Error", "");
            }
            return null;
        }
    }

    public static class DeleteUserProfileTask extends AsyncTask<UserProfile, Void, Void> {

        @Override
        protected Void doInBackground(UserProfile... userProfile) {
            verifySettings();

            try {
                String documentId = getDocumentIdFromUserProfile(userProfile[0]);
                if (documentId != null) {
                    Delete index = new Delete.Builder(documentId).index("user").build();
                    DocumentResult resultUpdate = client.execute(index);
                    if (resultUpdate.isSucceeded()) {
                        Log.i("Success", "Success deleting");
                    } else {
                        Log.i("Error", "Error deleting");
                    }
                } else {
                    Log.i("Error", "Error deleting");
                }
            } catch (Exception e) {
                Log.i("Error", "");
            }
            return null;
        }
    }
    //endregion

    //region HabitType services
//    public static class AddHabitTypeTask extends AsyncTask<Object, Void, Void> {
//
//        @Override
//        protected Void doInBackground(Object... params) {
//            verifySettings();
//
//            UserProfile userProfile = (UserProfile) params[0];
//            HabitType habitType = (HabitType) params[1];
//
//            try {
//                String documentId = getDocumentIdFromUserProfile(userProfile);
//                if (documentId != null) {
//                    Index index = new Index.Builder(userProfile).index("user").type(documentId).build();
//                    DocumentResult resultUpdate = client.execute(index);
//                    if (resultUpdate.isSucceeded()) {
//                        Log.i("Success", "Success updating");
//                    } else {
//                        Log.i("Error", "Error updating");
//                    }
//                } else {
//                    Log.i("Error", "");
//                }
//            } catch (Exception e) {
//                Log.i("Error", "");
//            }
//            return null;
//        }
//    }
    //endregion
}

@@ -1287,113 +1287,8 @@
 ) %7B%0A
-        String query = %22%7B%5Cn%22 + %22 %5C%22query%5C%22: %7B %5C%22term%5C%22: %7B%5C%22identifier%5C%22:%5C%22%22 + %22123%22 + %22%5C%22%7D %7D%5Cn%22 + %22%7D%22;%0A//
     
@@ -4634,1174 +4634,7 @@
 gion
-%0A%0A    //region HabitType services%0A//    public static class AddHabitTypeTask extends AsyncTask%3CObject, Void, Void%3E %7B%0A//%0A//        @Override%0A//        protected Void doInBackground(Object... params) %7B%0A//            verifySettings();%0A//%0A//            UserProfile userProfile = (UserProfile) params%5B0%5D;%0A//            HabitType habitType = (HabitType) params%5B1%5D;%0A//%0A//            try %7B%0A//                String documentId = getDocumentIdFromUserProfile(userProfile);%0A//                if (documentId != null) %7B%0A//                    Index index = new Index.Builder(userProfile).index(%22user%22).type(documentId).build();%0A//                    DocumentResult resultUpdate = client.execute(index);%0A//                    if (resultUpdate.isSucceeded()) %7B%0A//                        Log.i(%22Success%22, %22Success updating%22);%0A//                    %7D else %7B%0A//                        Log.i(%22Error%22, %22Error updating%22);%0A//                    %7D%0A//                %7D else %7B%0A//                    Log.i(%22Error%22, %22%22);%0A//                %7D%0A//            %7D catch (Exception e) %7B%0A//                Log.i(%22Error%22, %22%22);%0A//            %7D%0A//            return null;%0A//        %7D%0A//    %7D%0A    //endregion
 %0A%7D%0A

Remove leftover test code