import firebase from "firebase";
var config = {
    apiKey: "AIzaSyCa72mnBBhot94hMfGPvI4K5X056jODtwU",
    authDomain: "autyzm-d2f85.firebaseapp.com",
    databaseURL: "https://autyzm-d2f85.firebaseio.com",
    projectId: "autyzm-d2f85",
    storageBucket: "autyzm-d2f85.appspot.com",
    messagingSenderId: "1083503973985"
};

firebase.initializeApp(config);
export default class FirebaseAPI {
    static  database = firebase.database();
    // TODO:
    // static database_ref = FirebaseAPI.database.ref();
    static uid = localStorage.getItem("uid");
    static write(json,key='') {
		if(key == ''){
			key = FirebaseAPI.uid
		}
        console.log("save " + FirebaseAPI.uid);
        var x = FirebaseAPI.database.ref('results/' + key).set(json);
        console.log(x);
        
    }
    static auth() {
        firebase.auth().signInAnonymously().catch(function (error) {
            var errorCode = error.code;
            var errorMessage = error.message;
            console.error("cannot auth" + errorCode + "  " + errorMessage);
        });
        firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                // console.log("auth" + JSON.stringify(user))
                console.log("success auth")
                var isAnonymous = user.isAnonymous;
                if (FirebaseAPI.uid+"" == user.uid+""){
                 /*   var x = FirebaseAPI.database.ref('counter').set({counter:0});
        console.log(x);
           var x = FirebaseAPI.database.ref('counter').set({counter:0});
        console.log(x);*/
                } 
                FirebaseAPI.uid = user.uid;
                
                localStorage.setItem("uid", user.uid);
            } else {
                //alert("test");
                console.log("user is logged out")
            }
        }); 
       
    }
}


// Initialize Firebase
       