package com.jwipc.sys.controller.pc;

import cn.hutool.core.lang.Pair;
import com.jwipc.sys.dto.RoleDTO;
import com.jwipc.sys.dto.RoleEditDTO;
import com.jwipc.sys.logic.RoleLogic;
import com.jwipc.sys.logic.UserLogic;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

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

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/role")
@RequiredArgsConstructor
@Slf4j
public class RolePageController {
    private final UserLogic userLogic;
    private final RoleLogic roleLogic;

    @GetMapping("index")
    public String index() {
        return MODULE_ROOT + "/role/index";
    }

    @GetMapping("supplier")
    public String supplier() {
        return MODULE_ROOT + "/role/supplier_role";
    }


    @GetMapping("add")
    public String add(@RequestParam(required = false, defaultValue = "0") Integer supplierFlag, Model model) {
        model.addAttribute("supplierFlag", supplierFlag);
        return MODULE_ROOT + "/role/add";
    }

    @GetMapping("edit")
    public String edit(Long id, Model model, @RequestParam(required = false, defaultValue = "0") Integer supplierFlag) {
        model.addAttribute("id", model);
        model.addAttribute("supplierFlag", supplierFlag);
        RoleEditDTO roleEditDTO = roleLogic.getForEdit(id);
        model.addAttribute("role", roleEditDTO);
        return MODULE_ROOT + "/role/edit";
    }

    @GetMapping("userRoleAdd")
    public String userRoleAdd(Model model, Long userId) {

        Pair<List<RoleDTO>, List<RoleDTO>> result = userLogic.getForUserRoleAdd(userId);
        final List<RoleDTO> userRole = result.getKey();
        model.addAttribute("userRole", userRole);
        model.addAttribute("txtRoleSelect", userRole.stream().map(RoleDTO::getId).map(String::valueOf).collect(
                Collectors.joining(",")));
        model.addAttribute("role", result.getValue());
        model.addAttribute("userId", userId);
        return MODULE_ROOT + "/role/user_role_add";
    }

    @GetMapping("permissions")
    public String permissions(Long id, Model model, @RequestParam(required = false, defaultValue = "1") Integer type) {
        Pair<List<String>, List<String>> result = roleLogic.getForPermissions(id);
        model.addAttribute("id", id);
        model.addAttribute("resources", result.getKey().toString());
        model.addAttribute("buttons", result.getValue().toString());
        model.addAttribute("type", type);
        return MODULE_ROOT + "/role/permissions";
    }

    @GetMapping("dealtConfig")
    public String dealtConfig(Long id, Model model) {
        List<Long> result = roleLogic.getDealtConfig(id);
        model.addAttribute("id", id);
        model.addAttribute("resources", result.toString());
        return MODULE_ROOT + "/role/dealt_config";
    }
}
