package com.jwipc.sys.logic.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.sys.constant.DealtTargetCategroy;
import com.jwipc.sys.constant.MenuLanguageType;
import com.jwipc.sys.constant.MenuType;
import com.jwipc.sys.constant.SysConfigStatus;
import com.jwipc.sys.convert.ResourceConvertConvert;
import com.jwipc.sys.model.dto.DwtItemDto;
import com.jwipc.sys.dto.HomePageAttrDTO;
import com.jwipc.sys.dto.MenuTreeDTO;
import com.jwipc.sys.entity.DealtWithTargetRole;
import com.jwipc.sys.entity.SysConfig;
import com.jwipc.sys.logic.HomeLogic;
import com.jwipc.sys.model.dto.WorkbenchDwtDto;
import com.jwipc.sys.model.po.MenuTreePO;
import com.jwipc.sys.service.*;
import com.jwipc.sys.util.TreeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class HomeLogicImpl<dealtWithTargetTypeService> implements HomeLogic {
    private final IResourcesService resourcesService;
    private final ResourceConvertConvert resourceConvertConvert;
    private final ISysConfigService sysConfigService;
    private final IDealtWithTargetRoleService dealtWithTargetRoleService;
    private final IDealtWithTargetInfoService dealtWithTargetInfoService;
    private final IDealtWithTargetTypeService dealtWithTargetTypeService;

    @Override
    public HomePageAttrDTO renderHanlder(List<Long> roleIds, List<String> sysConfigKeys) {
        // 用户菜单信息
        List<MenuTreeDTO> list = Lists.newArrayList();
        if (CollUtil.isNotEmpty(roleIds)) {
            List<MenuTreePO> mps = resourcesService.findMenuTreeByIds(roleIds, MenuType.MENU_PC.getType(),
                    MenuLanguageType.CHINESE.getType());
            list.addAll(resourceConvertConvert.treePO2TreeDTO(mps));
        }
        TreeUtil treeUtil = new TreeUtil();
        List<MenuTreeDTO> ns = treeUtil.getChildMenuTreeDTOs(list, 0L);
        List<MenuTreeDTO> displayMenuData = new ArrayList<>();
        List<MenuTreeDTO> moreMenuData = Lists.newArrayList();
        int kk = ns.size();
        if (kk > 5) {
            kk = 5;
        }
        for (int i = 0; i < kk; i++) {
            displayMenuData.add(ns.get(i));
        }
        if (ns.size() > 5) {
            for (int ii = 5; ii < ns.size(); ii++) {
                moreMenuData.add(ns.get(ii));
            }
        }
        Map<String, String> map = Maps.newHashMap();
        final LambdaQueryWrapper<SysConfig> sysConfigLambdaQueryWrapper = new LambdaQueryWrapper<SysConfig>().eq(
                SysConfig::getStatus, SysConfigStatus.ENABLE.getStatus()).in(SysConfig::getConfigKey, sysConfigKeys);
        final List<SysConfig> sysConfigs = sysConfigService.list(sysConfigLambdaQueryWrapper);

        for (SysConfig sc : sysConfigs) {
            map.put(sc.getConfigKey(), sc.getConfigValue());
        }

        for (String key : sysConfigKeys) {
            if (StringUtils.isEmpty(map.get(key))) {
                map.put(key, "");
            }
        }
        final HomePageAttrDTO.HomePageAttrDTOBuilder builder = HomePageAttrDTO.builder();
        builder.ns(ns);
        builder.displayMenuData(displayMenuData);
        builder.moreMenuData(moreMenuData);
        builder.configs(map);
        return builder.build();
    }

    @Override
    public boolean checkUserAllotDwt(Long userId, DealtTargetCategroy dealtTargetCategroy) {
        List<DealtWithTargetRole> roles = dealtWithTargetRoleService.getUserDwt(userId, dealtTargetCategroy.getType());
        return CollUtil.isNotEmpty(roles);
    }
}