package com.dtok.service.impl;

import com.dtok.entity.Permission;
import com.dtok.entity.RolePermission;
import com.dtok.entity.params.PermissionNode;
import com.dtok.repository.AccountRepository;
import com.dtok.repository.PermissionRepository;
import com.dtok.repository.RolePermissionRepository;
import com.dtok.service.PermissionService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionImpl implements PermissionService {

    @Autowired
    RolePermissionRepository rolePermissionRepository;

    @Autowired
    AccountRepository userRepository;

    @Autowired
    PermissionRepository permissionRepository;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void authPermission(List<PermissionNode> permissions, String roleCode) {
        rolePermissionRepository.deleteAllByRoleCode(roleCode);
        List<RolePermission> rolePermissions = new ArrayList<>();
        for (PermissionNode permission : permissions) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleCode(roleCode);
            rolePermission.setPmCode(permission.getCode());
            rolePermissions.add(rolePermission);
            for (PermissionNode permissionChild : permission.getChildren()) {
                RolePermission rolePermission_ = new RolePermission();
                rolePermission_.setRoleCode(roleCode);
                rolePermission_.setPmCode(permissionChild.getCode());
                rolePermissions.add(rolePermission_);
            }
        }
        rolePermissionRepository.saveAll(rolePermissions);
    }


    @Override
    public List<Permission> getTreeMenu() {
        List<Permission> permissions = permissionRepository.findAllByIsMenuOrderBySeqAsc(true);
        List<Permission> parentPermission = permissions.stream().filter((item) -> item.getLevel() == 0).collect(Collectors.toList());
        parentPermission.forEach((p) -> {
            p.setChildren(getChild(permissions, p.getCode()));
        });
        return parentPermission;
    }

    @Override
    public List<Permission> getTree() {
        List<Permission> permissions = permissionRepository.findAll(Sort.by(Sort.Direction.ASC, "seq"));
        List<Permission> parentPermission = permissions.stream().filter((item) -> item.getLevel() == 0).collect(Collectors.toList());
        parentPermission.forEach((p) -> {
            p.setChildren(getChild(permissions, p.getCode()));
        });
        return parentPermission;
    }

    @Override
    public List<Permission> getTree(String userCode) {
        List<Permission> permissions = permissionRepository.findPermissionMenuByUsername(userCode);
        List<Permission> parentPermission = permissions.stream().filter((item) -> item.getLevel() == 0).collect(Collectors.toList());
        parentPermission.forEach((p) -> {
            p.setChildren(getChild(permissions, p.getCode()));
        });
        return parentPermission;
    }

    public List<Permission> getChild(List<Permission> permissions, String parentCode) {
        List<Permission> treeNodes = new ArrayList<>();
        for (Permission p : permissions.stream().filter((p) -> p.getParent() != null && p.getParent().equals(parentCode)).collect(Collectors.toList())) {
            treeNodes.add(p);

            p.setChildren(getChild(permissions, p.getCode()));
        }
        return treeNodes;
    }


}
