// @flow
import semver from 'semver';
import type { Error } from './Types';

export default class VersionManager {

  // TODO: Read version from package.json and URL from properties
  static current = '0.0.1';

  static url = 'https://s3.amazonaws.com/reddyapp/version.json';

  static checkVersion(onUpdateRequired: () => void, onError: (error: Error) => void) {
    fetch(this.url).then(response =>
      response.json()
    ).then(json => {
      const version = json.version;
      if (semver.gt(version, this.current)) {
        onUpdateRequired();
      }
      return version;
    }).catch(() => {
      onError({
        message: 'Unable to check for updates'
      });
    });
  }
}
