package cn.reghao.autodop.dmaster.rbac.controller.page;

import cn.reghao.autodop.dmaster.rbac.db.query.RoleQuery;
import cn.reghao.autodop.dmaster.rbac.db.query.UserQuery;
import cn.reghao.autodop.dmaster.rbac.model.po.Role;
import cn.reghao.autodop.dmaster.rbac.model.po.User;
import cn.reghao.autodop.dmaster.rbac.model.vo.UserVO;
import cn.reghao.autodop.dmaster.util.db.PageList;
import cn.reghao.autodop.dmaster.util.db.PageSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "用户页面")
@RequestMapping("/rbac/user")
@Controller
public class UserPageController {
    private UserQuery userQuery;
    private RoleQuery roleQuery;

    public UserPageController(UserQuery userQuery, RoleQuery roleQuery) {
        this.userQuery = userQuery;
        this.roleQuery = roleQuery;
    }

    @ApiOperation(value = "用户列表页面")
    @GetMapping
    public String userPage(@RequestParam(value = "nickname", required = false) String nickname, Model model) {
        Page<UserVO> page;
        PageList<UserVO> pageList;
        if (nickname != null) {
            List<UserVO> list = userQuery.getByMatchNickname(nickname);
            page = new PageImpl<>(list);
            pageList = PageList.pageList(page);
        } else {
            PageRequest pageRequest = PageSort.pageRequest();
            page = userQuery.getUserVOByPage(pageRequest);
            pageList = PageList.pageList(page);
        }

        model.addAttribute("page", page);
        model.addAttribute("list", pageList.getList());
        return "/rbac/user/index";
    }

    @ApiOperation(value = "新增用户页面")
    @GetMapping("/add")
    public String addUserPage(Model model) {
        Set<Role> allRoles = new HashSet<>(new HashSet<>(roleQuery.findAll()));
        Set<Role> userRoles = Collections.emptySet();

        model.addAttribute("allRoles", allRoles);
        model.addAttribute("userRoles", userRoles);
        return "/rbac/user/add";
    }

    @ApiOperation(value = "用户信息编辑页面")
    @GetMapping("/edit/{id}")
    public String editUserPage(@PathVariable("id") User user, Model model) {
        Set<Role> allRoles = new HashSet<>(roleQuery.findAll());
        Set<Role> userRoles = userQuery.getUserRoles(user);

        model.addAttribute("allRoles", allRoles);
        model.addAttribute("userRoles", userRoles);
        model.addAttribute("user", user);
        return "/rbac/user/edit";
    }

    @ApiOperation(value = "用户详细信息页面")
    @GetMapping("/detail/{id}")
    public String userDetailPage(@PathVariable("id") int id, Model model) {
        User user = userQuery.findById(id);
        Set<Role> roles = userQuery.getUserRoles(user);
        List<String> names = roles.stream().map(Role::getName).collect(Collectors.toList());

        model.addAttribute("roles", names.toString());
        model.addAttribute("user", user);
        return "/rbac/user/detail";
    }

    @ApiOperation(value = "用户修改密码页面")
    @GetMapping("/passwd/{id}")
    public String modifyPasswordPage(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("id", id);
        return "/rbac/user/passwd";
    }

    @ApiOperation(value = "用户角色分配页面")
    @GetMapping("/role/{id}")
    public String assignRolePage(@PathVariable("id") User user, Model model) {
        Set<Role> roles = new HashSet<>(roleQuery.findAll());
        int userId = user.getId();
        Set<Role> authRoles = userQuery.getUserRoles(userId);

        model.addAttribute("id", userId);
        model.addAttribute("list", roles);
        model.addAttribute("authRoles", authRoles);
        return "/rbac/user/role";
    }
}
