package me.shoutto.sdk;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.shoutto.sdk.internal.http.GsonDateAdapter;
import me.shoutto.sdk.internal.http.StmRequestQueue;

class MessageManager {

    private static final String TAG = MessageManager.class.getSimpleName();
    private StmService stmService;

    MessageManager(StmService stmService) {
        this.stmService = stmService;
    }

    void getMessages(final StmCallback<List<Message>> callback) {
        Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                List<Message> messageList = null;
                StmError stmError = null;
                try {
                    JSONObject data = response.getJSONObject("data");
                    JSONArray messageArray = data.getJSONArray(Message.LIST_SERIALIZATION_KEY);

                    RuntimeTypeAdapterFactory<StmBaseEntity> runtimeTypeAdapterFactory = RuntimeTypeAdapterFactory
                            .of(StmBaseEntity.class, StmBaseEntity.SERIALIZATION_FIELD)
                            .registerSubtype(Message.class, Message.SERIALIZATION_KEY);

                    Gson gson = new GsonBuilder()
                            .registerTypeAdapterFactory(runtimeTypeAdapterFactory)
                            .registerTypeAdapter(Date.class, new GsonDateAdapter())
                            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                            .create();

                    messageList = gson.fromJson(messageArray.toString(), Message.getSerializationListType());
                } catch (JSONException ex) {
                    Log.e(TAG, "Unable to parse message array response JSON", ex);
                    stmError = new StmError("Unable to parse message array response JSON", false,
                            StmError.SEVERITY_MAJOR);
                } catch (Exception ex) {
                    Log.e(TAG, "Unknown error occurred getting messages", ex);
                    stmError = new StmError("Unknown error occurred getting messages", false,
                            StmError.SEVERITY_MAJOR);
                } finally {
                    if (callback != null) {
                        if (stmError != null) {
                            callback.onError(stmError);
                        } else {
                            callback.onResponse(messageList);
                        }
                    }
                }
            }
        };

        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "An error occurred trying to get messages", error);
                if (callback != null) {
                    StmError stmError = new StmError("Error occurred getting messages.",
                            false, StmError.SEVERITY_MAJOR);
                    callback.onError(stmError);
                }
            }
        };

        String url = stmService.getServerUrl() + Message.BASE_ENDPOINT;
        final String authToken = stmService.getUserAuthToken();
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                responseListener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Authorization", "Bearer " + authToken);
                return params;
            }
        };
        StmRequestQueue.getInstance().addToRequestQueue(request);
    }

    void getUnreadMessageCount(final StmCallback<Integer> callback) {
        Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Integer count = null;
                StmError stmError = null;
                try {
                    JSONObject data = response.getJSONObject("data");
                    count = data.getInt("count");
                } catch(JSONException ex) {
                    Log.e(TAG, "Unable to parse get message count response JSON", ex);
                    stmError = new StmError("Unable to parse user update response JSON", false,
                            StmError.SEVERITY_MINOR);
                } catch (Exception ex) {
                    Log.e(TAG, "Unknown error occurred getting unread message count", ex);
                    stmError = new StmError("Unknown error occurred getting unread message count", false,
                            StmError.SEVERITY_MINOR);
                } finally {
                    if (callback != null) {
                        if (stmError != null) {
                            callback.onError(stmError);
                        } else {
                            callback.onResponse(count);
                        }
                    }
                }
            }
        };

        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "An error occurred trying to get unread message count", error);
                if (callback != null) {
                    StmError stmError = new StmError("Error occurred getting unread message count.",
                            false, StmError.SEVERITY_MINOR);
                    callback.onError(stmError);
                }
            }
        };

        String url = stmService.getServerUrl() + Message.BASE_ENDPOINT + "?count_only=true&unread_only=true";
        final String authToken = stmService.getUserAuthToken();
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                responseListener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Authorization", "Bearer " + authToken);
                return params;
            }
        };
        StmRequestQueue.getInstance().addToRequestQueue(request);
    }
}

@@ -4284,2623 +4284,7 @@
    %7D
-%0A%0A    void getUnreadMessageCount(final StmCallback%3CInteger%3E callback) %7B%0A        Response.Listener%3CJSONObject%3E responseListener = new Response.Listener%3CJSONObject%3E() %7B%0A            @Override%0A            public void onResponse(JSONObject response) %7B%0A                Integer count = null;%0A                StmError stmError = null;%0A                try %7B%0A                    JSONObject data = response.getJSONObject(%22data%22);%0A                    count = data.getInt(%22count%22);%0A                %7D catch(JSONException ex) %7B%0A                    Log.e(TAG, %22Unable to parse get message count response JSON%22, ex);%0A                    stmError = new StmError(%22Unable to parse user update response JSON%22, false,%0A                            StmError.SEVERITY_MINOR);%0A                %7D catch (Exception ex) %7B%0A                    Log.e(TAG, %22Unknown error occurred getting unread message count%22, ex);%0A                    stmError = new StmError(%22Unknown error occurred getting unread message count%22, false,%0A                            StmError.SEVERITY_MINOR);%0A                %7D finally %7B%0A                    if (callback != null) %7B%0A                        if (stmError != null) %7B%0A                            callback.onError(stmError);%0A                        %7D else %7B%0A                            callback.onResponse(count);%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D;%0A%0A        Response.ErrorListener errorListener = new Response.ErrorListener() %7B%0A            @Override%0A            public void onErrorResponse(VolleyError error) %7B%0A                Log.e(TAG, %22An error occurred trying to get unread message count%22, error);%0A                if (callback != null) %7B%0A                    StmError stmError = new StmError(%22Error occurred getting unread message count.%22,%0A                            false, StmError.SEVERITY_MINOR);%0A                    callback.onError(stmError);%0A                %7D%0A            %7D%0A        %7D;%0A%0A        String url = stmService.getServerUrl() + Message.BASE_ENDPOINT + %22?count_only=true&unread_only=true%22;%0A        final String authToken = stmService.getUserAuthToken();%0A        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,%0A                responseListener, errorListener) %7B%0A            @Override%0A            public Map%3CString, String%3E getHeaders() throws AuthFailureError %7B%0A                Map%3CString, String%3E params = new HashMap%3C%3E();%0A                params.put(%22Authorization%22, %22Bearer %22 + authToken);%0A                return params;%0A            %7D%0A        %7D;%0A        StmRequestQueue.getInstance().addToRequestQueue(request);%0A    %7D
 %0A%7D%0A

Remove an obsolete method