'use strict'

export class FireBaseAuth{
    constructor(config){
      
        let uiConfig = {
            'callbacks': {
                // Called when the user has been successfully signed in.
                'signInSuccess': function(user, credential, redirectUrl) {
                    console.info(user);
                //handleSignedInUser(user);
                    // Do not redirect.
                    return false;
                }
            },
            // Opens IDP Providers sign-in flow in a popup.
            'signInFlow': 'popup',
            'signInOptions': [
                // TODO(developer): Remove the providers you don't need for your app.
                {
                provider: firebase.auth.GoogleAuthProvider.PROVIDER_ID,
                scopes: ['https://www.googleapis.com/auth/plus.login']
                }/*,
                {
                provider: firebase.auth.FacebookAuthProvider.PROVIDER_ID,
                scopes :[
                    'public_profile',
                    'email',
                    'user_likes',
                    'user_friends'
                ]
                }*/,
                firebase.auth.TwitterAuthProvider.PROVIDER_ID,
                firebase.auth.GithubAuthProvider.PROVIDER_ID,
                firebase.auth.EmailAuthProvider.PROVIDER_ID
            ],
            // Terms of service url.
            'tosUrl': 'https://gdgnantes.com'
        };
        this.ui = new firebaseui.auth.AuthUI(firebase.auth());
        this.ui.start('#firebaseui-auth-container', uiConfig);
        this.user = null;
        this.idDivLogin = config.idDivLogin;
        this.idNextDiv = config.idNextDiv;
        this.idLogout = config.idLogout;

        // Optionals
        this.idImg = config.idImg ? config.idImg : null;
        this.idDisplayName = config.idDisplayName ? config.idDisplayName : null;


        firebase.auth().onAuthStateChanged(this._checkCallBackContext.bind(this), 
                                        this._checkCallBackErrorContext.bind(this)
                                        );

        this.cbAuthChanged = null;

        document.getElementById(this.idLogout).addEventListener('click', ()=>  firebase.auth().signOut());
    }

    _checkCallBackErrorContext(error){
        console.error(error);
    }

    _checkCallBackContext(user){
        this.user = user;
        if (user){
            document.getElementById(this.idDivLogin).setAttribute("hidden","");
            document.getElementById(this.idNextDiv).removeAttribute('hidden');
            document.getElementById(this.idLogout).removeAttribute("hidden");            
            if (this.idImg){
                document.getElementById(this.idImg).src = user.photoURL;
                document.getElementById(this.idImg).removeAttribute('hidden');                
            }
            if (this.idDisplayName){
                document.getElementById(this.idDisplayName).innerHTML = user.displayName;;                
            }
        }else{
            document.getElementById(this.idDivLogin).removeAttribute("hidden","");
            document.getElementById(this.idNextDiv).setAttribute("hidden","");
            document.getElementById(this.idLogout).setAttribute("hidden","");
            document.getElementById(this.idImg).src = "";
            document.getElementById(this.idImg).setAttribute('hidden', "");
            document.getElementById(this.idDisplayName).innerHTML = "Non Conntecté";            

        }
        if(this.cbAuthChanged){
            this.cbAuthChanged(user);
        }
        /*if (user) {
            user.getToken().then(function(accessToken) {
                console.info(accessToken);
            });
        } else {
            // User is signed out.
            console.info('signed out');            
        }*/
    }

    onAuthStateChanged(cb){
        this.cbAuthChanged = cb;
    }


    displayName(){
        return this.user ? this.user.displayName : null;
    }

    userId(){
        return this.user ? this.user.uid : null;
    }
}
@@ -270,92 +270,8 @@
 ) %7B%0A
-                    console.info(user);%0A                //handleSignedInUser(user);%0A
     
@@ -306,16 +306,16 @@
 direct.%0A
+
         
@@ -503,94 +503,8 @@
 : %5B%0A
-                // TODO(developer): Remove the providers you don't need for your app.%0A
     
@@ -660,16 +660,16 @@
 login'%5D%0A
+
         
@@ -681,303 +681,8 @@
    %7D
-/*,%0A                %7B%0A                provider: firebase.auth.FacebookAuthProvider.PROVIDER_ID,%0A                scopes :%5B%0A                    'public_profile',%0A                    'email',%0A                    'user_likes',%0A                    'user_friends'%0A                %5D%0A                %7D*/
 ,%0A  
@@ -3135,255 +3135,8 @@
     
-  /*if (user) %7B%0A            user.getToken().then(function(accessToken) %7B%0A                console.info(accessToken);%0A            %7D);%0A        %7D else %7B%0A            // User is signed out.%0A            console.info('signed out');            %0A        %7D*/
 %0A   

clean class