/*
 * Copyright 2025 Apollo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.ctrip.framework.apollo.portal.spi.springsecurity;

import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.ctrip.framework.apollo.portal.spi.UserService;
import java.util.function.Supplier;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.beans.factory.ObjectProvider;

import java.security.Principal;

public class SpringSecurityUserInfoHolder implements UserInfoHolder {

  private final Supplier<UserService> userServiceSupplier;

  public SpringSecurityUserInfoHolder(UserService userService) {
    this(() -> userService);
  }

  public SpringSecurityUserInfoHolder(ObjectProvider<UserService> userServiceProvider) {
    this(userServiceProvider::getObject);
  }

  private SpringSecurityUserInfoHolder(Supplier<UserService> userServiceSupplier) {
    this.userServiceSupplier = userServiceSupplier;
  }

  @Override
  public UserInfo getUser() {
    String userId = this.getCurrentUsername();
    UserInfo userInfoFound = this.userServiceSupplier.get().findByUserId(userId);
    if (userInfoFound != null) {
      return userInfoFound;
    }
    UserInfo userInfo = new UserInfo();
    userInfo.setUserId(userId);
    return userInfo;
  }

  private String getCurrentUsername() {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (principal instanceof UserDetails) {
      return ((UserDetails) principal).getUsername();
    }
    if (principal instanceof Principal) {
      return ((Principal) principal).getName();
    }
    return String.valueOf(principal);
  }

}
