import Integration from "../data/integration";
import Serializer from "../data/serializer";
import Net from "../tools/net";

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

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

    let deviceObjects = JSON.parse(response.data);
    return Serializer.deserializeDevices(deviceObjects);
  }

  async function createDevice({ name, description }) {
    let response = await Net.sendJsonRequest({
      method: "POST",
      url: "/api/devices",
      data: { name, description },
    });

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

    let deviceObject = JSON.parse(response.data);
    let device = Serializer.deserializeDevice(deviceObject);
    return device;
  }

  async function updateDevice(deviceId, { name, description }) {
    let response = await Net.sendJsonRequest({
      method: "PUT",
      url: "/api/devices/" + deviceId,
      data: { name, description },
    });

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

    let deviceObject = JSON.parse(response.data);
    let device = Serializer.deserializeDevice(deviceObject);
    return device;
  }

  async function deleteDevice(deviceId) {
    let response = await Net.sendJsonRequest({
      method: "DELETE",
      url: "/api/devices/" + deviceId,
    });

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

  async function getRegistrationCode() {
    let response = await Net.sendRequest({
      method: "POST",
      url: "/api/integrations",
    });

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

    let codeObject = JSON.parse(response.data);
    return codeObject.code;
  }

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

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

    let integrations = JSON.parse(response.data);
    integrations = Serializer.deserializeIntegrations(integrations);
    return integrations;
  }
  
  async function deleteIntegration(id) {
    let response = await Net.sendJsonRequest({
      method: "DELETE",
      url: "/api/integrations/" + id,
    });
    
    if (response.status !== 200) {
      let responseData = JSON.parse(response.data);
      throw new Error(responseData.error);
    }
  }

  return {
    getDevices,
    createDevice,
    updateDevice,
    deleteDevice,
    getRegistrationCode,
    getIntegrations,
    deleteIntegration,
  };
}

DeviceService = new DeviceService();

export default DeviceService;
