package com.cm.common.component;

import com.cm.common.pojo.bos.*;
import com.cm.common.pojo.dtos.CurrentUserIdInfoDTO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName: SecurityCompontent
 * @Description: 权限工具
 * @Author: WangGeng
 * @Date: 2019/2/26 3:31 PM
 * @Version: 1.0
 **/
@Component
public class SecurityComponent {

    /**
     * 超级管理员
     */
    public static final String USERNAME_ADMIN = "admin";

    /**
     * 获取当前用户
     *
     * @return
     */
    public UserInfoBO getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object user = authentication.getPrincipal();
        UserInfoBO userInfoBO = null;
        if (user instanceof UserBO) {
            userInfoBO = new UserInfoBO();
            UserBO userBO = (UserBO) user;
            userInfoBO.setUserId(userBO.getUserId());
            userInfoBO.setUserUsername(userBO.getUsername());
            userInfoBO.setUserName(userBO.getUserName());
            userInfoBO.setUserPhone(userBO.getUserPhone());
            userInfoBO.setUserAvatar(userBO.getUserAvatar());
            userInfoBO.setUserEmail(userBO.getUserEmail());
            userInfoBO.setDataAuthority(userBO.getDataAuthority());
            userInfoBO.setDataAuthorityUserIds(userBO.getDataAuthorityUserIds());
            userInfoBO.setBaseDepartmentIds(userBO.getBaseDepartmentIds());
            userInfoBO.setRoles(userBO.getRoles());
            userInfoBO.setDepartments(userBO.getDepartments());
            userInfoBO.setGroups(userBO.getGroups());
            userInfoBO.setPositions(userBO.getPositions());
        }
        if (user instanceof UserInfoBO) {
            userInfoBO = (UserInfoBO) user;
        }
        return userInfoBO;
    }

    /**
     * 当前角色列表
     *
     * @return
     */
    public List<RoleBO> listRole() {
        UserInfoBO userInfoBO = getCurrentUser();
        return userInfoBO.getRoles();
    }

    /**
     * 角色ID列表
     *
     * @return
     */
    public List<String> listRoleIds() {
        List<String> roleIds = new ArrayList<>();
        for (RoleBO roleBO : listRole()) {
            roleIds.add(roleBO.getRoleId());
        }
        return roleIds;
    }

    /**
     * 部门列表
     *
     * @return
     */
    public List<DepartmentBO> listDepartment() {
        UserInfoBO userInfoBO = getCurrentUser();
        return userInfoBO.getDepartments();
    }

    /**
     * 组列表
     *
     * @return
     */
    public List<GroupBO> listGroup() {
        UserInfoBO userInfoBO = getCurrentUser();
        return userInfoBO.getGroups();
    }

    /**
     * 职位列白
     *
     * @return
     */
    public List<PositionBO> listPosition() {
        UserInfoBO userInfoBO = getCurrentUser();
        return userInfoBO.getPositions();
    }

    /**
     * 获取当前用户名
     *
     * @return
     */
    public String getCurrentUsername() {
        UserInfoBO userInfoBO = getCurrentUser();
        return userInfoBO.getUserUsername();
    }

    /**
     * 获取当前用户ID信息
     *
     * @return
     */
    public CurrentUserIdInfoDTO getCurrentUserIdInfo() {
        UserInfoBO userInfoBO = getCurrentUser();
        CurrentUserIdInfoDTO currentUserIdInfoDTO = new CurrentUserIdInfoDTO();
        currentUserIdInfoDTO.setUserIdAndValue(new StringBuilder(userInfoBO.getUserId()).append("|").append(userInfoBO.getUserName()).toString());
        currentUserIdInfoDTO.setUserNameValue(userInfoBO.getUserName());

        String departmentIdAndNamesValue = userInfoBO.getDepartmentIdAndNamesValue();
        currentUserIdInfoDTO.setDepartmentIdAndNamesValue(departmentIdAndNamesValue);
        currentUserIdInfoDTO.setDepartmentNamesValue(departmentIdAndNamesValue.isEmpty() ? departmentIdAndNamesValue : departmentIdAndNamesValue.split("\\|")[1]);

        String roleIdAndNamesValue = userInfoBO.getRoleIdAndNamesValue();
        currentUserIdInfoDTO.setRoleIdAndNamesValue(roleIdAndNamesValue);
        currentUserIdInfoDTO.setRoleNamesValue(roleIdAndNamesValue.isEmpty() ? roleIdAndNamesValue : roleIdAndNamesValue.split("\\|")[1]);

        String groupIdAndNamesValue = userInfoBO.getGroupIdAndNamesValue();
        currentUserIdInfoDTO.setGroupIdAndNamesValue(groupIdAndNamesValue);
        currentUserIdInfoDTO.setGroupNamesValue(groupIdAndNamesValue.isEmpty() ? groupIdAndNamesValue : groupIdAndNamesValue.split("\\|")[1]);

        String positionIdAndNamesValue = userInfoBO.getPositionIdAndNamesValue();
        currentUserIdInfoDTO.setPositionIdAndNamesValue(positionIdAndNamesValue);
        currentUserIdInfoDTO.setPositionNamesValue(positionIdAndNamesValue.isEmpty() ? positionIdAndNamesValue : positionIdAndNamesValue.split("\\|")[1]);
        return currentUserIdInfoDTO;
    }

}
