package com.its.wthr.webapp.service;

import com.its.wthr.webapp.vo.UserVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService implements UserDetailsService {
	@Value("${application.user.id:admin}")
	private String userId;
	@Value("${application.user.pswd:1234}")
	private String userPswd;

	public UserVo loadUserByUsername(String username) throws UsernameNotFoundException {
		String userId = this.userId;
		String userPswd = this.userPswd;
		UserVo user = new UserVo(userId, "{noop}" + userPswd);
		if (username == null || !username.equals(user.getId())) {
			throw new UsernameNotFoundException("No user found with " + username + user.getUsername());
		} else {
			List<GrantedAuthority> roles = new ArrayList<>();
			roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
			user.setAuthorities(roles);
			return user;
		}
	}
}