package ink.wgink.login.oauth2.client.controller.route;

import ink.wgink.common.component.SecurityComponent;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.oauth2.client.service.menu.IMenuService;
import ink.wgink.pojo.bos.UserInfoBO;
import ink.wgink.pojo.dtos.menu.MenuDTO;
import ink.wgink.properties.oauth2.client.OAuth2ClientProperties;
import ink.wgink.properties.oauth2.client.OAuth2ClientServerProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: IndexRouteController
 * @Description: index
 * @Author: wanggeng
 * @Date: 2021/2/27 3:38 下午
 * @Version: 1.0
 */
@Controller
public class IndexRouteController {

    @Autowired
    private SecurityComponent securityComponent;
    @Autowired
    private OAuth2ClientServerProperties oAuth2ClientServerProperties;
    @Autowired
    private OAuth2ClientProperties oAuth2ClientProperties;
    @Autowired
    private IMenuService menuService;

    @GetMapping("index")
    public ModelAndView goIndex() {
        if (!StringUtils.isBlank(oAuth2ClientServerProperties.getDefaultIndexPage())) {
            return new ModelAndView(new RedirectView(oAuth2ClientServerProperties.getDefaultIndexPage()));
        }
        return new ModelAndView("forward:/default-main");
    }

    /**
     * 默认主页
     *
     * @return
     */
    @GetMapping("default-main")
    public ModelAndView defaultMain() {
        ModelAndView mv = new ModelAndView("default-main");
        UserInfoBO userInfoBO = securityComponent.getCurrentUser();
        mv.addObject("userUsername", userInfoBO.getUserUsername());
        Map<String, Object> config = new HashMap<>();
        // 先加载系统短标题，没有加载主标题，没有加载配置文件系统标题
        mv.addObject("systemTitle", oAuth2ClientServerProperties.getSystemTitle());
        mv.addObject("navPage", oAuth2ClientServerProperties.getNavPage());

        List<MenuDTO> menus;
        if (securityComponent.isAdmin()) {
            menus = menuService.listAll(oAuth2ClientProperties.getClient().getClientId(), ISystemConstant.ADMIN);
        } else {
            menus = menuService.listAll(oAuth2ClientProperties.getClient().getClientId(), securityComponent.getCurrentUser().getRoles());
        }
        mv.addObject("menus", menus);
        return mv;
    }

    /**
     * 默认导航首页
     *
     * @return
     */
    @GetMapping("default-home")
    public ModelAndView defaultHome() {
        ModelAndView mv;
        if (!StringUtils.isBlank(oAuth2ClientServerProperties.getDefaultHomePage())) {
            mv = new ModelAndView(new RedirectView(oAuth2ClientServerProperties.getDefaultHomePage()));
        } else {
            mv = new ModelAndView("default-home");
        }
        return mv;
    }

}
