package org.linlinjava.jz.admin;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.linlinjava.jz.admin.service.LogHelper;
import org.linlinjava.jz.admin.util.Permission;
import org.linlinjava.jz.admin.util.PermissionUtil;
import org.linlinjava.jz.admin.web.AdminStorageController;
import org.linlinjava.jz.db.domain.SysUser;
import org.linlinjava.jz.db.service.JzPermissionService;
import org.linlinjava.jz.db.service.JzRoleService;
import org.linlinjava.jz.db.service.JzStorageService;
import org.linlinjava.jz.db.service.JzSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.*;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class LoginTest {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private JzSysUserService adminService;
    @Autowired
    private JzRoleService roleService;
    @Autowired
    private JzPermissionService permissionService;

    @Autowired
    private JzStorageService storageService;
    @Test
    public  void testStorage(){
        AdminStorageController controller=new AdminStorageController();
    }

    @Test
    public void  test() {
        String username = "user123";
        String password = "user123";
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken u = new UsernamePasswordToken(username, password);

    }

    @Test
    public void testinfo(){
        String username = "user123";
        String password = "user123";
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken u = new UsernamePasswordToken(username, password);

        SysUser admin = (SysUser) currentUser.getPrincipal();

        Map<String, Object> data = new HashMap<>();
        data.put("name", admin.getUserName());
        data.put("avatar", admin.getAvatar());

        Integer[] roleIds = admin.getRoleIds();
        Set<String> roles = roleService.queryByIds(roleIds);
        Set<String> permissions = permissionService.queryByRoleIds(roleIds);
        data.put("roles", roles);
        // NOTE
        // 这里需要转换perms结构，因为对于前端而已API形式的权限更容易理解
        data.put("perms", toApi(permissions));

    }

    private HashMap<String, String> systemPermissionsMap = null;

    private Collection<String> toApi(Set<String> permissions) {
        if (systemPermissionsMap == null) {
            systemPermissionsMap = new HashMap<>();
            final String basicPackage = "org.linlinjava.jz.admin";
            List<Permission> systemPermissions = PermissionUtil.listPermission(context, basicPackage);
            for (Permission permission : systemPermissions) {
                String perm = permission.getRequiresPermissions().value()[0];
                String api = permission.getApi();
                systemPermissionsMap.put(perm, api);
            }
        }

        Collection<String> apis = new HashSet<>();
        for (String perm : permissions) {
            String api = systemPermissionsMap.get(perm);
            apis.add(api);

            if (perm.equals("*")) {
                apis.clear();
                apis.add("*");
                return apis;
                //                return systemPermissionsMap.values();

            }
        }
        return apis;
    }

}
