package org.ccpit.base.utils;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.ccpit.base.controller.BaseController;
import org.ccpit.base.orgManage.OrgService;
import org.ccpit.base.role.Role;
import org.ccpit.base.user.User;
import org.ccpit.base.user.UserService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Repository;

/**
 * 用户角色缓存，操作工具
 * <p/>
 * Created by sqp on 2015/9/30.
 */
@Repository
public class UserRolesUtil implements ApplicationContextAware {
    public static final String USER_ROLES = "user_roles_in_session";
    public static final String ORG_ORLES = "user_group_roles_in_session";
    public static final String ALL_ROLES = "user_all_roles_in_session";
    private static ApplicationContext ac;

    /**
     * 从session中获取用户信息
     *
     * @param request
     * @return
     */
    public User getSessionUser(HttpServletRequest request) {
        return (User) request.getSession().getAttribute(BaseController.USER_IN_SESSION);
    }

    /**
     * 初始化或重新载入用户的所有role信息
     *
     * @param request
     */
    public void initOrReload(HttpServletRequest request) {
        User user = getSessionUser(request);

        UserService userService = ac.getBean(UserService.class);
        OrgService orgService = ac.getBean(OrgService.class);
        Set<Role> userRoles = new HashSet<Role>(userService.getUserRoles(user));
        Set<Role> orgRoles = new HashSet<Role>(orgService.getRolesForUser(user));
        Set<Role> roleSet = new HashSet<Role>();
        roleSet.addAll(userRoles);
        roleSet.addAll(orgRoles);

        request.getSession().setAttribute(ALL_ROLES, roleSet);
        request.getSession().setAttribute(USER_ROLES, userRoles);
        request.getSession().setAttribute(ORG_ORLES, orgRoles);
    }

    /**
     * 获取用户roles
     *
     * @param request
     * @param scope
     * @return
     */
    public Set<Role> getRoles(HttpServletRequest request, String scope) {
        Set<Role> roles = new HashSet<Role>();
        Object object = request.getSession().getAttribute(scope);
        if(object==null) {
            initOrReload(request);
            object = request.getSession().getAttribute(scope);
        }
        roles.addAll((Set<Role>) object);
        return roles;
    }

    /**
     * 获取用户的所有角色 （可能来自缓存，如需重载请调用方法 @initOrReload）
     *
     * @param request
     * @return
     */
    public Set<Role> getUserRoles(HttpServletRequest request) {
        return getRoles(request, USER_ROLES);
    }

    /**
     * 获取用户的部门所有角色 （可能来自缓存，如需重载请调用方法 @initOrReload）
     *
     * @param request
     * @return
     */
    public Set<Role> getUserGroupRoles(HttpServletRequest request) {
        return getRoles(request, ORG_ORLES);
    }

    /**
     * 获取用户的所有角色 包括用户角色和部门角色 （可能来自缓存，如需重载请调用方法 @initOrReload）
     *
     * @param request
     * @return
     */
    public Set<Role> getAllRoles(HttpServletRequest request) {
        return getRoles(request, ALL_ROLES);
    }

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