<%

var log = new Log();
var userName = session.get("user");
var identityClient = require('identityMgtClient.jag');

var multitenantUtils = Packages.org.wso2.carbon.utils.multitenancy.MultitenantUtils;

var i18n = session.get("i18n");

function edit() {


    var tenantAwareUserName = multitenantUtils.getTenantAwareUsername(userName);
    var tenantDomain = multitenantUtils.getTenantDomain(userName);


    try {
        var setIDs = request.getParameter("qid[]");
        if (setIDs != null && setIDs.constructor != Array) {
            setIDs = [setIDs];
        }
        log.debug("Available challenge question set IDs");
        log.debug(setIDs);

        var user = <ser:user xmlns:ser="http://services.recovery.identity.carbon.wso2.org"
                             xmlns:xsd="http://model.common.application.identity.carbon.wso2.org/xsd">
            <xsd:userName>{0}</xsd:userName>"+
            <xsd:tenantDomain>{1}</xsd:tenantDomain>"+
        </ser:user>

        user.*::userName.setChildren(tenantAwareUserName);
        user.*::tenantDomain.setChildren(tenantDomain);

        var req = <ser:setUserChallengeAnswers xmlns:ser="http://services.recovery.identity.carbon.wso2.org">
        </ser:setUserChallengeAnswers>;

        // append user object ot the request
        req.appendChild(user);

        for (var i = 0; i < setIDs.length; i++) {
            var count = setIDs[i];
            var questionSetId = request.getParameter("challengeId_" + count);
            var questionText = request.getParameter("question_" + count);
            var locale = request.getParameter("locale_" + count);
            var ans = request.getParameter("ans_" + count);

            if (ans != null && ans.length > 0) {
                var child = <ser:userChallengeAnswers xmlns:xsd="http://model.recovery.identity.carbon.wso2.org/xsd"
                                                      xmlns:ser="http://services.mgt.identity.carbon.wso2.org">
                    <xsd:answer>ans</xsd:answer>
                </ser:userChallengeAnswers>;
                child.*::answer.setChildren(ans);

                var challengeQuestion = <ser:question xmlns:xsd="http://model.recovery.identity.carbon.wso2.org/xsd"
                                                      xmlns:ser="http://services.mgt.identity.carbon.wso2.org">
                    <xsd:question>questionText</xsd:question>
                    <xsd:questionSetId>questionSetId</xsd:questionSetId>
                    <xsd:questionId>questionId</xsd:questionId>
                    <xsd:locale>locale</xsd:locale>
                </ser:question>;

                challengeQuestion.*::question.setChildren(questionText);
                challengeQuestion.*::questionSetId.setChildren(questionSetId);
                challengeQuestion.*::questionId.setChildren(null);
                challengeQuestion.*::locale.setChildren(locale);

                // append challenge question payload to challenges element
                child.appendChild(challengeQuestion);
                req.appendChild(child);

            }
        }
        log.info("Sending challenge question answer response to backend.")
        identityClient.setUserChallengeAnswers(req);
        // response.sendRedirect("../../account_recovery.jag?s=1&sMsg=updated_acc_rec_q");
    }
    catch (e) {
        log.error(e);
        message({
            content: 'Error While updating recovery questions', type: 'error', cbk: function () {
            }
        });
        console.log('error');
        response.sendRedirect("../../index.jag?e=1&error=" + e);
    }

}

edit();


%>
