/*---------------------------------------------------------------------------*/
/* innovaphone.smtp.oauth2.js                                                */
/* A collection of helper functions for SMTP with OAuth2                     */
/*---------------------------------------------------------------------------*/

var innovaphone = innovaphone || {};
innovaphone.SMTP = innovaphone.SMTP || {};
innovaphone.SMTP.OAuth2 = innovaphone.SMTP.OAuth2 || function () {

    function getValues() {
        return ["off", "exchange", "microsoft365", "gmail", "google", "client-secret-post", "private-key-jwt"];
    }

    function getTexts() {
        return ["Off", "Exchange (smtp.office365.com:587)", "Microsoft 365 (smtp.office365.com:587)", "Gmail (smtp.gmail.com:587)", "Google Service Account (smtp.gmail.com:587)", "Client secret post", "Private key jwt"];
    }

    function getDefaultServer(oauth2) {
        return (((oauth2 == "exchange") || (oauth2 == "microsoft365")) ? "smtp.office365.com:587" :
            (((oauth2 == "gmail") || (oauth2 == "google")) ? "smtp.gmail.com:587" : undefined));
    }

    function isMethodPrivateKeyJWT(oauth2) {
        return ((oauth2 == "google") || (oauth2 == "private-key-jwt"));
    }

    function isMethodClientSecretPost(oauth2) {
        return ((oauth2 != "off") && !isMethodPrivateKeyJWT(oauth2));
    }

    function needsClientID(oauth2) {
        return (isMethodClientSecretPost(oauth2));
    }

    function needsClientSecret(oauth2) {
        return (isMethodClientSecretPost(oauth2));
    }

    function needsOwnerUsername(oauth2) {
        return (isMethodClientSecretPost(oauth2));
    }

    function needsOwnerPassword(oauth2) {
        return ((oauth2 == "microsoft365") || (oauth2 == "client-secret-post"));
    }

    function needsClientEMail(oauth2) {
        return (isMethodPrivateKeyJWT(oauth2));
    }

    function needsPrivateKeyID(oauth2) {
        return (isMethodPrivateKeyJWT(oauth2));
    }

    function needsPrivateKey(oauth2) {
        return (isMethodPrivateKeyJWT(oauth2));
    }

    function needsTenant(oauth2) {
        return ((oauth2 == "exchange") || (oauth2 == "microsoft365"));
    }

    function needsTokenEndpoint(oauth2) {
        return ((oauth2 == "client-secret-post") || (oauth2 == "private-key-jwt"));
    }

    function needsScope(oauth2) {
        return ((oauth2 == "client-secret-post") || (oauth2 == "private-key-jwt"));
    }

    function needsAuthorizationURL(oauth2) {
        return (oauth2 == "client-secret-post");
    }

    function needsAuthorize(oauth2) {
        return ((oauth2 == "exchange") || (oauth2 == "gmail") || (oauth2 == "client-secret-post"));
    }

    function getAuthorizationURL(oauth2, redirectURI, clientID, ownerUsername, tenant, tokenEndpoint, scope, authorizationURL) {
        var url;
        if (oauth2 == "exchange") {
            url = "https://login.microsoftonline.com/" + tenant + "/oauth2/v2.0/authorize" +
                "?scope=https://outlook.office.com/SMTP.Send offline_access" +
                "&state=0%0Ahttps://login.microsoftonline.com/" + tenant + "/oauth2/v2.0/token%0A" + clientID + "%0A" + encodeURIComponent(ownerUsername) + "%0Ahttps://outlook.office.com/.default";
        }
        else if (oauth2 == "gmail") {
            url = "https://accounts.google.com/o/oauth2/auth" +
                "?access_type=offline" +
                "&scope=https://mail.google.com/" +
                "&state=0%0Ahttps://oauth2.googleapis.com/token%0A" + clientID + "%0A" + encodeURIComponent(ownerUsername) + "%0Ahttps://mail.google.com/";
        }
        else {
            url = authorizationURL +
                "&state=0%0A" + tokenEndpoint + "%0A" + clientID + "%0A" + encodeURIComponent(ownerUsername) + "%0A" + scope;
        }
        url += "&response_type=code" +
            "&prompt=consent" +
            "&login_hint=" + encodeURIComponent(ownerUsername) +
            "&redirect_uri=" + redirectURI +
            "&client_id=" + clientID;
        return (url);
    }

    // public interface
    return {
        getValues: getValues,
        getTexts: getTexts,
        getDefaultServer: getDefaultServer,
        needsClientID: needsClientID,
        needsClientSecret: needsClientSecret,
        needsOwnerUsername: needsOwnerUsername,
        needsOwnerPassword: needsOwnerPassword,
        needsClientEMail: needsClientEMail,
        needsPrivateKeyID: needsPrivateKeyID,
        needsPrivateKey: needsPrivateKey,
        needsTenant: needsTenant,
        needsTokenEndpoint: needsTokenEndpoint,
        needsScope: needsScope,
        needsAuthorizationURL: needsAuthorizationURL,
        needsAuthorize: needsAuthorize,
        getAuthorizationURL: getAuthorizationURL
    };
}
