package com.tuoheng.service.impl;

import com.nimbusds.jose.shaded.json.JSONObject;
import com.tuoheng.mapper.UserMapper;
import com.tuoheng.model.dto.UserBaseInfoDto;
import com.tuoheng.service.OidcUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.core.oidc.OidcScopes;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Collections;
import java.util.Set;

/**
 * @author chenjiandong
 * @description: TODO
 * @date 2022/10/9 13:40
 */
@Service
public class OidcUserInfoServiceImpl implements OidcUserInfoService {

    @Override
    public OidcUserInfo loadUser(String name, Set<String> scopes, UserBaseInfoDto userBaseInfoDto) {
        OidcUserInfo.Builder builder = OidcUserInfo.builder().subject(name);
        if (!CollectionUtils.isEmpty(scopes)) {
            if (scopes.contains(OidcScopes.PROFILE)) {
                builder.claim("userId", userBaseInfoDto.getUserId())
                        .claim("userName", userBaseInfoDto.getUserName())
                        .claim("authority", userBaseInfoDto.getAuthorityList())
                        .claim("clientRoleList", userBaseInfoDto.getClientRoleDtoList());
            }
            /*if (scopes.contains(OidcScopes.EMAIL)) {
                builder.email(name + "@163.com").emailVerified(true);
            }
            if (scopes.contains(OidcScopes.ADDRESS)) {
                JSONObject address = new JSONObject();
                address.put("address", Collections.singletonMap("formatted", "Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance"));
                builder.address(address.toJSONString());
            }
            if (scopes.contains(OidcScopes.PHONE)) {
                builder.phoneNumber("13728903134").phoneNumberVerified("false");
            }*/
        }
        return builder.build();
    }

}
