package com.izouma.jmrh.web;

import com.alibaba.fastjson.JSONArray;
import com.izouma.jmrh.domain.DataCenterAuthorityMenu;
import com.izouma.jmrh.domain.DataCenterMenu;
import com.izouma.jmrh.repo.DataCenterAuthorityMenuRepo;
import com.izouma.jmrh.repo.DataCenterMenuRepo;
import com.izouma.jmrh.security.DataCenterAuthority;
import com.izouma.jmrh.utils.SecurityUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dataCenterMenu")
public class DataCenterMenuController extends BaseController {
    @Autowired
    private DataCenterMenuRepo          menuRepo;
    @Autowired
    private DataCenterAuthorityMenuRepo authorityMenuRepo;
    @Value("classpath:/static/backlog.json")
    private Resource                    resource;

    /*
    * 登录角色权限菜单
    * */
    @GetMapping("/userMenu")
    public List<DataCenterMenu> userMenu() {
        Set<DataCenterAuthority> authorities = SecurityUtils.getAuthenticatedUser().getDataCenterAuthorities();
        String menuIds = "";
        for (DataCenterAuthority item : authorities) {
            DataCenterAuthorityMenu byAuthorityName = authorityMenuRepo.findByAuthorityName(item.getName());
            if (byAuthorityName != null) {
                if (byAuthorityName.getMenuId() != null && byAuthorityName.getMenuId() != "") {
                    menuIds += "," + byAuthorityName.getMenuId();
                }
            }
        }
        String substring = menuIds.substring(1);
        List<DataCenterMenu> menuList = menuRepo.findByInId(substring);
        //List<Menu> menuList = menuRepo.findByNameAndRootTrue(name);
      /*  for (Menu menu : menuList) {
            sortMenu(menu);
        }*/
        List<DataCenterMenu> child = getChild(12542L, menuList);
        Collections.sort(child);
        return child;
    }

    /*
     * 待办列表菜单
     * */
    @GetMapping("/userMenuList")
    public List<Map<String, String>> userMenuList() {
        Set<DataCenterAuthority> authorities = SecurityUtils.getAuthenticatedUser().getDataCenterAuthorities();
        String menuIds = "";
        for (DataCenterAuthority item : authorities) {
            DataCenterAuthorityMenu byAuthorityName = authorityMenuRepo.findByAuthorityName(item.getName());
            if (byAuthorityName != null) {
                if (byAuthorityName.getMenuId() != null && byAuthorityName.getMenuId() != "") {
                    menuIds += "," + byAuthorityName.getMenuId();
                }
            }
        }
        String substring = menuIds.substring(1);
        List<DataCenterMenu> menuList = menuRepo.findByInId(substring);
        List<Map<String, String>> hashMaps = new ArrayList<>();
        try {
            String areaData = IOUtils.toString(resource.getInputStream(), Charset.forName("UTF-8"));
            List<Map<String, String>> list = JSONArray.parseObject(areaData, List.class);
            for (DataCenterMenu menu : menuList) {
                for (Map<String, String> map : list) {
                    if (menu.getPath().equals(map.get("url"))) {
                        String url = menu.getPath().substring(0, menu.getPath().indexOf("List"));
                        map.put("listAll", url + "/all");
                        map.put("countUrl", url + "/count");
                        hashMaps.add(map);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return hashMaps;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public List<DataCenterMenu> all() {
        List<DataCenterMenu> menuList = menuRepo.findByEnabledTrue();
        //menuList.forEach(this::sortMenu);
        return getChild(0L, menuList);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/allUserMenu")
    public Map allUserMenu(String authorityName) {
        List<DataCenterMenu> menuList = menuRepo.findByEnabledTrue();
        DataCenterAuthorityMenu byAuthorityName = authorityMenuRepo.findByAuthorityName(authorityName);
        HashMap mode = new HashMap<String, Objects>();
        mode.put("menuList", getChild(0L, menuList));
        if (byAuthorityName != null) {
            String menuId = byAuthorityName.getMenuId();
            String[] split = menuId.split(",");
            mode.put("menuId", split);
        }
        return mode;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public DataCenterMenu save(DataCenterMenu menu) {
        if (menu.getEnabled() == null) {
            menu.setEnabled(true);
        }
        if (menu.getSort() == null) {
            menu.setSort(menuRepo.nextSort());
        }
        menuRepo.save(menu);
        return menu;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/groupByCategory")
    public Map<String, List<DataCenterMenu>> groupByCategory() {
        return menuRepo.findAll().stream()
                .filter(menu -> menu.getCategory() != null)
                .collect(Collectors.groupingBy(DataCenterMenu::getCategory));
    }

    private void sortMenu(DataCenterMenu menu) {
        if (menu.getChildren() != null) {
            menu.getChildren().sort(Comparator.comparingInt(DataCenterMenu::getSort));
        }
        menu.getChildren().forEach(this::sortMenu);
    }

    private static List<DataCenterMenu> getChild(Long pid, List<DataCenterMenu> allList) {
        ArrayList<DataCenterMenu> child = new ArrayList<>();
        for (DataCenterMenu menu : allList) {
            if (pid.longValue() == menu.getParent().longValue()) {
                menu.setChildren(getChild(menu.getId(), allList));
                if (menu.getChildren().size() > 0) {
                    Collections.sort(menu.getChildren());
                    /*menu.getChildren().sort(Comparator.comparingInt(Menu::getSort));*/
                }
                child.add(menu);
            }
        }
        return child;
    }
}
