/**
 * Copyright (c) 2022 cloud Authors. All Rights Reserved.
 *
 * 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.lutao.cloud.auth.handler;

import com.lutao.cloud.admin.api.dto.SysLogDTO;
import com.lutao.cloud.admin.api.entity.SysUser;
import com.lutao.cloud.admin.api.feign.RemoteLogService;
import com.lutao.cloud.admin.api.feign.RemoteUserService;
import com.lutao.cloud.common.core.constant.SecurityConstants;
import com.lutao.cloud.common.core.util.KeyStrResolver;
import com.lutao.cloud.common.core.util.CloudWebUtils;
import com.lutao.cloud.common.log.util.LogTypeEnum;
import com.lutao.cloud.common.log.util.SysLogUtils;
import com.lutao.cloud.common.security.handler.AuthenticationSuccessHandler;
import com.lutao.cloud.common.security.service.CloudUser;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @date 2018/10/8
 */
@Slf4j
@Component
@AllArgsConstructor
public class CloudAuthenticationSuccessEventHandler implements AuthenticationSuccessHandler{

    private final RemoteLogService logService;

    private final KeyStrResolver tenantKeyStrResolver;

    private final RemoteUserService userService;


    /**
     * 处理登录成功方法
     * <p>
     * 获取到登录的authentication 对象
     * 登录成功后添加登录日志
     * @param authentication 登录对象
     * @param request        请求
     * @param response       返回
     */
    @Async
    @Override
    public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response){
        String username = authentication.getName();
        SysLogDTO sysLog = SysLogUtils.getSysLog(request, username);
        sysLog.setTitle(username + "用户登录");
        sysLog.setParams(username);
        String header = request.getHeader(HttpHeaders.AUTHORIZATION);
        sysLog.setServiceId(CloudWebUtils.extractClientId(header).orElse("N/A"));
        sysLog.setTenantId(Integer.parseInt(tenantKeyStrResolver.key()));
        Object object = authentication.getPrincipal();
        CloudUser cloudUser = (CloudUser) object;
        String loginType = request.getParameter("loginType");
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(cloudUser,sysUser);
        sysUser.setUserId(cloudUser.getId());

        log.info("用户：{} ，登录类型为：{}", username, loginType);
        sysLog.setDeptId(sysUser.getDeptId());
        sysLog.setType(LogTypeEnum.LOGIN.getType());
        sysLog.setUserId(sysUser.getUserId());
        logService.saveLog(sysLog, SecurityConstants.FROM_IN);
        log.info("用户：{} 登录成功", username);
    }

}
