package com.its.op.security;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;

@Slf4j
public class WebPasswordEncoder implements PasswordEncoder {

    @Override
    public String encode(CharSequence rawPassword) throws IllegalArgumentException {
        // UserInfrVo 의 비밀번호(DB 에서 조회한 비밀번호)
        if (rawPassword == null) {
            throw new IllegalArgumentException("rawPassword cannot be null");
        }
        //log.error("encode: {}", rawPassword.toString());
        return rawPassword.toString();
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) throws IllegalArgumentException {
        // 사용자가 입력한 비밀번호: rawPassword
        // UserDetails.getPassword : encodedPassword
        if (rawPassword == null) {
            throw new IllegalArgumentException("rawPassword cannot be null");
        }

        if (encodedPassword == null || encodedPassword.length() == 0) {
            log.warn("Empty encoded password");
            return false;
        }
        //log.error("matches: {}, {}, {}", rawPassword.toString(), encodedPassword, StringUtils.equals(rawPassword.toString(), encodedPassword));
        return StringUtils.equals(rawPassword.toString(), encodedPassword);
    }

    @Override
    public boolean upgradeEncoding(String encodedPassword) {
        //log.error("upgradeEncoding: {}", encodedPassword);
        if (encodedPassword == null || encodedPassword.length() == 0) {
            log.warn("Empty encoded password");
            return false;
        }
        return true;
    }
}
