var AADLogin = (function () {

    AADLogin = function () { }

    AADLogin.prototype.init = function (_clientId) {
        //https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devhowto-multi-tenant-overview
        this.config = {
            tenant:'common',
            clientId: _clientId, //'d05cd722-a9e2-4f92-b19f-ba6d90353d92',
            popUp: false,
            isAngular: false,
            cacheLocation: 'localStorage'
        };

        this.authContext = new AuthenticationContext(this.config);
        
        // Check For & Handle Redirect From AAD After Login
        var isCallback = this.authContext.isCallback(window.location.hash);
        //this.authContext.getLoginError()


        if(isCallback && !this.authContext.getLoginError())
        {
            //call back and successfull login
            this.authContext.handleWindowCallback();
        }
        else if (isCallback && this.authContext.getLoginError())
        {
            //call back and login failure
            Console.log(this.authContext.getLoginError());
        }

        return isCallback;
    }

    AADLogin.prototype.login = function ()
    {
        this.authContext.login();
    }

    AADLogin.prototype.logout = function () {
        this.authContext.logOut();
    }

    AADLogin.prototype.getUserInfo = function ()
    {
        return this.authContext.getCachedUser();
    }

    AADLogin.prototype.getToken = function ()
    {
        return this.authContext._extractIdToken(this.authContext._getItem(this.authContext.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY+this.config.clientId));
    }
    
    return AADLogin;
}());
