<%@ taglib uri="/struts-tags" prefix="ww" %>
<ww:set name="isCreateDirectory">${param.isCreateDirectory}</ww:set>
<ww:set name="isConnector">${param.isConnector}</ww:set>
<ww:property value="webResourceManager.requireResource('crowd.web.resources:extract-error-message')"/>
function testSearch(strategy, button) {
    var $testSearchButton = AJS.$(button);
    $testSearchButton.prop("disabled", true);
    var $testSearchSpinner = $testSearchButton.next();
    $testSearchSpinner.spin();
    var data = {
        <ww:if test="isCreateDirectory">
        "URL": AJS.$("#URL").val(),
        "secure": AJS.$("#secure").prop("checked"),
        "referral": AJS.$("#referral").prop("checked"),
        "baseDN": AJS.$("#baseDN").val(),
        "userDN": AJS.$("#userDN").val(),
        "ldapPassword": AJS.$("#ldapPassword").val(),
        "userEncryptionMethod": AJS.$("#userEncryptionMethod").val(),
        "connector": AJS.$("#connector").val(),
        "useUserMembershipAttribute": AJS.$("#useUserMembershipAttribute").prop("checked"),
        "pagedResults": AJS.$("#pagedResults").prop("checked"),
        "pagedResultsSize": AJS.$("#pagedResultsSize").val(),
        </ww:if>
        <ww:if test="isCreateDirectory && isConnector">
        "filterExpiredUsers": AJS.$("#filterExpiredUsers").prop("checked"),
        "primaryGroupSupport": AJS.$("#primaryGroupSupport").prop("checked"),
        "useRelaxedDNStandardisation": AJS.$("#useRelaxedDNStandardisation").prop("checked"),
        "useUserMembershipAttributeForGroupMembership": AJS.$("#useUserMembershipAttributeForGroupMembership").prop("checked"),
        "userPasswordAttr": AJS.$("#userPasswordAttr").val(),
        </ww:if>

        "strategy": strategy,
        "groupDNaddition": AJS.$("#groupDNaddition").val(),
        "groupDescriptionAttr": AJS.$("#groupDescriptionAttr").val(),
        "groupMemberAttr": AJS.$("#groupMemberAttr").val(),
        "groupNameAttr": AJS.$("#groupNameAttr").val(),
        "groupObjectClass": AJS.$("#groupObjectClass").val(),
        "groupObjectFilter": AJS.$("#groupObjectFilter").val(),

        "userDNaddition": AJS.$("#userDNaddition").val(),
        "userObjectClass": AJS.$("#userObjectClass").val(),
        "userGroupMemberAttr": AJS.$("#userGroupMemberAttr").val(),
        "userFirstnameAttr": AJS.$("#userFirstnameAttr").val(),
        "userLastnameAttr": AJS.$("#userLastnameAttr").val(),
        "userDisplayNameAttr": AJS.$("#userDisplayNameAttr").val(),
        "userMailAttr": AJS.$("#userMailAttr").val(),
        "userNameAttr": AJS.$("#userNameAttr").val(),
        "userNameRdnAttr": AJS.$("#userNameRdnAttr").val(),
        "userObjectFilter": AJS.$("#userObjectFilter").val(),
        "userExternalIdAttr": AJS.$("#userExternalIdAttr").val()
    };
    AJS.$.ajax({
        url: AJS.contextPath() + "/rest/directorymanagement/1/directory/testsearch/${param.directoryId}",
        type: "POST",
        data: JSON.stringify(data),
        dataType: 'json',
        contentType: 'application/json'
    }).done(function() {
        require(['aui/flag'], function(flag) {
            var myFlag = flag({
                type: 'success',
                title: '<ww:property value="getText('directorycrowd.testconnection.flag.success.title')"/>',
                body: '<div class="aui-message-wrapped-body"><ww:property value="getText('directoryconnector.testsearch.success')"/></div>'
            });
            setTimeout(function() {
                myFlag.close();
            }, 5000);
        });
    }).fail(function(jqxhr) {
        require(['aui/flag'], function (flag) {
            flag({
                type: 'error',
                title: '<ww:property value="getText('directorycrowd.testconnection.flag.failure.title')"/>',
                body: '<div class="aui-message-wrapped-body"><ww:property value="getText('directoryconnector.testsearch.invalid')"/>' + " " + Crowd.extractErrorMessage(jqxhr.responseText) + "</div>"
            });
        });
    }).always(function() {
        $testSearchButton.prop("disabled", false);
        $testSearchSpinner.spinStop();
    });
}

function wireTestSearchButtons() {
    AJS.$("#test-search-principal").click(function() {
        testSearch("USER", this);
    });
    AJS.$("#test-search-group").click(function() {
        testSearch("GROUP", this);
    });
}

AJS.$(wireTestSearchButtons);