package com.yzt.airconditionersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.yzt.airconditionersystem.bean.Permission;
import com.yzt.airconditionersystem.bean.RolePermission;
import com.yzt.airconditionersystem.bean.User;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.PermissionService;
import com.yzt.airconditionersystem.service.RolePermissionService;
import com.yzt.airconditionersystem.service.RoleService;
import com.yzt.airconditionersystem.service.UserService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author TzZach
 * @since 2022-05-25 21:09
 */
@Controller
public class PermissionController {

    private static final String PREFIX = "permission/";

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;
    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/permission-add")
    public String permissionAdd(@RequestParam("parentId") Integer parentId,
                                Model model) {
        model.addAttribute("parentId", parentId);
        return PREFIX + "permission-add";
    }

    @GetMapping("/permission-edit")
    public String permissionEdit(@RequestParam("id") Integer id,
                                 Model model) {
        Permission permission = permissionService.getById(id);
        model.addAttribute("permission", permission);
        return PREFIX + "permission-edit";
    }

    /**
     * 获取当前用户所拥有的权限
     * @param userId
     * @return
     */
    @GetMapping("/permission")
    @ResponseBody
    public Result getPermissionByUserId(@RequestParam("id") String userId) {
        User user = userService.getById(userId);
        Integer roleId = user.getRoleId();
        LambdaQueryWrapper<RolePermission> rolePermissionLambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.eq(RolePermission::getRoleId, roleId);
        List<RolePermission> rolePermissionList =
                rolePermissionService.list(rolePermissionLambdaQueryWrapper);
        List<Integer> permissionIds = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissionList) {
            Integer permissionId = rolePermission.getPermissionId();
            permissionIds.add(permissionId);
        }
        LambdaQueryWrapper<Permission> permissionLambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        permissionLambdaQueryWrapper.in(Permission::getId, permissionIds);
        List<Permission> permissionList = permissionService.list();
        return Result.success(permissionList);
    }

    @GetMapping("/permission/tree")
    @ResponseBody
    public Result getPermissionTree() {
        List<Permission> permissionList = permissionService.list();
        List<Permission> childrenList = permissionList.stream()
                .filter(permission -> !permission.getParentId().equals(Permission.NO_PARENT))
                .collect(Collectors.toList());
//        List<Permission> parentList = mapperFacade.mapAsList(permissionList, Permission.class);
//        List<Permission> parentList = Arrays.asList(new Permission[permissionList.size()]);
//        Gson gson = new GsonBuilder().create();
//        String json = gson.toJson(permissionList);
        //        Collections.copy(parentList, permissionList);
//        Permission[] permissions = gson.fromJson(json, Permission[].class);
//        List<Permission> parentList = Arrays.asList(permissions);
        for (Permission parent : permissionList) {
            List<Permission> children = new ArrayList<>();
            for (Permission child : childrenList) {
                if (parent.getId().equals(child.getParentId())) {
                    children.add(child);
                }
            }
            parent.setChildren(children);
        }
        return Result.success(permissionList);
    }

}
