package com.bsmlab.dfx.dfxconsole.framework.config;

import com.bsmlab.dfx.dfxconsole.app.user.DfxUserDto;
import io.micrometer.common.util.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Collection;
import java.util.List;

public class AppUserDetails implements UserDetails {
    private final DfxUserDto dfxUserDto;
    private final Collection<? extends GrantedAuthority> authorities;

    public AppUserDetails(DfxUserDto dfxUserDto) {
        this.dfxUserDto = dfxUserDto;
        this.authorities = List.of(new SimpleGrantedAuthority("USER"));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder.encode(this.dfxUserDto.getUserPassword());
        //return this.consoleUserDto.getUserPassword();
    }

    @Override
    public String getUsername() {
        return this.dfxUserDto.getUserEmail();
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.dfxUserDto != null && StringUtils.isNotBlank(this.dfxUserDto.getUserUuid());
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.dfxUserDto != null && StringUtils.isNotBlank(this.dfxUserDto.getUserUuid());
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.dfxUserDto != null && StringUtils.isNotBlank(this.dfxUserDto.getUserUuid());
    }

    @Override
    public boolean isEnabled() {
        return this.dfxUserDto != null && StringUtils.isNotBlank(this.dfxUserDto.getUserUuid());
    }
}
