package cn.fateverse.admin.facade;

import cn.fateverse.admin.convert.UserConvert;
import cn.fateverse.admin.query.UserQuery;
import cn.fateverse.admin.service.*;
import cn.fateverse.admin.vo.UserChooseVo;
import cn.fateverse.admin.vo.UserDetailVo;
import cn.fateverse.admin.vo.UserVo;
import cn.fateverse.common.core.entity.Dept;
import cn.fateverse.common.core.entity.Role;
import cn.fateverse.common.core.entity.User;
import cn.fateverse.common.core.entity.UserDetail;
import cn.fateverse.common.core.exception.CustomException;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.mybatisplus.utils.PageConditionUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserFacade {

    private final UserService userService;
    private final UserRoleService userRoleService;
    private final UserPostService userPostService;
    private final DeptService deptService;
    private final RoleService roleService;


    /**
     * 按用户名搜索
     *
     * @param username 用户名
     * @return {@link UserVo }
     */
    public UserVo searchByUserName(String username) {
        User user = userService.searchByUserName(username);
        return UserConvert.INSTANCE.entityToVo(user);
    }

    /**
     * 按用户id搜索
     *
     * @param userId 用户id
     * @return {@link UserVo }
     */
    public UserVo searchByUserId(Long userId) {
        User user = userService.getById(userId);
        return UserConvert.INSTANCE.entityToVo(user);
    }

    public UserDetail getUserDetailsByUserName(String username) {
        User user = userService.searchByUserName(username);
        return buildUserDetail(user);
    }

    /**
     * 构建用户细节
     *
     * @param user 用户
     * @return {@link UserDetail }
     */
    private UserDetail buildUserDetail(User user) {
        if (ObjectUtil.isNull(user)) {
            return null;
        }
        UserDetail userDetail = UserConvert.INSTANCE.entityToDetail(user);

        Dept dept = deptService.getById(user.getDeptId());
        if (ObjectUtil.isNotNull(dept)) {
            userDetail.setDept(dept);
        }
        List<Role> roleList = userRoleService.selectRoleByUserId(user.getUserId());
        if (CollUtil.isNotEmpty(roleList)) {
            userDetail.setRoles(roleList);
        }
        return userDetail;
    }

    /**
     * 搜索列表
     *
     * @param userQuery 用户查询
     * @return {@link List }<{@link UserVo }>
     */
    public TableDataInfo<UserVo> searchList(UserQuery userQuery) {
        IPage<UserVo> page = userService.searchList(userQuery);
        return PageConditionUtil.convertDataTable(page);
    }

    /**
     * 搜索用户选择角色或部门
     *
     * @param type     类型
     * @param chooseId 选择id
     * @return {@link List }<{@link UserChooseVo }>
     */
    public List<UserChooseVo> searchUserChooseRoleOrDept(Integer type, Long chooseId) {
        switch (type) {
            // 0代表角色
            case 0:
                return chooseRole(chooseId);
            // 1代表部门
            case 1:
                return chooseDept(chooseId);
            default:
                throw new CustomException("参数异常");
        }
    }

    /**
     * 选择角色
     *
     * @param roleId 角色id
     * @return 选择成功的用户信息
     */
    private List<UserChooseVo> chooseRole(Long roleId) {
        if (roleId.equals(0L)) {
            List<Role> roleList = roleService.list();
            return roleList.stream().map(UserChooseVo::toUserChooseByRole).collect(Collectors.toList());
        } else {
            List<UserVo> userList = userService.selectUserListByRoleId(roleId, null, null);
            return userList.stream().map(user ->
                    UserChooseVo.toUserChooseByUser(user, roleId)
            ).collect(Collectors.toList());
        }
    }


    /**
     * 选择部门
     *
     * @param deptId
     * @return
     */
    private List<UserChooseVo> chooseDept(Long deptId) {
        List<Dept> deptList = deptService.selectListByDeptParentId(deptId);
        List<UserChooseVo> result = deptList.stream().map(dept -> UserChooseVo.toUserChooseByDept(dept, deptId)).collect(Collectors.toList());
        List<UserVo> userList = userService.selectByDeptIds(Collections.singletonList(deptId));
        userList.forEach(user -> result.add(UserChooseVo.toUserChooseByUser(user, deptId)));
        return result;
    }

    /**
     * 按用户id搜索详细信息
     *
     * @param userId 用户id
     * @return {@link UserDetailVo }
     */
    public UserDetailVo searchDetailByUserId(Long userId) {
        User user = userService.getById(userId);
        UserDetail userDetail = buildUserDetail(user);
        UserDetailVo detailVo = new UserDetailVo();
        detailVo.setUser(userDetail);
        return detailVo;
    }
}
