package com.mobify.astro.messaging;


import android.util.Log;

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

public class RpcRequestFactory {
    private static final String TAG = RpcRequestFactory.class.getName();

    private static AddressableObject sender = new AddressableObject() {
        @Override
        public String getInstanceAddress() {
            return String.format("%s:%d", this.getClass().getCanonicalName(), this.hashCode());
        }
    };
    private static int id;

    public static RpcRequest emptyRequest() {
        JSONObject payload = new JSONObject();
        RpcRequest request = new RpcRequest(payload, sender, Integer.toString(id++));

        return request;
    }

    public static RpcRequest requestWithSenderAndPayloadJSON(MessageListener listener, String jsonString) {
        RpcRequest req = requestWithPayloadJSON(jsonString);
        req.setSenderAddress(listener);
        return req;
    }

    public static RpcRequest requestWithPayloadJSON(String jsonString) {
        JSONObject payload = null;
        try {
            payload = new JSONObject(jsonString);
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        RpcRequest request = new RpcRequest(payload, sender, Integer.toString(id++));
        return request;
    }
}
