package cn.reghao.devops.web.ws.event;

import cn.reghao.devops.web.mgr.app.db.query.AppDeployQuery;
import cn.reghao.devops.web.mgr.app.service.AppRunService;
import cn.reghao.devops.common.agent.app.dto.AppInfo;
import cn.reghao.devops.common.msg.event.EvtAgentHeartbeat;
import cn.reghao.devops.common.msg.event.EvtAgentStart;
import cn.reghao.devops.web.mgr.machine.db.repository.MachineHostRepository;
import cn.reghao.devops.web.mgr.machine.db.repository.MachineInfoRepository;
import cn.reghao.devops.web.mgr.machine.model.po.MachineHost;
import cn.reghao.devops.web.mgr.machine.model.po.MachineInfo;
import cn.reghao.jutil.jdk.machine.data.detail.DiskDetail;
import cn.reghao.jutil.jdk.machine.data.detail.MemoryDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author reghao
 * @date 2021-10-15 16:39:48
 */
@Slf4j
@Service
public class MachineEventService {
    private final MachineInfoRepository machineInfoRepository;
    private final MachineHostRepository machineHostRepository;
    private final AppDeployQuery appDeployQuery;
    private final AppRunService appRunService;

    public MachineEventService(MachineInfoRepository machineInfoRepository, MachineHostRepository machineHostRepository,
                               AppDeployQuery appDeployQuery, AppRunService appRunService) {
        this.machineInfoRepository = machineInfoRepository;
        this.machineHostRepository = machineHostRepository;
        this.appDeployQuery = appDeployQuery;
        this.appRunService = appRunService;
    }

    @Transactional(rollbackFor = Exception.class)
    public void start(EvtAgentStart evtAgentStart) {
        String machineId = evtAgentStart.getMachineId();
        long timestamp = evtAgentStart.getTimestamp();

        MachineInfo machineInfo = new MachineInfo(evtAgentStart);
        MachineInfo infoEntity = machineInfoRepository.findByMachineId(machineId);
        if (infoEntity == null) {
            infoEntity = machineInfoRepository.save(machineInfo);
            machineHostRepository.save(new MachineHost(infoEntity));
        } else {
            machineInfo.setId(infoEntity.getId());
            machineInfo.setCreateTime(infoEntity.getCreateTime());
            machineInfoRepository.save(machineInfo);

            MachineHost hostEntity = machineHostRepository.findByMachineId(machineId);
            String machineIpv4 = infoEntity.getMachineIpv4();
            if (!hostEntity.getMachineIpv4().equals(machineIpv4)) {
                hostEntity.setMachineIpv4(machineIpv4);
                machineHostRepository.save(hostEntity);
            }
        }

        // 向 agent 发送 EvtAppStat 事件, 获取应用的状态
        List<AppInfo> list = appDeployQuery.getAppsByMachineId(machineId);
        if (!list.isEmpty()) {
            appRunService.getStat(machineId, list);
        }
    }

    // TODO 根据 EvtAgentHeartbeat 中的信息检查系统负载，CPU，内存，磁盘等使用情况(暂未实现)
    public void heartbeat(EvtAgentHeartbeat evtAgentHeartbeat) {
        String machineId = evtAgentHeartbeat.getMachineId();
        long timestamp = evtAgentHeartbeat.getTimestamp();

        MemoryDetail memoryDetail = evtAgentHeartbeat.getMemoryDetail();
        List<DiskDetail> diskDetails = evtAgentHeartbeat.getDiskDetails();
        MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
        if (machineInfo != null) {
            String machineIpv4 = machineInfo.getMachineIpv4();
            //log.info("heartbeat from {}", machineIpv4);
        } else {
            //log.info("heartbeat from {}", machineId);
        }
    }
}
