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,) {
// console.log(this.currentUser);
//       console.log('hhhh');
  }

  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 'success';
  }
  
  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;
  }
  removeItem(url){
     return this.db.object(url).remove();
  }
  
  listItem(url){
    return  this.db.list(url);
      
  }
  listItemQuery(url,query){
    return  this.db.list(url,query);
      
  }

  checkItem(url){
    return this.db.list(url);
  }
}

