
const config = require("../config.js")
    ,Slack = require("../src/slack.js").Slack
    ,GoogleOAuth = require("../src/googleOAuth.js").GoogleOAuth
    ,FacebookOAuth = require("../src/facebookOAuth.js").FacebookOAuth
    ,slackManager = require("../src/slackManager.js").SlackManager
    ,accountManager = require("../src/models/accounts.js").accountManager
    ,accountConfigManager = require("../src/models/accountConfig.js").accountConfigManager
    ,sessionManager = require("../src/session.js").SessionManager
    ,templates = require('./_templates.js');

function checkTokens(service, req, cb) {
    switch (service) {
        case "slack":
            if (req.urlObj.queryTokens.code) {
                Slack.getUserId(req.urlObj.queryTokens.code, config.login.slack.redirect_uri, (id) => {
                    if (id) {
                        accountManager.fromSlackIdAuth(id, (account) => {
                            if (account) {
                                cb(account);
                            } else if (config.allowNewAccounts) {
                                var account = accountManager.createAccount();
                                account.authSlackUserEmailAndTeam = id;
                                accountManager.save(account, () => {
                                    cb(account);
                                    accountConfigManager.save(accountConfigManager.newConfigFor(account), () => {});
                                });
                            } else {
                                cb(false);
                            }
                        });
                    } else {
                        cb(null);
                    }
                });
            } else {
                cb(null);
            }
        break;

        case "google":
            if (req.urlObj.queryTokens.code) {
                GoogleOAuth.getUserId(req.urlObj.queryTokens.code, (id) => {
                    if (id) {
                        accountManager.fromGoogleIdAuth(id, (account) => {
                            if (account) {
                                cb(account);
                            } else if (config.allowNewAccounts) {
                                var account = accountManager.createAccount();
                                account.authGoogleUserId = id;
                                accountManager.save(account, () => {
                                    cb(account);
                                    accountConfigManager.save(accountConfigManager.newConfigFor(account), () => {});
                                });
                            } else {
                                cb(false);
                            }
                        });
                    } else {
                        cb(null);
                    }
                });
            } else {
                cb(null);
            }
        break;

        case "facebook":
            if (req.urlObj.queryTokens.code) {
                FacebookOAuth.getUserId(req.urlObj.queryTokens.code[0], (id) => {
                    if (id) {
                        accountManager.fromFacebookIdAuth(id, (account) => {
                            if (account) {
                                cb(account);
                            } else if (config.allowNewAccounts) {
                                var account = accountManager.createAccount();
                                account.authFacebookUserId = id;
                                accountManager.save(account, () => {
                                    cb(account);
                                    accountConfigManager.save(accountConfigManager.newConfigFor(account), () => {});
                                });
                            } else {
                                cb(false);
                            }
                        });
                    } else {
                        cb(null);
                    }
                });
            } else {
                cb(null);
            }
        break;

        default:
            cb(null);
        break;
    }
}

function makeLoginPage() {
    const
    slackUri = config.login.slack ? (config.login.slack.requestLoginUri
        +"?client_id=" +config.login.slack.clientId
        +"&scope=" +slackManager.getAuthScope().join(',')
        +"&redirect_uri=" +config.login.slack.redirect_uri) : "",
    googleUri = config.login.google ? (config.login.google.requestLoginUri
        +"?client_id=" +config.login.google.clientId
        +"&scope=" +(["openid", "email", "profile"]).join("%20")
        +"&redirect_uri=" +config.login.google.redirect_uri
        +"&response_type=code") : "",
    facebookUri = config.login.facebook ? (config.login.facebook.requestLoginUri
        +"?client_id=" +config.login.facebook.clientId
        +"&redirect_uri=" +config.login.facebook.redirect_uri) : null;

    return templates.header("Mimou - login", ["login.css"])
        +`<div class="services"><h1>Login</h1>`
        +(googleUri ? `<a href="${googleUri}"><img src="${config.rootUrl}btn_google_connect.png"></a>` : "")
        +(facebookUri ? `<a href="${facebookUri}"><img src="${config.rootUrl}btn_facebook_connect.png"></a>` : "")
        +(slackUri ? `<a href="${slackUri}"><img src="${config.rootUrl}btn_slack_connect.png"></a>` : "")
        +`</div>`
        +templates.footer();
}

module.exports.match = function(url) {
    if (url.urlParts.length === 1) {
        return true;
    } else if (url.urlParts.length === 2 && Object.keys(config.login).indexOf(url.urlParts[1]) >= 0) {
        return true;
    }
    return false;
};

module.exports.exec = function(req, res, srv) {
    if (!req.urlObj.urlParts[1]) {
        res.end(makeLoginPage());
    } else {
        checkTokens(req.urlObj.urlParts[1], req, (account) => {
            if (account) {
                req.account = account;
                req.session = sessionManager.lazyForRequest(req);
                req.session.setAccountId(req.reqT, account.id);
                res.writeHeader("302", {
                    Location: config.rootUrl,
                    "Set-Cookie": "sessID="+req.session.sessId +'; Path=/'
                });
                sessionManager.saveSession(req.session);
                res.end();
            } else if (account === null) {
                res.end(makeLoginPage());
            } else {
                srv.execTemplate(require("./_403.js"), req, res);
            }
        });
    }
};

module.exports.needLogin = false;

