/**
 * Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the LICENSE file in the root
 * directory of this source tree. An additional grant of patent rights can be found in the PATENTS
 * file in the same directory.
 */

package com.facebook.react.packagerconnection;

import java.util.Map;

import android.net.Uri;

import com.facebook.common.logging.FLog;
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.ws.WebSocket;

import org.json.JSONObject;

/**
 * A client for packager that uses WebSocket connection.
 */
final public class JSPackagerClient implements ReconnectingWebSocket.MessageCallback {
  private static final String TAG = JSPackagerClient.class.getSimpleName();
  private static final String PACKAGER_CONNECTION_URL_FORMAT = "ws://%s/message?device=%s&app=%s&context=%s";
  private static final int PROTOCOL_VERSION = 2;

  private class ResponderImpl implements Responder {
    private Object mId;

    public ResponderImpl(Object id) {
      mId = id;
    }

    public void respond(Object result) {
      try {
        JSONObject message = new JSONObject();
        message.put("version", PROTOCOL_VERSION);
        message.put("id", mId);
        message.put("result", result);
        mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message.toString()));
      } catch (Exception e) {
        FLog.e(TAG, "Responding failed", e);
      }
    }

    public void error(Object error) {
      try {
        JSONObject message = new JSONObject();
        message.put("version", PROTOCOL_VERSION);
        message.put("id", mId);
        message.put("error", error);
        mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message.toString()));
      } catch (Exception e) {
        FLog.e(TAG, "Responding with error failed", e);
      }
    }
  }

  private ReconnectingWebSocket mWebSocket;
  private Map<String, RequestHandler> mRequestHandlers;

  public JSPackagerClient(String clientId, PackagerConnectionSettings settings, Map<String, RequestHandler> requestHandlers) {
    super();

    Uri.Builder builder = new Uri.Builder();
    builder.scheme("ws")
      .encodedAuthority(settings.getDebugServerHost())
      .appendPath("message")
      .appendQueryParameter("device", AndroidInfoHelpers.getFriendlyDeviceName())
      .appendQueryParameter("app", settings.getPackageName())
      .appendQueryParameter("clientid", clientId);
    String url = builder.build().toString();

    mWebSocket = new ReconnectingWebSocket(url, this);
    mRequestHandlers = requestHandlers;
  }

  public void init() {
    mWebSocket.connect();
  }

  public void close() {
    mWebSocket.closeQuietly();
  }

  @Override
  public void onMessage(ResponseBody response) {
    if (response.contentType() != WebSocket.TEXT) {
      FLog.w(
        TAG,
        "Websocket received message with payload of unexpected type " + response.contentType());
      return;
    }

    try {
      JSONObject message = new JSONObject(response.string());

      int version = message.optInt("version");
      String method = message.optString("method");
      Object id = message.opt("id");
      Object params = message.opt("params");

      if (version != PROTOCOL_VERSION) {
        FLog.e(
          TAG,
          "Message with incompatible or missing version of protocol received: " + version);
        return;
      }

      if (method == null) {
        abortOnMessage(id, "No method provided");
        return;
      }

      RequestHandler handler = mRequestHandlers.get(method);
      if (handler == null) {
        abortOnMessage(id, "No request handler for method: " + method);
        return;
      }

      if (id == null) {
        handler.onNotification(params);
      } else {
        handler.onRequest(params, new ResponderImpl(id));
      }
    } catch (Exception e) {
      FLog.e(TAG, "Handling the message failed", e);
    } finally {
      response.close();
    }
  }

  private void abortOnMessage(Object id, String reason) {
    if (id != null) {
      (new ResponderImpl(id)).error(reason);
    }

    FLog.e(TAG, "Handling the message failed with reason: " + reason);
  }
}
