// Initialize Firebase
var config = {
  apiKey: "AIzaSyCuvwf78cmSDoZ2yS4XxHZhnjUn7yIHYfw",
  authDomain: "test-667ca.firebaseapp.com",
  databaseURL: "https://test-667ca.firebaseio.com",
  projectId: "test-667ca",
  storageBucket: "test-667ca.appspot.com",
  messagingSenderId: "221332577314"
};
firebase.initializeApp(config);


function checkAuth() {
  firebase.auth().onAuthStateChanged(async function (user) {
    if (user) {
      try {
        firebase.auth().currentUser.getIdToken( /* forceRefresh */ true).then(function (idToken) {
          console.log("Vor Replace");

          window.location.replace("/dash/" + idToken);
        }).catch(function (error) {
          console.error("Get id token client error: ", error)
        });
      } catch {
        console.error("checkAuth error: ");
      }

      // document.getElementById("loginbutton").style.display = "none";
      // 
    } else {
      // document.getElementById("loginbutton").style.display = "block";
      console.log("Check Auth error", user)
    }
  });
}
window.onload = function () {
  checkAuth();
}



var vue = new Vue({
  el: '#vue-app',
  data: {
    email: "",
    password: ""
  },
  methods: {
    login() {
      var provider = new firebase.auth.GoogleAuthProvider();
      provider.addScope('profile');
      provider.addScope('email');
      firebase.auth().signInWithPopup(provider).then(function (result) {
        // This gives you a Google Access Token. You can use it to access the Google API.
        var token = result.credential.accessToken;
        var idToken = result.credential.idToken;
        // The signed-in user info.
        var user = result.user;
        console.log("Eingelogt");
        checkAuth();
      }).catch(function (error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;

      });
    },
    loginemail() {
      firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(result => {
        checkAuth();
      }).catch(function (error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // ...
      });
    }

  },

  mounted() {
    /*axios.post('/user', {
        message_id: 'test',
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });*/
  }

  // mounted() {
  //     axios.post('/user', {
  //         message_id: 'test',
  //       })
  //       .then(function (response) {
  //         console.log(response);
  //       })
  //       .catch(function (error) {
  //         console.log(error);
  //       });
  // }

});