var RESLEVIS_API_BASE = "/frontend/api/reslevis";

function reslevisRequest(path, options) {
  var url = RESLEVIS_API_BASE + path;

  var baseHeaders = {
    "Accept": "application/json"
  };

  var headers = baseHeaders;
  if (options && options.headers) {
    headers = {};
    var k1;
    for (k1 in baseHeaders) {
      if (Object.prototype.hasOwnProperty.call(baseHeaders, k1)) {
        headers[k1] = baseHeaders[k1];
      }
    }
    var k2;
    for (k2 in options.headers) {
      if (Object.prototype.hasOwnProperty.call(options.headers, k2)) {
        headers[k2] = options.headers[k2];
      }
    }
  }

  var fetchOptions = {};
  if (options) {
    var k3;
    for (k3 in options) {
      if (Object.prototype.hasOwnProperty.call(options, k3)) {
        fetchOptions[k3] = options[k3];
      }
    }
  }
  fetchOptions.headers = headers;

  console.log("API URL", url);
  console.log("API options", fetchOptions);

  return fetch(url, fetchOptions).then(function (response) {
    console.log("HTTP status", response.status, response.statusText);

    var hdrs = {};
    response.headers.forEach(function (value, key) {
      hdrs[key] = value;
    });
    console.log("HTTP headers", hdrs);

    return response.text().then(function (body) {
      console.log("RAW body", body);

      if (!response.ok) {
        throw new Error("HTTP " + response.status + " " + response.statusText + " - " + body);
      }

      if (!body) {
        return null;
      }

      var ct = response.headers.get("content-type") || "";
      if (ct.indexOf("application/json") === -1) {
        throw new Error("Expected JSON but got Content-Type=" + ct + " body=" + body);
      }

      try {
        return JSON.parse(body);
      } catch (e) {
        throw new Error("JSON parse error: " + e.message + " body=" + body);
      }
    });
  });
}

/* ===== Gateways ===== */
function getGateways() {
  return reslevisRequest("/getGateways", { method: "GET" });
}

function postGateway(item) {
  return reslevisRequest("/postGateway", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updateGateway(item) {
  return reslevisRequest("/updateGateway", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removeGateway(id) {
  return reslevisRequest("/removeGateway/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Buildings ===== */
function getBuildings() {
  return reslevisRequest("/getBuildings", { method: "GET" });
}

function postBuilding(item) {
  return reslevisRequest("/postBuilding", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updateBuilding(item) {
  return reslevisRequest("/updateBuilding", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removeBuilding(id) {
  return reslevisRequest("/removeBuilding/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Plans ===== */
function getPlans() {
  return reslevisRequest("/getPlans", { method: "GET" });
}

function postPlan(item) {
  return reslevisRequest("/postPlan", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updatePlan(item) {
  return reslevisRequest("/updatePlan", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removePlan(id) {
  return reslevisRequest("/removePlan/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Zones ===== */
function getZones() {
  return reslevisRequest("/getZones", { method: "GET" });
}

function postZone(item) {
  return reslevisRequest("/postZone", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updateZone(item) {
  return reslevisRequest("/updateZone", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removeZone(id) {
  return reslevisRequest("/removeZone/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Trackers ===== */
function getTrackers() {
  return reslevisRequest("/getTrackers", { method: "GET" });
}

function postTracker(item) {
  return reslevisRequest("/postTracker", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updateTracker(item) {
  return reslevisRequest("/updateTracker", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removeTracker(id) {
  return reslevisRequest("/removeTracker/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Operators ===== */
function getOperators() {
  return reslevisRequest("/getOperators", { method: "GET" });
}

function postOperator(item) {
  return reslevisRequest("/postOperator", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updateOperator(item) {
  return reslevisRequest("/updateOperator", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removeOperator(id) {
  return reslevisRequest("/removeOperator/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Subjects ===== */
function getSubjects() {
  return reslevisRequest("/getSubjects", { method: "GET" });
}

function postSubject(item) {
  return reslevisRequest("/postSubject", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updateSubject(item) {
  return reslevisRequest("/updateSubject", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removeSubject(id) {
  return reslevisRequest("/removeSubject/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Alarms ===== */
function getAlarms() {
  return reslevisRequest("/getAlarms", { method: "GET" });
}

function postAlarm(item) {
  return reslevisRequest("/postAlarm", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updateAlarm(item) {
  return reslevisRequest("/updateAlarm", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removeAlarm(id) {
  return reslevisRequest("/removeAlarm/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Tracks ===== */
function getTracks() {
  return reslevisRequest("/getTracks", { method: "GET" });
}

function postTrack(item) {
  return reslevisRequest("/postTrack", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function updateTrack(item) {
  return reslevisRequest("/updateTrack", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(item)
  });
}

function removeTrack(id) {
  return reslevisRequest("/removeTrack/" + encodeURIComponent(id), { method: "DELETE" });
}

/* ===== Export global ===== */
window.getGateways = getGateways;
window.postGateway = postGateway;
window.updateGateway = updateGateway;
window.removeGateway = removeGateway;

window.getBuildings = getBuildings;
window.postBuilding = postBuilding;
window.updateBuilding = updateBuilding;
window.removeBuilding = removeBuilding;

window.getPlans = getPlans;
window.postPlan = postPlan;
window.updatePlan = updatePlan;
window.removePlan = removePlan;

window.getZones = getZones;
window.postZone = postZone;
window.updateZone = updateZone;
window.removeZone = removeZone;

window.getTrackers = getTrackers;
window.postTracker = postTracker;
window.updateTracker = updateTracker;
window.removeTracker = removeTracker;

window.getOperators = getOperators;
window.postOperator = postOperator;
window.updateOperator = updateOperator;
window.removeOperator = removeOperator;

window.getSubjects = getSubjects;
window.postSubject = postSubject;
window.updateSubject = updateSubject;
window.removeSubject = removeSubject;

window.getAlarms = getAlarms;
window.postAlarm = postAlarm;
window.updateAlarm = updateAlarm;
window.removeAlarm = removeAlarm;

window.getTracks = getTracks;
window.postTrack = postTrack;
window.updateTrack = updateTrack;
window.removeTrack = removeTrack;

