import Command from "../data/command";
import Serializer from "../data/serializer";
import Net from "../tools/net";
import WebRTCService from "./webrtc-service";

const MESSAGE_TYPE_COMMAND = "command";

function RemoteService() {
  async function getRemote(remoteId) {
    let response = await Net.sendRequest({
      method: "GET",
      url: "/api/remotes/" + remoteId,
    });

    if (response.status !== 200) {
      let responseData = JSON.parse(response.data);
      throw new Error(responseData.error);
    }

    let remoteObject = JSON.parse(response.data);
    return Serializer.deserializeRemote(remoteObject);
  }

  async function getRemotes() {
    let response = await Net.sendRequest({
      method: "GET",
      url: "/api/remotes",
    });

    if (response.status !== 200) {
      let responseData = JSON.parse(response.data);
      throw new Error(responseData.error);
    }

    let remoteObjects = JSON.parse(response.data);
    return Serializer.deserializeRemotes(remoteObjects);
  }

  async function createRemote(remote) {
    let remoteObject = Serializer.serializeRemote(remote);
    let response = await Net.sendJsonRequest({
      method: "POST",
      url: "/api/remotes",
      data: remoteObject,
    });

    if (response.status !== 200) {
      let responseData = JSON.parse(response.data);
      throw new Error(responseData.error);
    }
  }

  async function deleteRemote(remoteId) {
    let response = await Net.sendRequest({
      method: "DELETE",
      url: "/api/remotes/" + remoteId,
    });

    if (response.status !== 200) {
      let responseData = JSON.parse(response.data);
      throw new Error(responseData.error);
    }
  }

  async function getCommands() {
    let response = await Net.sendRequest({
      method: "GET",
      url: "/api/commands",
    });

    if (response.status !== 200) {
      let responseData = JSON.parse(response.data);
      throw new Error(responseData.error);
    }

    let commandObjects = JSON.parse(response.data);
    return Serializer.deserializeCommands(commandObjects);
  }

  async function createCommand(command) {
    return createCommands([command]);
  }

  async function createCommands(commands) {
    let commandObjects = Serializer.serializeCommands(commands);
    let response = await Net.sendJsonRequest({
      method: "POST",
      url: "/api/commands",
      data: commandObjects,
    });

    if (response.status !== 200) {
      let responseData = JSON.parse(response.data);
      throw new Error(responseData.error);
    }
  }

  async function deleteCommand(commandId) {
    let response = await Net.sendRequest({
      method: "DELETE",
      url: "/api/commands/" + commandId,
    });

    if (response.status !== 200) {
      let responseData = JSON.parse(response.data);
      throw new Error(responseData.error);
    }
  }

  function sendCommand(command) {
    let commandObject = Serializer.serializeCommand(command);
    WebRTCService.sendDataJson({type: MESSAGE_TYPE_COMMAND, data: commandObject});
  }

  return {
    getRemote,
    getRemotes,
    createRemote,
    deleteRemote,
    getCommands,
    createCommand,
    createCommands,
    deleteCommand,
    sendCommand,
  };
}

RemoteService = new RemoteService();

export default RemoteService;
