package com.fsd.hellovelo.security.services;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fsd.hellovelo.entity.EStatus;
import com.fsd.hellovelo.entity.Role;
import com.fsd.hellovelo.entity.User;
import org.sonatype.guice.plexus.config.Roles;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public class UserDetailsImpl implements UserDetails {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String username;
    private String email;
    private String firstName;
    private String lastName;
    private EStatus status;
    private String stripeUserId;
    private Set<Role> roles;
    @JsonIgnore
    private String password;
    private boolean provisionAccess;
    private Collection<? extends GrantedAuthority> authorities;
    public UserDetailsImpl(Long id, String username, String email, String password, String firstName, String lastName,
                           EStatus status, String stripeUserId, Set<Role> roles,
                           Collection<? extends GrantedAuthority> authorities, boolean provisionAccess) {
        this.id = id;
        this.username = username;
        this.email = email;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.status = status;
        this.stripeUserId = stripeUserId;
        this.authorities = authorities;
        this.roles = roles;
        this.provisionAccess = provisionAccess;
    }
    public static UserDetailsImpl build(User user) {
        List<GrantedAuthority> authorities = user.getRoles().stream()
                .map(role -> new SimpleGrantedAuthority(role.getName().name()))
                .collect(Collectors.toList());
        return new UserDetailsImpl(
                user.getId(),
                user.getUsername(),
                user.getEmail(),
                user.getPassword(),
                user.getFirstName(),
                user.getLastName(),
                user.getStatus(),
                user.getStripeUserId(),
                user.getRoles(),
                authorities, user.isProvisionAccess());
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
    public Long getId() {
        return id;
    }
    public String getEmail() {
        return email;
    }
    public String getStripeUserId() {
        return stripeUserId;
    }
    public String getFirstName(){
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public EStatus getStatus() {
        return status;
    }
    public boolean isProvisionAccess(){
        return provisionAccess;
    }
    public Set<Role> getRoles() {
        return roles;
    }
    @Override
    public String getPassword() {
        return password;
    }
    @Override
    public String getUsername() {
        return username;
    }
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    @Override
    public boolean isEnabled() {
        return true;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        UserDetailsImpl user = (UserDetailsImpl) o;
        return Objects.equals(id, user.id);
    }
}
