import {
  addQueryParams, errorHandler, fetchRespHandler, getAuthHeaderOptions
} from "../utils/http";

const API_ENDPOINT = process.env.REACT_APP_OTA_SERVICE_URL;

const createDeployUpdatesClosure = (suffix) => {
  return async (data, token) =>
    fetch(`${API_ENDPOINT}/${suffix}`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify(data),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
};

const updatesAPI = {
  createFleetUpdates: createDeployUpdatesClosure("fleetupdate"),

  createCarUpdates: createDeployUpdatesClosure("carupdate"),

  getCarUpdateLog: async (query, token) => {
    const u = addQueryParams(`${API_ENDPOINT}/carupdateslog`, query);
    return fetch(u, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  getCarUpdateProgress: async (carupdateids, token) => {
    const u = `${API_ENDPOINT}/carupdatesstatuses?carupdateids=${carupdateids}`;
    return fetch(u, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  getCarUpdates: async (search, token) => {
    const u = addQueryParams(`${API_ENDPOINT}/carupdates`, search);
    return fetch(u, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  getVINUpdates: async (vin, token) => {
    const u = addQueryParams(`${API_ENDPOINT}/carupdates`, { vin });
    return fetch(u, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  cancelCarUpdate: async (id, token) => {
    return fetch(`${API_ENDPOINT}/carupdate/${id}/cancel`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  deployCarUpdate: async (id, token) => {
    return fetch(`${API_ENDPOINT}/carupdate/${id}/deploy`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  getSUMSVersions: async (options, token) => {
    return fetch(addQueryParams(`${API_ENDPOINT}/manifest/sums`, options), {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  addSUMSVersion: async (data, token) => {
    return fetch(`${API_ENDPOINT}/manifest/sums`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify(data),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  deleteSUMSVersion: async (version, token) => {
    return fetch(`${API_ENDPOINT}/manifest/sums/${version}`, {
      method: "DELETE",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  updateSUMSVersion: async (id, version, token) => {
    return fetch(`${API_ENDPOINT}/manifests/${id}/sums`, {
      method: "PUT",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token),
      ),
      body: JSON.stringify({ version }),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  getSUMSVersionRxSwins: async (sums_version, options, token) => {
    return fetch(addQueryParams(`${API_ENDPOINT}/manifest/sums/${sums_version}/rxswins`, options), {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  deleteSUMSVersionRxSwins: async (sums_version, rxswin, token) => {
    return fetch(`${API_ENDPOINT}/manifest/sums/${sums_version}/rxswins/${rxswin}`, {
      method: "DELETE",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

  addSUMSVersionRxSwins: async (sums_version, data, token) =>
    fetch(`${API_ENDPOINT}/manifest/sums/${sums_version}/rxswins`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify(data),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),
};

export default updatesAPI;