package org.ta.pddserver.config.security;

import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.ta.pddserver.utils.MD5Util;

@Component
public class JwtUtils {
//    @Value("${app.jwt.secret}")
    private String secret = "Allen";
//    @Value("${app.jwt.expiration}")
    private long expiration = 3600 * 18;
    public String generateToken(UserDetails userDetails) {
        return userDetails.getUsername() + ":" + MD5Util.encode(userDetails.getPassword());
    }
    public String extractUsername(String token) {
        return token.split(":")[0];
//        return "Allen";
    }
    public boolean validateToken(String token, UserDetails userDetails) {
        final String username = extractUsername(token);
        return (username.equals(userDetails.getUsername())) && !isTokenExpired(token);
    }
    private boolean isTokenExpired(String token) {
        return false;
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
