package org.ccpit.base.utils;

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

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户角色缓存，操作工具
 * <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 USER_GROUP_ROLES = "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);
        UserGroupService groupService = ac.getBean(UserGroupService.class);
        Set<Role> userRoles = new HashSet<Role>(userService.getUserRoles(user));
        Set<Role> groupRoles = new HashSet<Role>(groupService.getRolesForUser(user));
        Set<Role> roleSet = new HashSet<Role>();
        roleSet.addAll(userRoles);
        roleSet.addAll(groupRoles);

        request.getSession().setAttribute(ALL_ROLES, roleSet);
        request.getSession().setAttribute(USER_ROLES, userRoles);
        request.getSession().setAttribute(USER_GROUP_ROLES, groupRoles);
    }

    /**
     * 获取用户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, USER_GROUP_ROLES);
    }

    /**
     * 获取用户的所有角色 包括用户角色和用户组角色 （可能来自缓存，如需重载请调用方法 @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;
    }
}
