package ru.ulstu.user.model;

import ru.ulstu.odin.model.OdinDto;
import ru.ulstu.odin.model.annotation.OdinCaption;
import ru.ulstu.odin.model.annotation.OdinVisible;
import ru.ulstu.user.controller.UserController;

import java.util.Collections;
import java.util.Objects;
import java.util.Optional;

public class UserListDto implements OdinDto {
    @OdinVisible(type = OdinVisible.OdinVisibleType.NONE)
    private int id;
    @OdinCaption("Логин")
    private String login;
    @OdinCaption("Имя")
    private String firstName;
    @OdinCaption("Фамилия")
    private String lastName;
    @OdinCaption("E-Mail")
    private String email;
    @OdinCaption("Аккаунт активен")
    private boolean activated;
    @OdinCaption("Права администратора")
    private boolean admin;

    public UserListDto(User user) {
        this.id = user.getId();
        this.login = user.getLogin();
        this.firstName = user.getFirstName();
        this.lastName = user.getLastName();
        this.email = user.getEmail();
        this.activated = user.getActivated();
        this.admin = Optional.ofNullable(user.getRoles()).orElse(Collections.emptySet()).stream()
                .anyMatch(role -> Objects.equals(UserRoleConstants.ADMIN, role.getName()));
    }

    public Integer getId() {
        return id;
    }

    @Override
    public String getViewValue() {
        return login;
    }

    @Override
    public String getControllerPath() {
        return UserController.URL;
    }

    public String getLogin() {
        return login;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getEmail() {
        return email;
    }

    public boolean isActivated() {
        return activated;
    }

    public boolean isAdmin() {
        return admin;
    }
}
