package com.qmth.qrzk.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.qmth.qrzk.repository.user.model.Role;
import com.qmth.qrzk.repository.user.model.User;
import com.qmth.qrzk.repository.user.service.IRoleService;

@Component
public class UserLoginService implements UserDetailsService{

	@javax.annotation.Resource(name="userDaoImpl")
	private LoginDao loginDao;
	
	@Autowired
	@Qualifier("roleServiceImpl")
	private IRoleService roleService;
	
	public User getUserByUsername(String username){
		return (User)loginDao.getLoginUserByUsername(username);
	}
	
	
	public UserDetails loadUserByUsername(String username)
	throws UsernameNotFoundException{
		UserDetails ud = null;
		
		boolean accountNonExpired = true;  
        boolean credentialsNonExpired = true;  
        boolean accountNonLocked = true;  
        
        if(username.equals("superadmin")){
        	Collection<GrantedAuthority> superAuths = new LinkedList<GrantedAuthority>();
        	superAuths.add(new SimpleGrantedAuthority("9999"));
        	ud = new org.springframework.security.core.userdetails.User(username, 
            		"qmth87863577", 
            		true, 
            		accountNonExpired, 
            		credentialsNonExpired, 
            		accountNonLocked,
            		superAuths);
        }
        else{
        	LoginUser user = loginDao.getLoginUserByUsername(username);
    		if(user == null){
    			throw new UsernameNotFoundException("用户不存在");
    		}
    		
    		Collection<GrantedAuthority> auths = getAuthorities(user);
    		boolean enabled = user.isEnabled();
    		ud = new org.springframework.security.core.userdetails.User(username, 
    				user.getPassword(), 
            		enabled, 
            		accountNonExpired, 
            		credentialsNonExpired, 
            		accountNonLocked,
            		auths);
        }
		return ud;
	}
	
	/**
	 * 查询用户角色，spring security授权用
	 * @param user
	 * @return
	 */
	protected Collection<GrantedAuthority> getAuthorities(LoginUser user) {
		
		List<Role> roles = new ArrayList<Role>();
		if(user instanceof User){ 
			roles = roleService.getRolesByUserId(((User) user).getId());
		}
		
		Collection<GrantedAuthority> auths = new LinkedList<GrantedAuthority>();
		for(Role role : roles){
			auths.add(new SimpleGrantedAuthority(role.getCode()));
		}
		return auths;
	}
	
//	public Set<Resource> getLoginUserResources(LoginUser user){
//		return user.getResources();
//	}
}
