import ch.nevis.esauth.auth.engine.AuthResponse
import ch.nevis.idm.client.IdmRestClient
import ch.nevis.idm.client.IdmRestClientFactory
import ch.nevis.idm.client.HTTPRequestWrapper

import java.time.Duration

import groovy.json.JsonSlurper
import groovy.xml.XmlSlurper

def getHeader(String name) {
    def inctx = request.getLoginContext()
    // case-insensitive lookup of HTTP headers
    def map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER)
    map.putAll(inctx)
    return map['connection.HttpHeader.' + name]
}


// Accounting
def requester = session['ch.nevis.auth.saml.request.scoping.requesterId'] ?: 'unknown'
def requestId = session['ch.nevis.auth.saml.request.id'] ?: 'unknown'
def user = session['ch.adnovum.nevisidm.user.extId'] ?: 'unknown'
def sourceIp = request.getLoginContext()['connection.HttpHeader.X-Real-IP'] ?: 'unknown'
def userAgent = request.getLoginContext()['connection.HttpHeader.user-agent'] ?: request.getLoginContext()['connection.HttpHeader.User-Agent'] ?: 'unknown'

IdmRestClient idmRestClient = IdmRestClientFactory.get(parameters)

String clientExtId = session.get('ch.adnovum.nevisidm.user.clientExtId')
String userExtId = session.get('ch.adnovum.nevisidm.user.extId')
String mobile = session.get('ch.nevis.idm.User.mobile')

String baseUrl = parameters.get('baseUrl')
String endPoint = "${baseUrl}/core/v1/${clientExtId}/users/${userExtId}"


if (mobile) {
    LOG.debug("User '${user}' has already registered a mobile number")
    response.setResult('done')
    return
}

if (!inargs['submit'] && (!inargs['mobile'] || !inargs['mobile'].isEmpty()) && inargs['language'] && inargs['language'] != session['ch.nevis.session.user.language']) {
    // language switch, nothing else to do, just display again the GUI
    response.setStatus(AuthResponse.AUTH_CONTINUE)
    return
}

// TODO/haburger/2024-11-24: check/adapt value of skip checkbox
if (inargs['submit'] && (!inargs['mobile'] || inargs['mobile'].isEmpty()) && inargs['skip'] && inargs['skip'] == 'true') {
    // no mobile, and user wants to skip it

    LOG.info("Event='NOMOBILENUMBER', Requester='${requester}', RequestId='${requestId}', User=${user}, SourceIp=${sourceIp}, UserAgent='${userAgent}'")

    response.setCookie("testcookie", "testvalue", "/path", "Thu, 01 Jan 2025 00:00:00 GMT",
				Duration.ofDays(1), true, false);
    def agovSkipAskingMobileCookie = "agovSkipAskingMobile=true; Domain=${parameters.get('cookie.domain')}; Path=/; Max-Age=86400; SameSite=Strict; Secure; HttpOnly"
    response.setHeader('Set-Cookie', agovSkipAskingMobileCookie)
    response.setResult('done')
    return
}

def agovSkipAskingMobileCookie = 'missing'

if (getHeader('cookie') != null) {
    def cookies = getHeader('cookie')
    if (cookies.matches('^.*agovSkipAskingMobile=([^;]+).*$')) {
        agovSkipAskingMobileCookie = cookies.replaceAll('^.*agovSkipAskingMobile=([^;]+).*$', '$1')
    }
}
if (agovSkipAskingMobileCookie == 'true') {
    // Don't aske the user again...
    LOG.info("Event='SKIPPEDMOBILENUMBER', Requester='${requester}', RequestId='${requestId}', User=${user}, SourceIp=${sourceIp}, UserAgent='${userAgent}'")
    response.setResult('done')
    return
}


if (inargs['submit'] && inargs['mobile'] && !inargs['mobile'].isEmpty()) {
    String result

    def patchBdy = "{\"contacts\":{\"mobile\":\"${inargs['mobile']?.trim()}\"},\"modificationComment\":\"added mobile number from user during request ${requestId}\"}"
    try {
        result = idmRestClient.patch(endPoint, patchBdy)
    } catch(Exception e) {
        LOG.warn("Event='MOBILEFAILED', Requester='${requester}', RequestId='${requestId}', User=${user}, SourceIp=${sourceIp}, UserAgent='${userAgent}', reason='failed to save number (${e})'")
    }
    response.setResult('done')
    return
}


// we should ask the user
response.setStatus(AuthResponse.AUTH_CONTINUE)