//
//  FirebaseService.swift
//  Vendoo
//
//  Created by Okechi Onyeje on 6/2/16.
//  Copyright © 2016 Okechi Onyeje. All rights reserved.
//

import Foundation
import Firebase
import Locksmith
import FirebaseStorage
import TYMActivityIndicatorView


class FirebaseManager: NSObject {
    
    let ref = FIRDatabase.database().reference()
    let user_email = ((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!).stringByReplacingOccurrencesOfString(".", withString: "_")
    let storage = FIRStorage.storage()
    let indicator = TYMActivityIndicatorView.init(activityIndicatorStyle: (TYMActivityIndicatorViewStyle.Normal))
    //let key: String!
    
    override init(){
        super.init()

        
        
        //check if user has been created
        self.ref.child("Users").observeSingleEventOfType(.Value, withBlock: { snapshot in
            // do some stuff once
            
            if(!snapshot.hasChild(self.user_email)){
                let user = ["user_Listings": "","listing_Keys": "","favorite_listings_keys": ""]
                // let userUpdates = ["/Users/\((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!)" : user]
                self.ref.child("Users").child(self.user_email).setValue(user)
                
                //self.ref.child("Users").setValue(["email": (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!])
            }
        })
        
        
        
    }
    
}



//string adjustment functions
extension FirebaseManager {
    func adjustUserEmail(aString: String) -> String {
        return aString.stringByReplacingOccurrencesOfString(".", withString: "_")
    }
}
