package kr.co.uplus.ez.common.jwt;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import kr.co.uplus.ez.api.login.dto.AuthUser;

@JsonIgnoreProperties({ "status", "userPwd", "pwdChgDt", "loginFailCnt", "phone", "sms", "username", "password",
		"enabled", "accountNonExpired", "accountNonLocked", "credentialsNonExpired" })
public class JwtUser extends AuthUser {
	private static final long serialVersionUID = -1233591656437541107L;
	
	public static JwtUser createJwtUser(AuthUser user) {
		JwtUser u = new JwtUser();
		u.setCorpId(user.getCorpId());
		u.setOprtrId(user.getOprtrId());
		u.setOprtrNm(user.getUsername());
		u.setHpNo(user.getHpNo());
		u.setAuthorities(user.getAuthorities());
		u.setPwd(user.getPassword());
		return u;
	}

	public static JwtUser createAuthUser(Map<String, Object> principal) {
		JwtUser u = new JwtUser();
		if (principal.get("corpId") != null) {
			u.setCorpId((String) principal.get("corpId"));
		}
		u.setOprtrId((String) principal.get("oprtrId"));
		u.setOprtrNm((String) principal.get("oprtrId"));
		@SuppressWarnings({ "unchecked", "rawtypes" })
		Collection<? extends GrantedAuthority> authorities = ((List<Map>) principal.get("authorities")).stream()
				.map(o -> new SimpleGrantedAuthority((String) o.get("authority"))).collect(Collectors.toList());
		u.setAuthorities(authorities);
		return u;
	}
}
