package com.yunfeiyun.agmp.common.framework.web.service;

import com.yunfeiyun.agmp.common.constant.Constants;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.exception.SsoException;
import com.yunfeiyun.agmp.common.core.domain.model.IBaseUser;
import com.yunfeiyun.agmp.common.core.domain.model.LoginUser;
import com.yunfeiyun.agmp.common.core.domain.model.PortalInfo;
import com.yunfeiyun.agmp.common.enums.UserStatus;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户验证处理
 *
 * @author 杨晓辉
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {
        IBaseUser user =  SpringUtils.getBean(ISysUserService.class).selectUserByUserName(username);
        PortalInfo portalInfo = SpringUtils.getBean(PortalService.class).getSsoInfo();
        if("1".equalsIgnoreCase(portalInfo.getSsoEnabled()) && ! portalInfo.getAppName().toUpperCase().contains(Constants.PTS)){
            if (StringUtils.isNull(user))
            {
                log.info("登录用户：{} 不存在.", username);
                throw new SsoException( ErrorCode.AUTH_SSO_LOGIN_USERNOEXIST.getCode(), ErrorCode.AUTH_SSO_LOGIN_USERNOEXIST.getMessage() );  //网关集成失败
            }
            else if (UserStatus.DELETED.getCode().equals(user.getUserDelstatus()))
            {
                log.info("登录用户：{} 已被删除.", username);
                throw new SsoException( ErrorCode.AUTH_SSO_ACCOUNT_DELETE.getCode(),ErrorCode.AUTH_SSO_ACCOUNT_DELETE.getMessage() );
            }
            else if (UserStatus.DISABLE.getCode().equals(user.getUserLockstatus()))
            {
                log.info("登录用户：{} 已被停用.", username);
                throw new SsoException(ErrorCode.AUTH_SSO_ACCOUNT_DISABLED.getCode(), ErrorCode.AUTH_SSO_ACCOUNT_DISABLED.getMessage());
            }
            return createLoginUser(user);
        }else{   //正常流程
                if (StringUtils.isNull(user))
                {
                    log.info("登录用户：{} 不存在.", username);
                    throw new BizException(ErrorCode.LOGIN_USERNOEXIST_ERROR.getCode(), ErrorCode.LOGIN_USERNOEXIST_ERROR.getMessage());
                }
                else if (UserStatus.DELETED.getCode().equals(user.getUserDelstatus()))
                {
                    log.info("登录用户：{} 已被删除.", username);
                    throw new BizException(ErrorCode.ACCOUNT_DELETE_ERROR.getCode(),ErrorCode.ACCOUNT_DELETE_ERROR.getMessage());
                }
                else if (UserStatus.DISABLE.getCode().equals(user.getUserLockstatus()))
                {
                    log.info("登录用户：{} 已被停用.", username);
                    throw new BizException(ErrorCode.ACCOUNT_DISABLED_ERROR.getCode(),ErrorCode.ACCOUNT_DISABLED_ERROR.getMessage());
                }
                SpringUtils.getBean(SysPasswordService.class).validate(user);
         }


        return createLoginUser(user);
    }

    public UserDetails createLoginUser(IBaseUser user)
    {
        return new LoginUser(user.getUserId(), user.getDeptId(), user, SpringUtils.getBean(SysPermissionService.class).getMenuPermission(user));
    }
}
