package cn.reghao.oss.console.account.controller;

import cn.reghao.jutil.jdk.jvm.JVM;
import cn.reghao.jutil.jdk.jvm.model.JvmInfo;
import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
import cn.reghao.oss.console.account.model.constant.RoleType;
import cn.reghao.oss.console.account.model.po.Menu;
import cn.reghao.oss.console.account.model.po.User;
import cn.reghao.oss.console.account.service.HomeService;
import cn.reghao.oss.console.account.service.UserContext;
import cn.reghao.oss.console.sys.db.repository.SysMessageRepository;
import io.swagger.annotations.Api;
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;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Slf4j
@Api(tags = "登录页和首页")
@Controller
public class AccountAuthController {
    private final JVM jvm;
    private final MachineIdLinux machineId;
    private final HomeService homeService;
    private final SysMessageRepository sysMessageRepository;

    public AccountAuthController(HomeService homeService, SysMessageRepository sysMessageRepository) {
        this.jvm = new JVM();
        this.machineId = new MachineIdLinux();
        this.homeService = homeService;
        this.sysMessageRepository = sysMessageRepository;
    }
    
    @GetMapping("/login")
    public String toLogin(Model model) {
        model.addAttribute("isCaptcha", false);
        return "/login";
    }

    @GetMapping("/")
    public String index(Model model) throws Exception {
        User user = UserContext.getUser();
        if (user == null) {
            throw new Exception("未登录");
        }

        boolean isAdmin = user.getRole().contains(RoleType.ROLE_ADMIN.name());
        if (isAdmin) {
            int unreadTotal = sysMessageRepository.countByUnread(true);
            String unreadMessage;
            if (unreadTotal > 9) {
                unreadMessage = "9+";
            } else {
                unreadMessage = String.valueOf(unreadTotal);
            }
            model.addAttribute("unreadMessage", unreadMessage);
        }

        List<Menu> menus = homeService.userMenus(user.getRole());
        Map<Integer, Menu> treeMenu = homeService.treeMenu(menus);
        model.addAttribute("user", user);
        model.addAttribute("treeMenu", treeMenu);
        return "/main";
    }

    @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();

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

        String template = "/home/index";
        template = "/home/index1";
        return template;
    }
}
