package com.ffii.lioner.modules.user.web;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    private final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private LdapTemplate ldapTemplate;

    @GetMapping("/test")
    public ResponseEntity<?> test(@RequestParam(defaultValue = "") String username) throws Exception {
        List<Map<String, String>> searchUser = ldapTemplate.search("", "(&(objectClass=*)(cn=*" + username + "*))",
                (AttributesMapper<Map<String, String>>) (attrs -> {
                    return Map.of("cn", attrs.get("cn").get().toString(), "mail", attrs.get("mail").get().toString());
                }));
        return ResponseEntity.ok(searchUser);
    }

}
