<%#
 Copyright 2013-2024 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.service.mapper;

<%_
const manyListName = ormTool !== 'mybatis' ? 'Set' : 'List';
const manyListType = ormTool !== 'mybatis' ? 'LinkedHashSet' : 'ArrayList';
_%>
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j) { _%>
import <%= packageName %>.domain.Authority;
import <%= packageName %>.service.dto.AuthorityDTO;
import <%= packageName %>.repository.AuthorityRepository;
<%_ } _%>
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.service.dto.<%= user.adminUserDto %>;
import <%= packageName %>.service.dto.<%= user.dtoClass %>;

import java.util.*;
import java.util.stream.Collectors;

import org.mapstruct.BeanMapping;
import org.mapstruct.Mapping;
import org.mapstruct.Mapper;
import org.mapstruct.Named;

/**
 * Mapper for the entity {@link <%= user.persistClass %>} and its DTO called {@link <%= user.dtoClass %>}.
 *
 * Normal mappers are generated using MapStruct, this one is hand-coded as MapStruct
 * support is still in beta, and requires a manual step with an IDE.
 */
@Mapper(componentModel = "spring")
public interface UserMapper extends EntityMapper<UserDTO, User> {

    default List<<%= user.dtoClass %>> usersToUserDTOs(List<<%= user.persistClass %>> users) {
        return users.stream()
            .filter(Objects::nonNull)
            .map(this::toDto)
            .collect(Collectors.toList());
    }

    default List<<%= user.adminUserDto %>> usersToAdminUserDTOs(List<<%= user.persistClass %>> users) {
        return users.stream()
            .filter(Objects::nonNull)
            .map(this::userToAdminUserDTO)
            .collect(Collectors.toList());
    }

    @Mapping(source = "authorities", target = "authorities", qualifiedByName = "name<%= manyListName %>")
    <%= user.adminUserDto %> userToAdminUserDTO(<%= user.persistClass %> user);

    default List<<%= user.persistClass %>> userDTOsToUsers(List<<%= user.adminUserDto %>> userDTOs) {
        return userDTOs.stream()
            .filter(Objects::nonNull)
            .map(this::userDTOToUser)
            .collect(Collectors.toList());
    }

    @Mapping(target = "authorities", ignore = true)
    <%= user.persistClass %> userDTOToUser(<%= user.adminUserDto %> userDTO);
<%_ if (databaseTypeCassandra || databaseTypeCouchbase) { _%>

    default <%= manyListName%><String> cleanNullStringAuthorities(<%= manyListName%><String> authoritiesAsString) {
        <%= manyListName%><String> authorities = new <%= manyListType%><>();

        if (authoritiesAsString != null) {
            authorities = authoritiesAsString.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.to<%= manyListName%>());
        }

        return authorities;
    }
<%_ } _%>

    default <%= user.persistClass %> userFromId(<%= user.primaryKey.type %> id) {
        if (id == null) {
            return null;
        }
        <%= user.persistClass %> user = new <%= user.persistClass %>();
        user.setId(id);
        return user;
    }

    @Named("id")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    default <%= user.dtoClass %> toDtoId(<%= user.persistClass %> user) {
        if (user == null) {
            return null;
        }
        <%= user.dtoClass %> userDto = new <%= user.dtoClass %>();
        userDto.setId(user.getId());
        return userDto;
    }

    @Named("idSet")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    default Set<<%= user.dtoClass %>> toDtoIdSet(Set<<%= user.persistClass %>> users) {
        if ( users == null ) {
            return Collections.emptySet();
        }

        Set<<%= user.dtoClass %>> userSet = new LinkedHashSet<>();
        for ( <%= user.persistClass %> userEntity : users ) {
            userSet.add( this.toDtoId( userEntity ) );
        }

        return userSet;
    }

    @Named("login")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    @Mapping(target = "login", source = "login")
    default <%= user.dtoClass %> toDtoLogin(<%= user.persistClass %> user) {
        if (user == null) {
            return null;
        }
        <%= user.dtoClass %> userDto = new <%= user.dtoClass %>();
        userDto.setId(user.getId());
        userDto.setLogin(user.getLogin());
        return userDto;
    }

    @Named("loginSet")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    @Mapping(target = "login", source = "login")
    default Set<<%= user.dtoClass %>> toDtoLoginSet(Set<<%= user.persistClass %>> users) {
        if ( users == null ) {
            return Collections.emptySet();
        }

        Set<<%= user.dtoClass %>> userSet = new LinkedHashSet<>();
        for ( <%= user.persistClass %> userEntity : users ) {
            userSet.add( this.toDtoLogin( userEntity ) );
        }

        return userSet;
    }

    @Named("firstName")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    @Mapping(target = "firstName", source = "firstName")
    default UserDTO toDtoFirstName(User user) {
        if (user == null) {
            return null;
        }
        UserDTO userDto = new UserDTO();
        userDto.setId(user.getId());
        userDto.setFirstName(user.getFirstName());
        return userDto;
    }

     @Named("authorityName")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    @Mapping(target = "name", source = "name")
    @Mapping(target = "code", source = "code")
    default AuthorityDTO toDtoName(Authority authority) {
        if (Objects.isNull(authority)) {
            return null;
        }
        AuthorityDTO result = new AuthorityDTO();
        result.setId(authority.getId());
        result.setName(authority.getName());
        return result;
    }

    @Named("name<%= manyListName %>")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    @Mapping(target = "name", source = "name")
    @Mapping(target = "code", source = "code")
    default <%= manyListName %><AuthorityDTO> toDtoNameList(<%= manyListName %><Authority> authorities) {
        if (authorities == null) {
            return Collections.empty<%= ormTool === 'mybatis' ? 'List' : 'Set' %>();
        }
        <%= manyListName %><AuthorityDTO> authorityList = new <%= manyListType %><>();
        for (Authority authorityEntity : authorities) {
            authorityList.add(this.toDtoName(authorityEntity));
        }
        return authorityList;
    }

    UserDTO toDto(User user);
}
