package ink.wgink.module.menu.init;

import com.alibaba.fastjson2.JSON;
import ink.wgink.interfaces.init.IInitBaseMenu;
import ink.wgink.interfaces.init.build.IBuildServiceMenu;
import ink.wgink.interfaces.menu.IMenuBaseService;
import ink.wgink.module.menu.dao.IMenuDao;
import ink.wgink.pojo.bos.menu.InitMenuBO;
import ink.wgink.pojo.dtos.menu.MenuDTO;
import ink.wgink.util.ResourceUtil;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: InitMenu
 * @Description: 初始化菜单
 * @Author: wanggeng
 * @Date: 2021/6/15 4:20 下午
 * @Version: 1.0
 */
@Component
public class InitMenu implements IInitBaseMenu {

    @Autowired
    private IMenuDao menuDao;
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void init() {
        try {
            InputStream initMenuJsonInputStream = ResourceUtil.getJarResourceInputStream("json/init-menu.json");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(initMenuJsonInputStream));
            StringBuffer jsonSB = new StringBuffer();
            for (String line; (line = bufferedReader.readLine()) != null; ) {
                jsonSB.append(line);
            }
            List<InitMenuBO> initMenus = JSON.parseArray(jsonSB.toString(), InitMenuBO.class);
            // 构建业务菜单列表
            Map<String, IBuildServiceMenu> buildServiceMenuMap = applicationContext.getBeansOfType(IBuildServiceMenu.class);
            buildServiceMenuMap.forEach((k, v) -> {
                v.buildMenu(initMenus);
            });
            // 构建编码
            buildMenuCode("0001", initMenus);
            // 创建根节点
            Map<String, Object> params = new HashMap<>(20);
            String gmt = DateUtil.getTime();
            params.put("menuCode", "0001");
            MenuDTO menuDTO = menuDao.getSimple(params);
            String menuId = IMenuBaseService.MENU_UNIFIED_USER;
            if (menuDTO == null) {
                params.put("menuId", menuId);
                params.put("menuParentId", "0");
                params.put("menuName", "统一用户系统");
                params.put("menuSummary", "统一用户系统");
                params.put("menuUrl", "javascript:void(0);");
                params.put("menuType", "1");
                params.put("menuIcon", "fa-icon-color-white fa fa-cogs");
                params.put("menuOrder", "1");
                params.put("menuStatus", "0");
                params.put("openType", "1");
                params.put("gmtCreate", gmt);
                params.put("gmtModified", gmt);
                params.put("creator", "1");
                params.put("modifier", "1");
                params.put("isDelete", 0);
                menuDao.save(params);
            }
            // 创建菜单
            createMenu(menuId, initMenus);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void buildMenuCode(String parentMenuCode, List<InitMenuBO> initMenus) {
        for (int i = 0; i < initMenus.size(); i++) {
            String indexMenuCode = String.format("%04d", (i + 1));
            String menuCode = parentMenuCode + indexMenuCode;
            InitMenuBO initMenu = initMenus.get(i);
            initMenu.setMenuCode(menuCode);
            if (initMenu.getSubMenus().isEmpty()) {
                continue;
            }
            buildMenuCode(menuCode, initMenu.getSubMenus());
        }
    }

    /**
     * 创建菜单
     *
     * @param menuParentId
     * @param initMenus
     */
    private void createMenu(String menuParentId, List<InitMenuBO> initMenus) {
        String gmt = DateUtil.getTime();
        for (int i = 0; i < initMenus.size(); i++) {
            InitMenuBO initMenu = initMenus.get(i);
            Map<String, Object> params = new HashMap<>();
            params.put("menuCode", initMenu.getMenuCode());
            MenuDTO menuDTO = menuDao.getSimple(params);
            String menuId;
            if (menuDTO == null) {
                menuId = UUIDUtil.getUUID();
                params.put("menuId", menuId);
                params.put("menuParentId", menuParentId);
                params.put("menuName", initMenu.getMenuName());
                params.put("menuSummary", initMenu.getMenuSummary());
                params.put("menuUrl", initMenu.getMenuUrl());
                params.put("menuType", initMenu.getMenuType());
                params.put("menuIcon", initMenu.getMenuIcon());
                params.put("openType", 1);
                params.put("menuStatus", 0);
                params.put("menuOrder", i);
                params.put("gmtCreate", gmt);
                params.put("gmtModified", gmt);
                params.put("creator", "1");
                params.put("modifier", "1");
                params.put("isDelete", 0);
                menuDao.save(params);
            } else {
                menuId = menuDTO.getMenuId();
            }
            if (initMenu.getSubMenus().isEmpty()) {
                continue;
            }
            createMenu(menuId, initMenu.getSubMenus());
        }
    }

}
