/**
 * Created by 322 on 01/04/2017.
 */
import * as firebase from "firebase";
import {observable, action, computed} from 'mobx';

export function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

  // Windows Phone must come first because its UA also contains "Android"
  if (/windows phone/i.test(userAgent)) {
    return "Windows Phone";
  }

  if (/android/i.test(userAgent)) {
    return "Android";
  }

  // iOS detection from: http://stackoverflow.com/a/9039885/177710
  if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
    return "iOS";
  }

  return "unknown";
}

export class Firebase {

  @observable tokenMessage = '';

  constructor(context) {
    this.http = context.http;
    // if(getMobileOperatingSystem() !== 'iOS') {
      this.init();
    // }
  }

  init() {
    const config = {
      apiKey: "AIzaSyDhh7woZMxSNXmilAmmxRpOze4YatZbgjA",
      authDomain: "cerdas-fc4a2.firebaseapp.com",
      databaseURL: "https://cerdas-fc4a2.firebaseio.com/",
      projectId: "cerdas-fc4a2",
      storageBucket: "cerdas-fc4a2.appspot.com",
      messagingSenderId: "192592024837"
    };
    firebase.initializeApp(config);
    const messaging = firebase.messaging();
    messaging.requestPermission()
      .then(function() {
        console.log('Notification permission granted.');
        // TODO(developer): Retrieve an Instance ID token for use with FCM.
        // ...
      })
      .catch(function(err) {
        console.log('Unable to get permission to notify.', err);
      });

    
  }

}
