// fs-company模块
package com.fs.framework.config;

import com.fs.app.utils.JwtUtils;
import com.fs.common.config.LoginContextCallback;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.enums.DataScopeEnum;
import com.fs.common.enums.PlatEnum;
import com.fs.common.utils.SecurityUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录上下文回调实现
 */
@Component
public class CompanyLoginContextCallback implements LoginContextCallback, ApplicationContextAware {

    private static ApplicationContext applicationContext;
    @Autowired
    JwtUtils jwtUtils;

    @Autowired
    private ICompanyUserService companyUserService;

    /**
     * 用户ID的request attribute key
     */
    private static final String USER_KEY = "userId";

    /**
     * 从request中获取当前登录的userId
     *
     * @return userId
     */
    private Long getCurrentUserId() {
        HttpServletRequest request = ServletUtils.getRequest();
        Object userIdObj = request.getAttribute(USER_KEY);
        if (userIdObj == null) {
            return null;
        }
        return (Long) userIdObj;
    }
    /**
     * 根据userId从数据库查询用户信息
     *
     * @param userId 用户ID
     * @return CompanyUser对象
     */
    private CompanyUser getCompanyUserByUserId(Long userId) {
        if (userId == null) {
            return null;
        }
        return companyUserService.selectCompanyUserById(userId);
    }

    @Override
    public Long getUserId() {
       throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Long getCompanyUserId() {
        return getCurrentUserId();
    }

    @Override
    public String getUsername() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getCompanyUserName() {
        Long userId = getCurrentUserId();
        if (userId == null) {
            return null;
        }
        CompanyUser user = getCompanyUserByUserId(userId);
        return user != null ? user.getUserName() : null;
    }

    @Override
    public Long getCompanyId() {
        Long userId = getCurrentUserId();
        if (userId == null) {
            return null;
        }
        CompanyUser user = getCompanyUserByUserId(userId);
        return user != null ? user.getCompanyId() : null;
    }

    @Override
    public Long getDeptId() {
        Long userId = getCurrentUserId();
        if (userId == null) {
            return null;
        }
        CompanyUser user = getCompanyUserByUserId(userId);
        return user != null ? user.getDeptId() : null;
    }


    @Override
    public DataScopeEnum getDataScope(){

        return null;
    }

    @Override
    public PlatEnum getNowPlatEnum() {
        return PlatEnum.COMPANY_APP;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CompanyLoginContextCallback.applicationContext = applicationContext;
    }
}
