<%
include("constants.jag");

var log = new Log();
var identityServerURL = Packages.org.wso2.carbon.identity.core.util.IdentityUtil;

/*
 * Function to update server url information
 */
function setServerURL() {
    var serverUrl = stringify(application.get(SERVER_URL));
    if(serverUrl === 'null' || serverUrl == null || serverUrl.length <= 0){
        //Server URL is not specified in the jaggery.conf, therefore using the service url mentioned in the carbon.conf
        var carbon = require('carbon');
        var serverConfigService = carbon.server.osgiService('org.wso2.carbon.base.api.ServerConfigurationService');

        var path = "/conf/site.json";
        var file = new File(path);
        file.open("r");
        var json = file.readAll();
        file.close();

        var jsonConf = parse(json);
        var serverCoreUrl = '';

        var mgtTransport = "https";
        var fidoAppId = jsonConf.fido.appId;
        var enableWebAuthn = jsonConf.fido.enableWebAuthn;
        //retrieving site configuration for overiding ui urls
        var proxyConfHost = jsonConf.proxy.proxyHost;
        var proxyConfPort = jsonConf.proxy.proxyHTTPSPort;
        var proxyOrigin = '';
        var proxyContextPath = '';

        if ((proxyConfHost == null || proxyConfHost === '' || proxyConfHost === 'null' || proxyConfHost.length <= 0)
            || (proxyConfPort == null || proxyConfPort === '' || proxyConfPort === 'null' || proxyConfPort.length <=
                0)) {
            //overriding carbon proxy settings configuration not available for ui urls
            proxyOrigin = identityServerURL.getServerURL("", false, false);
            if (log.isDebugEnabled()) {
                log.debug("Carbon proxy settings available in carbon.xml- Proxy Origin Name: " + proxyOrigin);
            }
        } else {
            proxyOrigin = mgtTransport + "://" + proxyConfHost + ":" + proxyConfPort;
            //proxy settings available for ui urls
            if (log.isDebugEnabled()) {
                log.debug("site configuration ui url overiding available - Proxy Origin Name: " + proxyOrigin);
            }
        }

        var proxyConfContextPath = jsonConf.proxy.proxyContextPath;
        if (proxyConfContextPath == null || proxyConfContextPath === '' || proxyConfContextPath === 'null' ||
            proxyConfContextPath.length <= 0) {
            //overriding carbon proxy settings configuration not available for ui urls
            proxyContextPath = serverConfigService.getFirstProperty(PROXY_CONTEXT_PATH);
        } else {
            //proxy settings available for ui urls
            if (log.isDebugEnabled()) {
                log.debug("site configuration ui url overiding available - Proxy Context Path : " +
                    proxyConfContextPath);
            }
            proxyContextPath = proxyConfContextPath;
        }
        if (proxyContextPath === 'null' || proxyContextPath == null || proxyContextPath === '' || proxyContextPath
            .length <= 0) {
            proxyContextPath = "";
        } else {
            if (proxyContextPath.indexOf('/') != 0) {
                proxyContextPath = '/' + proxyContextPath;
            }
        }

        var serviceOrigin = '';
        if (jsonConf.service == undefined) {
            serviceOrigin = '';
        } else if (jsonConf.service.serviceOrigin == undefined) {
            serviceOrigin = '';
        } else {
            serviceOrigin = jsonConf.service.serviceOrigin;
        }
        if ( serviceOrigin == null || serviceOrigin === '' || serviceOrigin === 'null' || serviceOrigin.length <= 0 ){
            serverCoreUrl = identityServerURL.getServerURL("", true, false);
            serverUrl = identityServerURL.getServerURL("services", true, true);
        } else {
            serverCoreUrl = identityServerURL.fillURLPlaceholders(serviceOrigin);
            serverUrl = serverCoreUrl + "/services";
        }

        application.put(MGT_TRANSPORT, mgtTransport);
        application.put(SERVER_URL, serverUrl);
        application.put(SERVER_CORE_URL, serverCoreUrl);
        application.put(FIDO_APP_ID, fidoAppId);
        application.put(PROXY_CONTEXT_PATH, proxyContextPath);
        application.put(PROXY_ORIGIN, proxyOrigin);
        application.put(ENABLE_WEBAUTHN, identityServerURL.getProperty("FIDO.WebAuthn.Enable"));

        if (log.isDebugEnabled()) {
            log.debug("Proxy Origin      :" + proxyOrigin);
            log.debug("Server Url      :" + serverUrl);
            log.debug("Server Core Url :" + serverCoreUrl);
            log.debug("Proxy Context Path :" + proxyContextPath);
            application.put(ENABLE_WEBAUTHN, identityServerURL.getProperty("FIDO.WebAuthn.Enable"));
        }
    }
}

function getFidoAppId(){
    setServerURL();
    return application.get(FIDO_APP_ID);
}

function getFIDOMetaDataUrl() {

    return getFIDOContext() + FIDO_METADATA_URI;
}

function getFIDOStartRegUrl() {

    return getFIDOContext() + FIDO_START_REGISTRATION_URI;
}

function getFIDOFinishRegUrl() {

    return getFIDOContext() + FIDO_FINISH_REGISTRATION_URI;
}

function getFIDORemoveUrl() {

    return getFIDOContext() + FIDO_DEVICE_REMOVE_URI;
}

function getFIDOContext() {
    var carbon = require('carbon');
    var tenantUser = carbon.server.tenantUser(user);

    return application.get(SERVER_CORE_URL) + "/t/" + tenantUser.domain;
}

%>