import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { FirebaseListObservable } from 'angularfire2/database-deprecated';
import * as firebase from 'firebase';
import 'rxjs/add/operator/toPromise';

// import { Router } from '@angular/router';

@Injectable()
export class fireService {

  items: FirebaseListObservable<any[]>;
  //data: FirebaseObjectObservable<any[]>;
  data:any;
  currentUser: any;
  userData: any;

  // private basePath: string = 'users';
  
  
  constructor(
                private db: AngularFireDatabase) {
  }

  pushItem(url,data){
      var key = firebase.database().ref(url).push().key;
      var new_node = url + "/" + key;
      data.push_id = key;
      this.updateItem(new_node, data)
    return key;
  }
  
  updateItem(url,data){
    //  return this.db.object(url).update(data);
      var promise = new Promise((resolve, reject) => {
          if (this.db.object(url).update(data)) {
              // console.log("Updated");
              resolve({ 'status': 'success' });
          } else {
              reject();
          }
      });
      return promise;
  }
}

