//
//  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()
    var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    
    //let key: String!
    
    override init(){
        super.init()
        indicator.color = UIColor.blueColor()
        //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 = ["name": (NSUserDefaults.standardUserDefaults().objectForKey("name") as? String)!, "user_Listings": "", "listing_Keys": "", "favorite_listings_keys": ""]
                self.ref.child("Users").child(self.user_email).setValue(user)
                
            }
        })
        
        
        
    }
    
}



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