package com.ningdatech.carapi.user.security.auth.password;

import java.util.ArrayList;
import java.util.Objects;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.ningdatech.carapi.common.helper.UserInfoHelper;
import com.ningdatech.carapi.sys.entity.Role;
import com.ningdatech.carapi.sys.entity.vo.UserInfoVO;
import com.ningdatech.carapi.sys.manage.UserManage;
import com.ningdatech.carapi.sys.service.RoleService;
import com.ningdatech.carapi.user.security.auth.facade.UserAuthLoginFacade;
import com.ningdatech.carapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.carapi.user.security.auth.model.dto.UserFullInfoDTO;

import lombok.RequiredArgsConstructor;

/**
 * @author LiuXinXin
 * @date 2022/9/30 上午9:49
 */

@Service("passwordLoginUserDetailService")
@RequiredArgsConstructor
public class PasswordLoginUserDetailServiceImpl implements UserDetailsService {

    private final UserAuthLoginFacade userAuthLoginFacade;
    private final UserManage userManage;
    private final RoleService roleService;
    private final UserInfoHelper userInfoHelper;

    @Override
    public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserFullInfoDTO userFullInfoDTO = userAuthLoginFacade.queryUserInfoInPasswordAuth(username);
        if (Objects.isNull(userFullInfoDTO)) {
            throw new UsernameNotFoundException(String.format("%s user not exist", username));
        }

        UserInfoDetails userInfoDetails = new UserInfoDetails();
        userInfoDetails.setUserId(userFullInfoDTO.getUserId());
        userInfoDetails.setNickName(userFullInfoDTO.getNickName());
        userInfoDetails.setUsername(userFullInfoDTO.getNickName());
        userInfoDetails.setIdentifier(userFullInfoDTO.getIdentifier());
        userInfoDetails.setPassword(userFullInfoDTO.getCredential());
        userInfoDetails.setCompanyId(userFullInfoDTO.getCompanyId());
        userInfoDetails.setCompanyName(userFullInfoDTO.getCompanyName());
        userInfoDetails.setRoleList(new ArrayList<>());
        userInfoDetails.setRegionId(userFullInfoDTO.getRegionId());
        userInfoDetails.setRegionName(userFullInfoDTO.getRegionName());

        //UserInfoVO userInfoVO = userManage.queryDetails(userFullInfoDTO.getUserId());
        UserInfoVO userInfoVO = userInfoHelper.getUserDetailByUserId(userFullInfoDTO.getUserId());
        //即便是多个 暂时取一个
        if(Objects.isNull(userInfoVO) || Objects.isNull(userInfoVO.getRole())){
            return userInfoDetails;
        }
        userInfoDetails.setRoleId(userInfoVO.getRole());
        Role role = roleService.getById(userInfoVO.getRole());
        if(Objects.isNull(role) || Objects.isNull(role.getDataScope())){
            return userInfoDetails;
        }
        userInfoDetails.setDataScope(role.getDataScope());
        return userInfoDetails;
    }
}
