package com.hzs.system.config.controller.api;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.constant.SysConstants;
import com.hzs.common.core.enums.EMenu;
import com.hzs.common.core.enums.EMenuDetail;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.domain.system.config.BdMenuDetail;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.config.service.IBdMenuDetailService;
import com.hzs.system.config.vo.ApiMenuVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 前端菜单权限控制器
 */
@RestController
@RequestMapping("/api/menu")
public class ApiBdMenuController {

    @Autowired
    private IBdMenuDetailService iBdMenuDetailService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    /**
     * 获取菜单权限列表
     *
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list() {
        // 当前用户ID
        Long userId = SecurityUtils.getUserId();

        // 返回菜单显示
        List<ApiMenuVO> resultList = new ArrayList<>();

        CuMemberExt cuMember = iMemberServiceApi.getMenuMember(userId).getData();
        if (null == cuMember) {
            // 不在正常用户树上，此处不做作任何返回
            return AjaxResult.success(resultList);
        }
        // 结算等级
        int pkGrade = cuMember.getPkSettleGrade();
        // 真实奖衔
        int pkAwards = cuMember.getPkAwards();
        // 注册权限
        int authority = cuMember.getRegisterAuthority();

        // 续约标记 -- 会员还有3月到期，需要能看到续约专区
        boolean expireFlag = false;
        if (null != cuMember.getExpireDate()) {
            expireFlag = DateUtils.addMonths(new Date(), 3).compareTo(cuMember.getExpireDate()) > 0;
        }

        boolean isLex = true;
        boolean isLexRegister = true;

        // 查询配置菜单列表
        List<BdMenuDetail> list = iBdMenuDetailService.showMenuDetail(pkGrade, pkAwards, authority, SecurityUtils.getPkCountry());

        if (CollectionUtil.isNotEmpty(list)) {
            // 如果有配置数据，则按配置显示
            for (BdMenuDetail bdMenuDetail : list) {
                EMenu eMenu = EMenu.getEnumByValue(bdMenuDetail.getDetailValue());
                if (null != eMenu) {
                    if (SysConstants.SPECIAL_CODE.equals(cuMember.getMemberCode())) {
                        // 特殊账号，单独处理隐藏 市场动态 不显示
                        if (!EMenu.specialList().contains(eMenu.getValue())) {
                            continue;
                        }
                    }

                    resultList.add(ApiMenuVO.builder()
                            .menuKey(eMenu.getMenuKey())
                            .menu(eMenu.getValue())
                            .menuParent(0)
                            .menuVal(eMenu.getLabel())
                            .build());
                    continue;
                }
                EMenuDetail eMenuDetail = EMenuDetail.getEnumByValue(bdMenuDetail.getDetailValue());
                if (null != eMenuDetail) {
                    if (SysConstants.SPECIAL_CODE.equals(cuMember.getMemberCode())) {
                        // 特殊账号，部分菜单不显示
                        if (!EMenuDetail.specialList().contains(eMenuDetail.getValue())) {
                            continue;
                        }
                    }

                    if (EMenuDetail.RENEWAL_AREA.getValue() == eMenuDetail.getValue()
                            && !expireFlag
                    ) {
                        // 2024.06.18 新需求，不到差3个月续约，不能看到续约专区
                        continue;
                    }

                    handleMenuDetail(isLex, resultList, eMenuDetail, isLexRegister);
                }
            }
        } else {
            // 如果没有配置数据，则显示全部
            for (EMenu eMenu : EMenu.values()) {
                if (SysConstants.SPECIAL_CODE.equals(cuMember.getMemberCode())) {
                    // 特殊账号，单独处理隐藏 市场动态 不显示
                    if (!EMenu.specialList().contains(eMenu.getValue())) {
                        continue;
                    }
                }

                resultList.add(ApiMenuVO.builder()
                        .menuKey(eMenu.getMenuKey())
                        .menu(eMenu.getValue())
                        .menuParent(0)
                        .menuVal(eMenu.getLabel())
                        .build());
            }
            for (EMenuDetail eMenuDetail : EMenuDetail.values()) {
                if (SysConstants.SPECIAL_CODE.equals(cuMember.getMemberCode())) {
                    // 特殊账号，部分菜单不显示
                    if (!EMenuDetail.specialList().contains(eMenuDetail.getValue())) {
                        continue;
                    }
                }

                if (EMenuDetail.RENEWAL_AREA.getValue() == eMenuDetail.getValue()
                        && !expireFlag
                ) {
                    // 2024.06.18 新需求，不到差3个月续约，不能看到续约专区
                    continue;
                }

                handleMenuDetail(isLex, resultList, eMenuDetail, isLexRegister);
            }
        }

        return AjaxResult.success(resultList);
    }

    /**
     * 处理菜单详情
     *
     * @param isLex         是否乐学易考
     * @param resultList    返回菜单显示数据
     * @param eMenuDetail   菜单明细
     * @param isLexRegister 是否显示乐学注册、乐学注册
     */
    private void handleMenuDetail(boolean isLex, List<ApiMenuVO> resultList, EMenuDetail eMenuDetail, boolean isLexRegister) {
        switch (eMenuDetail) {
            case REGISTER_LXYK:
            case UPGRADE_LXYK:
                if (isLexRegister) {
                    resultList.add(ApiMenuVO.builder()
                            .menuKey(eMenuDetail.getMenuKey())
                            .menu(eMenuDetail.getValue())
                            .menuParent(eMenuDetail.getParent())
                            .menuVal(eMenuDetail.getLabel())
                            .build());
                }
                break;
            case REPURCHASE_LXYK:
                // 乐学易考（复购）
                if (isLex) {
                    // 当前用户是乐学易考伞下
                    resultList.add(ApiMenuVO.builder()
                            .menuKey(eMenuDetail.getMenuKey())
                            .menu(eMenuDetail.getValue())
                            .menuParent(eMenuDetail.getParent())
                            .menuVal(eMenuDetail.getLabel())
                            .build());
                }
                break;
            default:
                resultList.add(ApiMenuVO.builder()
                        .menuKey(eMenuDetail.getMenuKey())
                        .menu(eMenuDetail.getValue())
                        .menuParent(eMenuDetail.getParent())
                        .menuVal(eMenuDetail.getLabel())
                        .build());
        }
    }

}
