/**
 * update the installation object using REST API and masterKey
 *
 * @param {Object} installation, the JavaScript object for the record
 */
function updateInstallation(installation, user, done) {
  const errorMsg = `Could not update installation object: ${installation.objectId}`;
  const parseVersion = Parse.CoreManager.get('VERSION');
  const update = {
    appName: name,
    appVersion: version,
    parseVersion: parseVersion,
    userId: user.id,
  };
  let runCount = installation.runCount;
  if (typeof runCount === 'undefined') {
    runCount = 0;
  }
  if (installation.platform === 'android') {
    update.pushType = 'gcm';
  }
  if (installation.platform === 'ios') {
    update.badge = 0;
  }
  update.runCount = ++runCount;
  const authConfig = {
    method: 'PUT',
    headers: {
      'X-Parse-Application-Id': Settings.parse.appId,
      'X-Parse-Master-Key': Settings.parse.masterKey,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(update),
  };
  const url = `${Settings.parse.serverUrl}/installations/${installation.objectId}`;
  fetch(url, authConfig).then((res) => {
    if (!res.ok) {
      done(errorMsg);
      return;
    }
    return res.text();
  }).then((responseText) => {
    let jsonData = {};
    try {
      jsonData = JSON.parse(responseText);
    } catch (e) {
      done(e);
      return;
    }
    if (!jsonData.hasOwnProperty('updatedAt')) {
      done(errorMsg);
      return;
    }
    done(null, jsonData);
  }).catch((e) => {
    if (e.hasOwnProperty('message')) {
      done(e.message);
      return;
    }
    done(errorMsg);
  });
}
/**
 * update the installation object using REST API and masterKey
 *
 * @param {Object} installation, the JavaScript object for the record
 */
function createInstallation(installationId, user, tokenId, platform, done) {
  const errorMsg = `Could not create installation object with installationId: ${installationId}`;
  const parseVersion = Parse.CoreManager.get('VERSION');
  const installation = {
    appName: name,
    appVersion: version,
    appIdentifier: Settings.identifier,
    deviceToken: tokenId,
    deviceType: platform,
    installationId: installationId,
    parseVersion: parseVersion,
    runCount: 1,
    userId: user.id,
  };
  if (platform === 'android') {
    installation.pushType = 'gcm';
  }
  if (platform === 'ios') {
    installation.badge = 0;
  }
  const authConfig = {
    method: 'POST',
    headers: {
      'X-Parse-Application-Id': Settings.parse.appId,
      'X-Parse-Master-Key': Settings.parse.masterKey,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(installation),
  };
  const url = `${Settings.parse.serverUrl}/installations`;
  fetch(url, authConfig).then((res) => {
    if (!res.ok) {
      done(errorMsg);
      return;
    }
    return res.text();
  }).then((responseText) => {
    let jsonData = {};
    try {
      jsonData = JSON.parse(responseText);
    } catch (e) {
      done(e);
      return;
    }
    if (Object.keys(jsonData).length <= 0) {
      done(errorMsg);
      return;
    }
    done(null, jsonData);
  }).catch((e) => {
    if (e.hasOwnProperty('message')) {
      done(e.message);
      return;
    }
    done(errorMsg);
  });
}