package it.mice.voila.runtime.security;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import it.mice.voila.runtime.security.encoders.PBEWithMD5AndDESPasswordEncryption;

/**
 * This class exists only because the new spring security infrastructure is too
 * protected !!! Overriding all getters to extends its visibility from protected
 * to public to allow more flexible usage.
 */
public class DefaultDaoAuthenticationProvider extends DaoAuthenticationProvider {

	private SaltSource saltSource;

	public DefaultDaoAuthenticationProvider() {
		Map<String, org.springframework.security.crypto.password.PasswordEncoder> encoders = new HashMap<>();
//      // Aggiungi l'encoder bcrypt
//      // encoders.put("bcrypt", new BCryptPasswordEncoder());
//      // Aggiungi il tuo encoder personalizzato
		encoders.put("pbe", new PBEWithMD5AndDESPasswordEncryption());

		DelegatingPasswordEncoder delegatingPasswordEncoder = new DelegatingPasswordEncoder("pbe", encoders);
		delegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(encoders.get("pbe"));

		setPasswordEncoder(delegatingPasswordEncoder);
	}

//	private PasswordEncoder passwordEncoder;

	protected void doAfterPropertiesSet() {
		super.doAfterPropertiesSet();
	}

//	@Autowired
//	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
//		// TODO Auto-generated method stub
////		super.setPasswordEncoder(passwordEncoder);
//		this.passwordEncoder = passwordEncoder;
//	}

//	@Autowired(required = false)
//	private PasswordEncoder passwordEncoder;

	/**
	 * The source of salts to use when decoding passwords. <code>null</code> is a
	 * valid value, meaning the <code>DaoAuthenticationProvider</code> will present
	 * <code>null</code> to the relevant <code>PasswordEncoder</code>.
	 * <p>
	 * Instead, it is recommended that you use an encoder which uses a random salt
	 * and combines it with the password field. This is the default approach taken
	 * in the {@code org.springframework.security.crypto.password} package.
	 *
	 * @param saltSource to use when attempting to decode passwords via the
	 *                   <code>PasswordEncoder</code>
	 */
	@Autowired(required = false)
	public void setSaltSource(SaltSource saltSource) {
		this.saltSource = saltSource;
//		super.setSaltSource(saltSource);
	}

	@Autowired
	public void setUserDetailsService(UserDetailsService userDetailsService) {
		// TODO Auto-generated method stub
		super.setUserDetailsService(userDetailsService);
	}

	public PasswordEncoder getPasswordEncoder() {
		// TODO Auto-generated method stub
		return super.getPasswordEncoder();
//		return passwordEncoder;
	}

	public SaltSource getSaltSource() {
		return saltSource;
	}

	@Override
	public UserDetailsService getUserDetailsService() {
		// TODO Auto-generated method stub
		return super.getUserDetailsService();
	}

}
