package cn.reghao.devops.mgr.admin.controller;

import cn.reghao.devops.common.util.NotAvailable;
import cn.reghao.devops.common.version.AppVersion;
import cn.reghao.devops.mgr.admin.model.constant.RoleType;
import cn.reghao.devops.mgr.admin.model.po.Menu;
import cn.reghao.devops.mgr.admin.model.po.User;
import cn.reghao.devops.mgr.admin.service.HomeService;
import cn.reghao.devops.mgr.admin.service.UserContext;
import cn.reghao.devops.mgr.admin.service.SysMessageService;
import cn.reghao.devops.mgr.machine.db.query.MachineQuery;
import cn.reghao.devops.mgr.machine.model.vo.MachineStat;
import cn.reghao.jutil.jdk.jvm.JVM;
import cn.reghao.jutil.jdk.jvm.model.JvmInfo;
import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Slf4j
@Api(tags = "登录页和首页")
@Controller
public class HomeController {
    private final JVM jvm;
    private final MachineIdLinux machineId;
    private final AppVersion appVersion;
    private final HomeService homeService;
    private final SysMessageService sysMessageService;
    private final MachineQuery machineQuery;

    public HomeController(HomeService homeService, SysMessageService sysMessageService,
                          MachineQuery machineQuery) {
        this.jvm = new JVM();
        this.machineId = new MachineIdLinux();
        this.appVersion = AppVersion.getVersion();
        this.homeService = homeService;
        this.sysMessageService = sysMessageService;
        this.machineQuery = machineQuery;
    }

    @ApiOperation(value = "帐号登入页面", notes = "N")
    @GetMapping("/login")
    public String toLogin(Model model) {
        model.addAttribute("isCaptcha", false);
        return "/login";
    }

    @ApiOperation(value = "系统主页面", notes = "N")
    @GetMapping("/")
    public String index(Model model) throws Exception {
        User user = UserContext.getUser();
        if (user == null) {
            throw new Exception("未登录");
        }

        boolean isAdmin = user.getRole().getName().equals(RoleType.ROLE_ADMIN.name());
        if (isAdmin) {
            String unreadMessage = sysMessageService.getUnreadCount();
            model.addAttribute("unreadMessage", unreadMessage);
        }

        // TODO 此处直接传入 Role 对象, 然后调用 Role#getMenus 会抛出 LazyInitializationException 异常
        List<Menu> menus = homeService.userMenus(Set.of(user.getRole().getName()));
        Map<Integer, Menu> treeMenu = homeService.treeMenu(menus);
        model.addAttribute("user", user);
        model.addAttribute("treeMenu", treeMenu);
        return "/main";
    }

    @ApiOperation(value = "系统 home 页面", notes = "N")
    @GetMapping("/home")
    public String home(Model model) {
        String commitId = NotAvailable.na.getDesc();
        if (appVersion != null) {
            commitId = appVersion.getCommitId();
        }

        JvmInfo jvmInfo = jvm.info();
        //JvmStat jvmStat = jvm.stat();
        String osInfo = String.format("%s %s", jvmInfo.getOsName(), jvmInfo.getOsVersion());
        String jvmInfo1 = String.format("%s %s", jvmInfo.getJvmName(), jvmInfo.getJvmVersion());
        int pid = jvmInfo.getJvmPid();
        String startAt = jvmInfo.getJvmStartTime();
        String ipv4 = machineId.ipv4();
        List<MachineStat> list = machineQuery.getMachineStats();

        model.addAttribute("mgrVersion", commitId);
        model.addAttribute("hostAddr", ipv4);
        model.addAttribute("osInfo", osInfo);
        model.addAttribute("jvmInfo", jvmInfo1);
        model.addAttribute("pid", pid);
        model.addAttribute("startAt", startAt);
        //model.addAttribute("jvmStat", jvmStat);
        model.addAttribute("list", list);

        return "/home/index";
    }
}
