package cn.reghao.bnt.web.admin.service;

import cn.reghao.bnt.web.admin.model.vo.DashboardData;
import cn.reghao.bnt.web.admin.model.vo.SysInfo;
import cn.reghao.jutil.jdk.web.result.NotAvailable;
import cn.reghao.bnt.web.devops.machine.model.vo.MachineStat;
import cn.reghao.bnt.web.devops.machine.service.MachineQuery;
import cn.reghao.jutil.jdk.jvm.JVM;
import cn.reghao.jutil.jdk.jvm.model.JvmInfo;
import cn.reghao.jutil.jdk.machine.id.MachineId;
import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;

/**
 * @author reghao
 * @date 2025-06-03 11:30:49
 */
@Service
public class HomeViewService {
    private final JVM jvm;
    private final MachineId machineId;
    private final AppVersion appVersion;
    private final MachineQuery machineQuery;
    private final RoleService roleService;

    public HomeViewService(MachineQuery machineQuery, RoleService roleService) {
        this.jvm = new JVM();
        this.machineId = new MachineIdLinux();
        this.appVersion = AppVersion.getVersion();
        this.machineQuery = machineQuery;
        this.roleService = roleService;
    }

    /**
     * 根据用户角色渲染其主页内容和侧边栏路由
     * 侧边栏路由渲染在 cn.reghao.bnt.web.template.MenusDirective 中完成
     *
     * @param
     * @return
     * @date 2025-06-03 11:55:32
     */
    public String getHomeView(Model model) {
        String viewPath = roleService.getRolesByUser(UserContext.getUserId()).get(0).getViewPath();
        switch (viewPath) {
            case "/admin/blog/index":
                renderBlogView(model);
                break;
            case "/admin/console/index":
                renderConsoleView(model);
                break;
            case "/admin/devops/index":
                renderDevopsView(model);
                break;
            default:
                renderAdminView(model);
                break;
        }

        return viewPath;
    }

    public void renderBlogView(Model model) {
        renderAdminView(model);
    }

    public void renderConsoleView(Model model) {
        renderAdminView(model);
    }

    public void renderDevopsView(Model model) {
        List<MachineStat> machineStatList = machineQuery.getMachineStats();
        model.addAttribute("machineStatList", machineStatList);

        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("sysVersion", commitId);
        model.addAttribute("hostAddr", ipv4);
        model.addAttribute("osInfo", osInfo);
        model.addAttribute("jvmInfo", jvmInfo1);
        model.addAttribute("startAt", startAt);
        model.addAttribute("pid", pid);

        float freeMemory = (float) Runtime.getRuntime().freeMemory();
        float totalMemory = (float) Runtime.getRuntime().totalMemory();
        float usedMemory = (totalMemory - freeMemory);
        float memPercent = Math.round(freeMemory / totalMemory * 100) ;
        model.addAttribute("freeMemory", freeMemory);
        model.addAttribute("totalMemory", totalMemory / 1024 / 1024);
        model.addAttribute("usedMemory", usedMemory / 1024 / 1024);
        model.addAttribute("memPercent", memPercent);
    }

    public DashboardData getDashboardData() {
        List<MachineStat> machineStatList = machineQuery.getMachineStats();
        String commitId = NotAvailable.na.getDesc();
        if (appVersion != null) {
            commitId = appVersion.getCommitId();
        }

        JvmInfo jvmInfo = jvm.info();
        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();
        SysInfo sysInfo = new SysInfo(commitId, ipv4, osInfo, jvmInfo1, startAt, pid);
        return new DashboardData(machineStatList, sysInfo);
    }

    public void renderAdminView(Model model) {
        List<MachineStat> machineStatList = machineQuery.getMachineStats();
        model.addAttribute("machineStatList", machineStatList);

        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("sysVersion", commitId);
        model.addAttribute("hostAddr", ipv4);
        model.addAttribute("osInfo", osInfo);
        model.addAttribute("jvmInfo", jvmInfo1);
        model.addAttribute("startAt", startAt);
        model.addAttribute("pid", pid);

        float freeMemory = (float) Runtime.getRuntime().freeMemory();
        float totalMemory = (float) Runtime.getRuntime().totalMemory();
        float usedMemory = (totalMemory - freeMemory);
        float memPercent = Math.round(freeMemory / totalMemory * 100) ;
        model.addAttribute("freeMemory", freeMemory);
        model.addAttribute("totalMemory", totalMemory / 1024 / 1024);
        model.addAttribute("usedMemory", usedMemory / 1024 / 1024);
        model.addAttribute("memPercent", memPercent);
    }
}
