package eu.eudat.models.data.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import eu.eudat.types.Authorities;

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


public class Principal {
    private UUID id;
    private UUID token;
    private String name;
    private Date expiresAt;
    private String avatarUrl;
    private Set<Authorities> authorities;
    private String culture;
    private String language;
    private String timezone;

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public UUID getToken() {
        return token;
    }

    public void setToken(UUID token) {
        this.token = token;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getExpiresAt() {
        return expiresAt;
    }

    public void setExpiresAt(Date expiresAt) {
        this.expiresAt = expiresAt;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public Set<Integer> getAuthorities() {
        return authorities.stream().map(authz -> authz.getValue()).collect(Collectors.toSet());
    }

    public String getCulture() {
        return culture;
    }

    public void setCulture(String culture) {
        this.culture = culture;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getTimezone() {
        return timezone;
    }

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }

    @JsonIgnore
    public Set<Authorities> getAuthz() {
        return this.authorities;
    }

    public void setAuthorities(Set<Authorities> authorities) {
        this.authorities = authorities;
    }

    public boolean isAuthorized(List<Authorities> requiredAuthorities) {
        if (!Collections.disjoint(this.authorities, requiredAuthorities) || requiredAuthorities.size() == 0)
            return true;
        else return false;
    }
}
