package ru.ulstu.user.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.util.StringUtils;
import ru.ulstu.configuration.Constants;
import ru.ulstu.odin.model.OdinDto;
import ru.ulstu.odin.model.annotation.OdinCaption;
import ru.ulstu.odin.model.annotation.OdinReadOnly;
import ru.ulstu.odin.model.annotation.OdinString;
import ru.ulstu.odin.model.annotation.OdinVisible;
import ru.ulstu.user.controller.UserController;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static ru.ulstu.odin.model.annotation.OdinString.OdinStringType.PASSWORD;

public class UserDto implements OdinDto {
    @OdinReadOnly
    private Integer id;

    @NotBlank
    @Pattern(regexp = Constants.LOGIN_REGEX)
    @Size(min = 4, max = 50)
    @OdinCaption("Логин")
    private String login;

    @NotBlank
    @Size(min = 2, max = 50)
    @OdinCaption("Имя")
    private String firstName;

    @NotBlank
    @Size(min = 2, max = 50)
    @OdinCaption("Фамилия")
    private String lastName;

    @Email
    @NotBlank
    @Size(min = 5, max = 100)
    @OdinCaption("E-Mail")
    private String email;

    @OdinCaption("Аккаунт активен")
    private boolean activated;

    @OdinCaption("Роли")
    private LinkedHashSet<UserRoleDto> roles;

    @OdinString(type = PASSWORD)
    @OdinVisible(type = OdinVisible.OdinVisibleType.ON_UPDATE)
    @OdinCaption("Текущий пароль")
    @Size(max = 50)
    private String oldPassword;

    @OdinString(type = PASSWORD)
    @OdinCaption("Пароль")
    @Size(min = Constants.MIN_PASSWORD_LENGTH, max = 50)
    private String password;

    @OdinString(type = PASSWORD)
    @OdinCaption("Пароль (подтверждение)")
    @Size(min = Constants.MIN_PASSWORD_LENGTH, max = 50)
    private String passwordConfirm;

    public UserDto() {
        activated = false;
        roles = new LinkedHashSet<>();
    }

    public UserDto(User user) {
        this();
        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.roles.addAll(user.getRoles().stream()
                .map(UserRoleDto::new)
                .collect(Collectors.toList()));
    }

    public Integer getId() {
        return id;
    }

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

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

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isActivated() {
        return activated;
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    public Set<UserRoleDto> getRoles() {
        return roles;
    }

    public void setRoles(Collection<UserRoleDto> roles) {
        this.roles.clear();
        this.roles.addAll(roles);
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public String getPassword() {
        return password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    @JsonIgnore
    public boolean isPasswordsValid() {
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(passwordConfirm)) {
            return false;
        }
        return Objects.equals(password, passwordConfirm);
    }

    @JsonIgnore
    public boolean isOldPasswordValid() {
        return !StringUtils.isEmpty(oldPassword);
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + " {" +
                "id=" + id +
                ", login='" + login + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", activated=" + activated +
                ", roles=" + roles +
                ", password='" + password + '\'' +
                ", passwordConfirm='" + passwordConfirm + '\'' +
                '}';
    }
}
